|
|
@ -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
|
|
|
|
)
|
|
|
|
)
|
|
|
|