Skip to content

Commit de3f8bc

Browse files
tomapcurbengh
authored andcommitted
feat: Add option to disable headerIds (#106)
* Add option to disable headerIds (Enabled by default for backward compatibility) Fixes #61 * Update README.md
1 parent 483d99d commit de3f8bc

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ marked:
2929
modifyAnchors: ''
3030
autolink: true
3131
sanitizeUrl: false
32+
headerIds: true
3233
```
3334
3435
- **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown)
@@ -39,6 +40,7 @@ marked:
3940
- **modifyAnchors** - Use for transform anchorIds. if `1` to lowerCase and if `2` to upperCase. **Must be integer**.
4041
- **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `<a href="https://hexo.io">https://hexo.io</a>`.
4142
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.
43+
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.
4244

4345
## Extras
4446

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ hexo.config.marked = Object.assign({
1212
smartypants: true,
1313
modifyAnchors: '',
1414
autolink: true,
15-
sanitizeUrl: false
15+
sanitizeUrl: false,
16+
headerIds: true
1617
}, hexo.config.marked);
1718

1819
hexo.extend.renderer.register('md', 'html', renderer, true);

lib/renderer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ require('util').inherits(Renderer, MarkedRenderer);
1515

1616
// Add id attribute to headings
1717
Renderer.prototype.heading = function(text, level) {
18+
if (!this.options.headerIds) {
19+
return `<h${level}>${text}</h${level}>`;
20+
}
21+
1822
const transformOption = this.options.modifyAnchors;
1923
let id = anchorId(stripHTML(text), transformOption);
2024
const headingId = this._headingId;

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ describe('Marked renderer', () => {
5959
result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
6060
});
6161

62+
it('should render headings without headerIds when disabled', () => {
63+
const body = '## hexo-server';
64+
ctx.config.marked.headerIds = false;
65+
66+
const result = r({text: body});
67+
68+
result.should.eql([
69+
'<h2>hexo-server</h2>'
70+
].join(''));
71+
});
72+
6273
// Description List tests
6374

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

0 commit comments

Comments
 (0)