diff --git a/.gitignore b/.gitignore index d1d5c6d..ca1d708 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- node_modules + +.DS_Store diff --git a/lib/kramed.js b/lib/kramed.js index 5319d79..b10a8f1 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -93,7 +93,7 @@ function kramed(src, opt, callback) { return; } try { - if (opt) opt = merge({}, kramed.defaults, opt); + opt = merge({}, kramed.defaults, opt || {}); return Parser.parse(Lexer.lex(src, opt), opt); } catch (e) { e.message += '\nPlease report this to https://github.com/GitbookIO/kramed.'; @@ -134,6 +134,7 @@ kramed.defaults = { langPrefix: 'lang-', smartypants: false, headerPrefix: '', + autoIds: true, xhtml: false, // Default rendrer passed to Parser diff --git a/lib/renderer.js b/lib/renderer.js index 89f2db2..243f761 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -1,6 +1,7 @@ var _utils = require('./utils'); var escape = _utils.escape; var unescape = _utils.unescape; +var merge = _utils.merge; /** * Renderer @@ -10,11 +11,12 @@ var defaultOptions = { langPrefix: 'lang-', smartypants: false, headerPrefix: '', + autoIds: true, xhtml: false, }; function Renderer(options) { - this.options = options || defaultOptions; + this.options = merge(defaultOptions, options || {}); } Renderer.prototype.code = function(code, lang, escaped) { @@ -60,14 +62,20 @@ Renderer.prototype._createId = function(str) { }; Renderer.prototype.heading = function(text, level, raw) { - var id = /({#)(.+)(})/g.exec(raw); + var hash = /(\s{#)(.+)(})/g.exec(raw); + var id = ''; + + if (hash) { + id = ' id="' + hash[2] + '"'; + } else if(this.options.autoIds) { + id = ' id="' + this.options.headerPrefix + this._createId(raw) + '"'; + } + return '' - + text.replace(/{#.+}/g, '') + + id + + '>' + + text.replace(/\s{#.+}/g, '') + '\n'; diff --git a/test/tests/header_id.html b/test/tests/header_id.html index 50cbf08..1025d4d 100644 --- a/test/tests/header_id.html +++ b/test/tests/header_id.html @@ -5,3 +5,5 @@

Hello World.

no ascii 中文

special characters � '`"|,.\/

+ +

Heading with ID

diff --git a/test/tests/header_id.txt b/test/tests/header_id.txt index d239815..8a3702d 100644 --- a/test/tests/header_id.txt +++ b/test/tests/header_id.txt @@ -5,3 +5,5 @@ ## no ascii 中文 ### special characters � '`"|,.\/ + +# Heading with ID {#myid}