feat: properly remove custom anchor in headers

pull/486/head
Evan You 3 years ago
parent 294b1d2817
commit 6120da25a8

@ -10,7 +10,7 @@ import { snippetPlugin } from './plugins/snippet'
import { hoistPlugin } from './plugins/hoist' import { hoistPlugin } from './plugins/hoist'
import { preWrapperPlugin } from './plugins/preWrapper' import { preWrapperPlugin } from './plugins/preWrapper'
import { linkPlugin } from './plugins/link' import { linkPlugin } from './plugins/link'
import { extractHeaderPlugin } from './plugins/header' import { headingPlugin } from './plugins/headings'
import { Header } from '../shared' import { Header } from '../shared'
import anchor from 'markdown-it-anchor' import anchor from 'markdown-it-anchor'
import attrs from 'markdown-it-attrs' import attrs from 'markdown-it-attrs'
@ -65,7 +65,7 @@ export const createMarkdownRenderer = (
.use(snippetPlugin, srcDir) .use(snippetPlugin, srcDir)
.use(hoistPlugin) .use(hoistPlugin)
.use(containerPlugin) .use(containerPlugin)
.use(extractHeaderPlugin) .use(headingPlugin)
.use(linkPlugin, { .use(linkPlugin, {
target: '_blank', target: '_blank',
rel: 'noopener noreferrer', rel: 'noopener noreferrer',

@ -3,7 +3,7 @@ import { deeplyParseHeader } from '../../utils/parseHeader'
import { slugify } from './slugify' import { slugify } from './slugify'
import MarkdownIt from 'markdown-it' 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) => { md.renderer.rules.heading_open = (tokens, i, options, env, self) => {
const token = tokens[i] const token = tokens[i]
if (include.includes(token.tag)) { if (include.includes(token.tag)) {

@ -12,14 +12,14 @@
import emojiData from 'markdown-it-emoji/lib/data/full.json' import emojiData from 'markdown-it-emoji/lib/data/full.json'
const parseEmojis = (str: string) => { const parseEmojis = (str: string) => {
return String(str).replace( return str.replace(
/:(.+?):/g, /:(.+?):/g,
(placeholder, key) => emojiData[key] || placeholder (placeholder, key) => emojiData[key] || placeholder
) )
} }
const unescapeHtml = (html: string) => const unescapeHtml = (html: string) =>
String(html) html
.replace(/"/g, '"') .replace(/"/g, '"')
.replace(/'/g, "'") .replace(/'/g, "'")
.replace(/:/g, ':') .replace(/:/g, ':')
@ -27,11 +27,14 @@ const unescapeHtml = (html: string) =>
.replace(/>/g, '>') .replace(/>/g, '>')
const removeMarkdownTokens = (str: string) => const removeMarkdownTokens = (str: string) =>
String(str) str
.replace(/(\[(.[^\]]+)\]\((.[^)]+)\))/g, '$2') // []() .replace(/(\[(.[^\]]+)\]\((.[^)]+)\))/g, '$2') // []()
.replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_ .replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_
.replace(/(\\)(\*|_|`|\!|<|\$)/g, '$2') // remove escape char '\' .replace(/(\\)(\*|_|`|\!|<|\$)/g, '$2') // remove escape char '\'
const remvoeCustomAnchor = (str: string) =>
str.replace(/\{#([a-z0-9\-_]+?)\}\s*$/, '') // {#custom-header}
const trim = (str: string) => str.trim() const trim = (str: string) => str.trim()
// This method remove the raw HTML but reserve the HTML wrapped by `<code>`. // This method remove the raw HTML but reserve the HTML wrapped by `<code>`.
@ -54,6 +57,7 @@ const compose = (...processors: ((str: string) => string)[]) => {
export const parseHeader = compose( export const parseHeader = compose(
unescapeHtml, unescapeHtml,
parseEmojis, parseEmojis,
remvoeCustomAnchor,
removeMarkdownTokens, removeMarkdownTokens,
trim trim
) )

Loading…
Cancel
Save