Skip to content

Commit

Permalink
feat: use unescaped heading text (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jul 12, 2023
1 parent 51ddd6a commit 014f410
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const { marked } = require('marked');
let JSDOM,
createDOMPurify;

const { encodeURL, slugize, stripHTML, url_for, isExternalLink, escapeHTML: escape } = require('hexo-util');
const { encodeURL, slugize, stripHTML, url_for, isExternalLink, escapeHTML: escape, unescapeHTML: unescape } = require('hexo-util');
const MarkedRenderer = marked.Renderer;
const MarkedTokenizer = marked.Tokenizer;
const { basename, dirname, extname, join } = require('path').posix;
const rATag = /<a(?:\s+?|\s+?[^<>]+\s+?)?href=["'](?:#)([^<>"']+)["'][^<>]*>/i;
const rDlSyntax = /(?:^|\s)(\S.+)<br>:\s+(\S.+)/;

const anchorId = (str, transformOption) => {
return slugize(str.trim(), {transform: transformOption});
return slugize(stripHTML(unescape(str)).trim(), { transform: transformOption });
};

class Renderer extends MarkedRenderer {
Expand All @@ -33,13 +33,13 @@ class Renderer extends MarkedRenderer {
}

const transformOption = modifyAnchors;
let id = anchorId(stripHTML(text), transformOption);
let id = anchorId(text, transformOption);
const headingId = _headingId;

const anchorAliasOpt = anchorAlias && text.startsWith('<a href="#');
if (anchorAliasOpt) {
const customAnchor = text.match(rATag)[1];
id = anchorId(stripHTML(customAnchor), transformOption);
id = anchorId(customAnchor, transformOption);
}

// Add a number after id if repeated
Expand Down Expand Up @@ -208,14 +208,14 @@ module.exports = function(data, options) {

// exec filter to extend renderer.
const renderer = new Renderer(this);
this.execFilterSync('marked:renderer', renderer, {context: this});
this.execFilterSync('marked:renderer', renderer, { context: this });

const tokenizer = new Tokenizer();
this.execFilterSync('marked:tokenizer', tokenizer, {context: this});
this.execFilterSync('marked:tokenizer', tokenizer, { context: this });

const extensions = [];
this.execFilterSync('marked:extensions', extensions, {context: this});
marked.use({extensions});
this.execFilterSync('marked:extensions', extensions, { context: this });
marked.use({ extensions });

let postPath = '';
if (path && post_asset_folder && prependRoot && postAsset) {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ describe('Marked renderer', () => {
].join(''));
});

it('should use unescaped heading text https://github.com/hexojs/hexo-renderer-marked/issues/246', () => {
const body = '## 1.1 aaa/bbb';

const result = r({ text: body });

result.should.eql('<h2 id="1-1-aaa-bbb"><a href="#1-1-aaa-bbb" class="headerlink" title="1.1 aaa&#x2F;bbb"></a>1.1 aaa&#x2F;bbb</h2>');
});

describe('anchorAlias', () => {
beforeEach(() => { hexo.config.marked.anchorAlias = true; });

Expand Down

0 comments on commit 014f410

Please sign in to comment.