Skip to content

Commit

Permalink
feat: Add option to disable headerIds (#106)
Browse files Browse the repository at this point in the history
* Add option to disable headerIds
(Enabled by default for backward compatibility)
Fixes #61

* Update README.md
  • Loading branch information
tomap authored and curbengh committed Aug 11, 2019
1 parent 483d99d commit de3f8bc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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)
Expand All @@ -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 `<a href="https://hexo.io">https://hexo.io</a>`.
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.

## Extras

Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions lib/renderer.js
Expand Up @@ -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 `<h${level}>${text}</h${level}>`;
}

const transformOption = this.options.modifyAnchors;
let id = anchorId(stripHTML(text), transformOption);
const headingId = this._headingId;
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Expand Up @@ -59,6 +59,17 @@ describe('Marked renderer', () => {
result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
});

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([
'<h2>hexo-server</h2>'
].join(''));
});

// Description List tests

it('should render description lists with a single space after the colon', () => {
Expand Down

0 comments on commit de3f8bc

Please sign in to comment.