Skip to content

Commit

Permalink
feat: properly remove custom anchor in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 6, 2022
1 parent 294b1d2 commit 6120da2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 7 additions & 3 deletions src/node/utils/parseHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@
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, ':')
.replace(/&lt;/g, '<')
.replace(/&gt;/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) =>

This comment has been minimized.

Copy link
@yyx990803

yyx990803 Jan 6, 2022

Author Member

@ShenQingchuan this is actually all that is needed because markdown-it-attrs already support the {#custom-anchor} syntax. We just need to remove it from parsed heading content.

This comment has been minimized.

Copy link
@yyx990803

yyx990803 Jan 6, 2022

Author Member

Should no longer need the custom logic for handling anchors - can also remove render-perma-link.

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 `<code>`.
Expand All @@ -54,6 +57,7 @@ const compose = (...processors: ((str: string) => string)[]) => {
export const parseHeader = compose(
unescapeHtml,
parseEmojis,
remvoeCustomAnchor,
removeMarkdownTokens,
trim
)
Expand Down

0 comments on commit 6120da2

Please sign in to comment.