diff --git a/README.md b/README.md index 872589e..90e3e01 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ marked: modifyAnchors: '' autolink: true sanitizeUrl: false + headerIds: true ``` - **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown) @@ -39,6 +40,7 @@ marked: - **modifyAnchors** - Use for transform anchorIds. if `1` to lowerCase and if `2` to upperCase. **Must be integer**. - **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `https://hexo.io`. - **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`. +- **headerIds** - Insert header id, e.g. `

text

`. Useful for inserting anchor link to each paragraph with a heading. ## Extras diff --git a/index.js b/index.js index 66505fe..aa1605c 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,8 @@ hexo.config.marked = Object.assign({ smartypants: true, modifyAnchors: '', autolink: true, - sanitizeUrl: false + sanitizeUrl: false, + headerIds: true }, hexo.config.marked); hexo.extend.renderer.register('md', 'html', renderer, true); diff --git a/lib/renderer.js b/lib/renderer.js index 4fb2397..61f9fb3 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -15,6 +15,10 @@ require('util').inherits(Renderer, MarkedRenderer); // Add id attribute to headings Renderer.prototype.heading = function(text, level) { + if (!this.options.headerIds) { + return `${text}`; + } + const transformOption = this.options.modifyAnchors; let id = anchorId(stripHTML(text), transformOption); const headingId = this._headingId; diff --git a/test/index.js b/test/index.js index 1738791..169b9b9 100644 --- a/test/index.js +++ b/test/index.js @@ -59,6 +59,17 @@ describe('Marked renderer', () => { result.should.eql('

中文

'); }); + it('should render headings without headerIds when disabled', () => { + const body = '## hexo-server'; + ctx.config.marked.headerIds = false; + + const result = r({text: body}); + + result.should.eql([ + '

hexo-server

' + ].join('')); + }); + // Description List tests it('should render description lists with a single space after the colon', () => {