diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index 934f15e23ba7..199a9cc55892 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -10,7 +10,7 @@ import { snippetPlugin } from './plugins/snippet' import { hoistPlugin } from './plugins/hoist' import { preWrapperPlugin } from './plugins/preWrapper' import { linkPlugin } from './plugins/link' -import { extractHeaderPlugin } from './plugins/header' +import { headingPlugin } from './plugins/headings' import { Header } from '../shared' import anchor from 'markdown-it-anchor' import attrs from 'markdown-it-attrs' @@ -65,7 +65,7 @@ export const createMarkdownRenderer = ( .use(snippetPlugin, srcDir) .use(hoistPlugin) .use(containerPlugin) - .use(extractHeaderPlugin) + .use(headingPlugin) .use(linkPlugin, { target: '_blank', rel: 'noopener noreferrer', diff --git a/src/node/markdown/plugins/header.ts b/src/node/markdown/plugins/headings.ts similarity index 90% rename from src/node/markdown/plugins/header.ts rename to src/node/markdown/plugins/headings.ts index 8c6e5923cf3a..ce186862763e 100644 --- a/src/node/markdown/plugins/header.ts +++ b/src/node/markdown/plugins/headings.ts @@ -3,7 +3,7 @@ import { deeplyParseHeader } from '../../utils/parseHeader' import { slugify } from './slugify' import MarkdownIt from 'markdown-it' -export const extractHeaderPlugin = (md: MarkdownIt, include = ['h2', 'h3']) => { +export const headingPlugin = (md: MarkdownIt, include = ['h2', 'h3']) => { md.renderer.rules.heading_open = (tokens, i, options, env, self) => { const token = tokens[i] if (include.includes(token.tag)) { diff --git a/src/node/utils/parseHeader.ts b/src/node/utils/parseHeader.ts index 9fe051840c89..d1b40c8068b2 100644 --- a/src/node/utils/parseHeader.ts +++ b/src/node/utils/parseHeader.ts @@ -12,14 +12,14 @@ import emojiData from 'markdown-it-emoji/lib/data/full.json' const parseEmojis = (str: string) => { - return String(str).replace( + return str.replace( /:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder ) } const unescapeHtml = (html: string) => - String(html) + html .replace(/"/g, '"') .replace(/'/g, "'") .replace(/:/g, ':') @@ -27,11 +27,14 @@ const unescapeHtml = (html: string) => .replace(/>/g, '>') const removeMarkdownTokens = (str: string) => - String(str) + str .replace(/(\[(.[^\]]+)\]\((.[^)]+)\))/g, '$2') // []() .replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_ .replace(/(\\)(\*|_|`|\!|<|\$)/g, '$2') // remove escape char '\' +const remvoeCustomAnchor = (str: string) => + str.replace(/\{#([a-z0-9\-_]+?)\}\s*$/, '') // {#custom-header} + const trim = (str: string) => str.trim() // This method remove the raw HTML but reserve the HTML wrapped by ``. @@ -54,6 +57,7 @@ const compose = (...processors: ((str: string) => string)[]) => { export const parseHeader = compose( unescapeHtml, parseEmojis, + remvoeCustomAnchor, removeMarkdownTokens, trim )