-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: properly remove custom anchor in headers
- Loading branch information
Showing
3 changed files
with
10 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(/</g, '<') | ||
.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) => | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yyx990803
Author
Member
|
||
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>`. | ||
|
@@ -54,6 +57,7 @@ const compose = (...processors: ((str: string) => string)[]) => { | |
export const parseHeader = compose( | ||
unescapeHtml, | ||
parseEmojis, | ||
remvoeCustomAnchor, | ||
removeMarkdownTokens, | ||
trim | ||
) | ||
|
@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.