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 2 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 README.md
Original file line number Diff line number Diff line change
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** - will insert a header id, useful to insert anchor links to specific paragraphs
tomap marked this conversation as resolved.
Show resolved Hide resolved

## Extras

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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