Created
September 10, 2020 09:53
-
-
Save shaunthegeek/12a273020749952e2d30cf4b2bd415e8 to your computer and use it in GitHub Desktop.
hexo custom header ID(anchor alias)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff -uNr node_modules/hexo-renderer-marked/index.js hexo-renderer-marked/index.js | |
| --- node_modules/hexo-renderer-marked/index.js 1985-10-26 16:15:00.000000000 +0800 | |
| +++ hexo-renderer-marked/index.js 2020-09-10 16:08:59.441937082 +0800 | |
| @@ -15,6 +15,7 @@ | |
| mangle: true, | |
| sanitizeUrl: false, | |
| headerIds: true, | |
| + anchorAlias: false, | |
| lazyload: false, | |
| // TODO: enable prependRoot by default in v3 | |
| prependRoot: false, | |
| diff -uNr node_modules/hexo-renderer-marked/lib/renderer.js hexo-renderer-marked/lib/renderer.js | |
| --- node_modules/hexo-renderer-marked/lib/renderer.js 1985-10-26 16:15:00.000000000 +0800 | |
| +++ hexo-renderer-marked/lib/renderer.js 2020-09-10 16:19:15.592954986 +0800 | |
| @@ -27,7 +27,26 @@ | |
| } | |
| const transformOption = modifyAnchors; | |
| - let id = anchorId(stripHTML(text), transformOption); | |
| + const aliasOption = this.options.anchorAlias; | |
| + | |
| + // https://regex101.com/r/Kw7KzC/1 | |
| + const hrefRegex = /(<a\s+(?:[^>]*?\s+)?href=(["']))(.*?)\2/i; | |
| + const matches = text.match(hrefRegex); | |
| + const isLink = matches && matches[3] && matches[3].startsWith('#'); | |
| + const innerText = stripHTML(text); | |
| + let idText = innerText; | |
| + | |
| + // Use href attribute as id | |
| + if (aliasOption && isLink) { | |
| + idText = matches[3]; | |
| + } | |
| + | |
| + // For `[]()` | |
| + if (!innerText && !idText) { | |
| + return '<h' + level + '></h' + level + '>'; | |
| + } | |
| + | |
| + let id = anchorId(idText, transformOption); | |
| const headingId = _headingId; | |
| // Add a number after id if repeated | |
| @@ -37,8 +56,15 @@ | |
| headingId[id] = 1; | |
| } | |
| + // if `text` is a <a> element, replace the href with the generated id; | |
| + // else, `text` is plain string, just keep it. | |
| + let innerHTML = text; | |
| + if (isLink) { | |
| + innerHTML = text.replace(hrefRegex, '$1#' + id + '$2'); | |
| + } | |
| + | |
| // add headerlink | |
| - return `<h${level} id="${id}"><a href="#${id}" class="headerlink" title="${stripHTML(text)}"></a>${text}</h${level}>`; | |
| + return `<h${level} id="${id}"><a href="#${id}" class="headerlink" title="${innerText}"></a>${innerHTML}</h${level}>`; | |
| } | |
| link(href, title, text) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "private": true, | |
| "hexo": { | |
| "version": "5.1.1" | |
| }, | |
| "scripts": { | |
| "dev": "hexo serve", | |
| "postinstall": "find patches/ -name '*.patch' -print0 | xargs patch -N -p0 -i || true", | |
| }, | |
| "devDependencies": { | |
| "hexo": "^5.1.1", | |
| "hexo-renderer-marked": "^3.2.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment