Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/kramed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -134,6 +134,7 @@ kramed.defaults = {
langPrefix: 'lang-',
smartypants: false,
headerPrefix: '',
autoIds: true,
xhtml: false,

// Default rendrer passed to Parser
Expand Down
22 changes: 15 additions & 7 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _utils = require('./utils');
var escape = _utils.escape;
var unescape = _utils.unescape;
var merge = _utils.merge;

/**
* Renderer
Expand All @@ -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) {
Expand Down Expand Up @@ -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 '<h'
+ level
+ ' id="'
+ this.options.headerPrefix
+ (id ? id[2] : this._createId(raw))
+ '">'
+ text.replace(/{#.+}/g, '')
+ id
+ '>'
+ text.replace(/\s{#.+}/g, '')
+ '</h'
+ level
+ '>\n';
Expand Down
2 changes: 2 additions & 0 deletions test/tests/header_id.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ <h1 id="hello-world">Hello World.</h1>
<h2 id="no-ascii-%E4%B8%AD%E6%96%87">no ascii 中文</h2>

<h3 id="special-characters-%EF%BF%BD">special characters � &#39;`&quot;|,.\/</h3>

<h1 id="myid">Heading with ID</h1>
2 changes: 2 additions & 0 deletions test/tests/header_id.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
## no ascii 中文

### special characters � '`"|,.\/

# Heading with ID {#myid}