Skip to content

Commit

Permalink
fix($shared-utils): improve title inference and header extraction for…
Browse files Browse the repository at this point in the history
… markdown links syntax
  • Loading branch information
nuochong committed Dec 19, 2020
1 parent cd4ad04 commit d264e50
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
Expand Up @@ -19,6 +19,10 @@ describe('parseHeaders', () => {
'\\*vue\\*': '*vue*',
'\\!vue\\!': '!vue!',

// #2688
'[vue](vuejs.org) / [vue](vuejs.org)': 'vue / vue',
'[\\<ins>](vuejs.org)': '<ins>',

// #564 For multiple markdown tokens
'`a` and `b`': 'a and b',
'***bold and italic***': 'bold and italic',
Expand Down
Expand Up @@ -44,7 +44,10 @@ test('removeNonCodeWrappedHTML', () => {
'# H1 `<Comp></Comp>` H2': '# H1 `<Comp></Comp>` H2',
'# H1 `<Comp a="b"></Comp>` H2': '# H1 `<Comp a="b"></Comp>` H2',
'# H1 `<Comp/>` H2': '# H1 `<Comp/>` H2',
'# H1 `<Comp a="b"/>` H2': '# H1 `<Comp a="b"/>` H2'
'# H1 `<Comp a="b"/>` H2': '# H1 `<Comp a="b"/>` H2',

// #2688
'# \\<ins>': '# \\<ins>'
}

Object.keys(asserts).forEach(input => {
Expand Down
6 changes: 3 additions & 3 deletions packages/@vuepress/shared-utils/src/parseHeaders.ts
Expand Up @@ -15,9 +15,9 @@ import parseEmojis from './parseEmojis'
// wrapped by <code>(markdown token: '`') tag.

const removeMarkdownTokens = (str: string): string => String(str)
.replace(/\[(.*)\]\(.*\)/, '$1') // []()
.replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_
.replace(/(\\)(\*|_|`|\!)/g, '$2') // remove escape char '\'
.replace(/(\[(.[^\]]+)\]\((.[^)]+)\))/g, '$2') // []()
.replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_
.replace(/(\\)(\*|_|`|\!|<)/g, '$2') // remove escape char '\'

const trim = (str: string): string => str.trim()

Expand Down
Expand Up @@ -3,5 +3,5 @@
// Input: "<a> b", Output: "b"
// Input: "`<a>` b", Output: "`<a>` b"
export = function removeNonCodeWrappedHTML (str: string): string {
return String(str).replace(/(^|[^><`])<.*>([^><`]|$)/g, '$1$2')
return String(str).replace(/(^|[^><`\\])<.*>([^><`]|$)/g, '$1$2')
}

0 comments on commit d264e50

Please sign in to comment.