Skip to content

Commit

Permalink
feat: use unescaped heading text
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jul 11, 2023
1 parent f134063 commit 5b2a526
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 5b2a526

Please sign in to comment.