diff --git a/package.json b/package.json index 703640ef..9e6aabf9 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,6 @@ "lodash": "4.17.15", "markdown-it": "10.0.0", "markdown-it-abbr": "1.0.4", - "markdown-it-anchor": "5.2.5", "markdown-it-attrs": "3.0.1", "markdown-it-emoji": "1.4.0", "markdown-it-expand-tabs": "1.0.13", diff --git a/server/modules/rendering/html-core/renderer.js b/server/modules/rendering/html-core/renderer.js index 1d3f88e3..cb98e505 100644 --- a/server/modules/rendering/html-core/renderer.js +++ b/server/modules/rendering/html-core/renderer.js @@ -1,5 +1,6 @@ const _ = require('lodash') const cheerio = require('cheerio') +const uslug = require('uslug') /* global WIKI */ @@ -142,6 +143,43 @@ module.exports = { } } + // -------------------------------- + // Add header handles + // -------------------------------- + + let headers = [] + $('h1,h2,h3,h4,h5,h6').each((i, elm) => { + if ($(elm).attr('id')) { + return + } + let headerSlug = uslug($(elm).text()) + + // -> Cannot start with a number (CSS selector limitation) + if (headerSlug.match(/^\d/)) { + headerSlug = `h-${headerSlug}` + } + + // -> Make sure header is unique + if (headers.indexOf(headerSlug) >= 0) { + let isUnique = false + let hIdx = 1 + while (!isUnique) { + const headerSlugTry = `${headerSlug}-${hIdx}` + if (headers.indexOf(headerSlugTry) < 0) { + isUnique = true + headerSlug = headerSlugTry + } + hIdx++ + } + } + + // -> Add anchor + $(elm).attr('id', headerSlug).addClass('toc-header') + $(elm).prepend(` `) + + headers.push(headerSlug) + }) + return $.html('body').replace('', '').replace('', '') } } diff --git a/server/modules/rendering/markdown-core/renderer.js b/server/modules/rendering/markdown-core/renderer.js index 44523ea2..40ded6cf 100644 --- a/server/modules/rendering/markdown-core/renderer.js +++ b/server/modules/rendering/markdown-core/renderer.js @@ -1,8 +1,6 @@ const md = require('markdown-it') -const mdAnchor = require('markdown-it-anchor') const mdAttrs = require('markdown-it-attrs') const _ = require('lodash') -const uslug = require('uslug') const quoteStyles = { Chinese: '””‘’', @@ -32,14 +30,6 @@ module.exports = { } }) - mkdown.use(mdAnchor, { - slugify: s => uslug(s), - permalink: true, - permalinkClass: 'toc-anchor', - permalinkSymbol: '¶', - permalinkBefore: true - }) - mkdown.use(mdAttrs) for (let child of this.children) { diff --git a/yarn.lock b/yarn.lock index 7ffbbe37..a439fd54 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8223,11 +8223,6 @@ markdown-it-abbr@1.0.4: resolved "https://registry.yarnpkg.com/markdown-it-abbr/-/markdown-it-abbr-1.0.4.tgz#d66b5364521cbb3dd8aa59dadfba2fb6865c8fd8" integrity sha1-1mtTZFIcuz3Yqlna37ovtoZcj9g= -markdown-it-anchor@5.2.5: - version "5.2.5" - resolved "https://registry.yarnpkg.com/markdown-it-anchor/-/markdown-it-anchor-5.2.5.tgz#dbf13cfcdbffd16a510984f1263e1d479a47d27a" - integrity sha512-xLIjLQmtym3QpoY9llBgApknl7pxAcN3WDRc2d3rwpl+/YvDZHPmKscGs+L6E05xf2KrCXPBvosWt7MZukwSpQ== - markdown-it-attrs@3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/markdown-it-attrs/-/markdown-it-attrs-3.0.1.tgz#5ce7b4405f2286190fbcf4c482aa9e3e2d55ece5"