fix(heading-link): compatibility with marked v12

https://github.com/hexojs/hexo-renderer-marked/pull/280
This commit is contained in:
Ming Di Leom 2024-06-07 12:38:33 +00:00
parent a7dc49c013
commit 24c1b84e33
No known key found for this signature in database
GPG Key ID: 32D3E28E96A695E8
1 changed files with 7 additions and 5 deletions

View File

@ -3,20 +3,22 @@
/* /*
* Add Link button next to a heading * Add Link button next to a heading
* based on https://github.com/hexojs/hexo-renderer-marked/blob/master/lib/renderer.js
*/ */
const { slugize, stripHTML } = require('hexo-util') const { slugize, stripHTML, unescapeHTML: unescape } = require('hexo-util')
const anchorId = (str, transformOption) => { const anchorId = (str, transformOption) => {
return slugize(str.trim(), { transform: transformOption }) return slugize(stripHTML(unescape(str)).trim(), { transform: transformOption });
} }
hexo.extend.filter.register('marked:renderer', function (renderer) { hexo.extend.filter.register('marked:renderer', function (renderer) {
const { config } = this const { config } = this
renderer.heading = function (text, level) { renderer.heading = function (text, level) {
const transformOption = config.marked.modifyAnchors const { modifyAnchors } = config.marked
let id = anchorId(stripHTML(text), transformOption) const transformOption = modifyAnchors;
const headingId = this._headingId let id = anchorId(text, transformOption)
const headingId = {};
// Add a number after id if repeated // Add a number after id if repeated
if (headingId[id]) { if (headingId[id]) {