Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable headerIds #106

Merged
merged 3 commits into from Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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