Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove leading markup from h1 header (closes #693, closes #689) #694

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/util/parseHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ const removeMarkdownToken = str => String(str)
.replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_
.replace(/(\\)(\*|_|`)/g, '$2') // remove escape char '\'

exports.removeTailHtml = (str) => {
return String(str).replace(/\s*?<.*>\s*$/g, '')
}
exports.removeTailHtml = (str) => String(str)
.replace(/\s*?<.*>\s*$/g, '')
.replace(/\s`<(.*?)\/>`$/g, ' <code><$1></code>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <code> was generated by markdown loader instead of this method.


exports.removeLeadingHtml = (str) => String(str)
.replace(/^(#\s)<.*\/?>./g, '$1')
.replace(/^(#)<.*\/?>./g, '$1 ')
.replace(/^(#\s)`<(.*?)\/?>`/g, '$1<code><$2></code>')
.replace(/^(#)`<(.*?)\/?>`/g, '$1 <code><$2></code>')

// Only remove some md tokens.
exports.parseHeaders = compose(
Expand All @@ -32,6 +38,7 @@ exports.parseHeaders = compose(
// Since we want to support tailed badge in headers.
// See: https://vuepress.vuejs.org/guide/using-vue.html#badge
exports.deeplyParseHeaders = compose(
exports.removeLeadingHtml,
exports.removeTailHtml,
exports.parseHeaders,
)
13 changes: 13 additions & 0 deletions test/util/parseHeaders.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
parseHeaders,
removeTailHtml,
removeLeadingHtml,
deeplyParseHeaders
} from '@/util/parseHeaders'

Expand Down Expand Up @@ -39,8 +40,20 @@ describe('parseHeaders', () => {
expect(removeTailHtml('# H1 <Comp a="b"/>')).toBe('# H1')
})

test('should strip leading html correctly', () => {
expect(removeLeadingHtml('# <Comp/> H1')).toBe('# H1')
expect(removeLeadingHtml('# <Comp></Comp> H1')).toBe('# H1')
expect(removeLeadingHtml('#<Comp></Comp> H1')).toBe('# H1')
expect(removeLeadingHtml('# `<Comp/>` H1')).toBe('# <code><Comp></code> H1')
expect(removeLeadingHtml('#`<Comp/>` H1')).toBe('# <code><Comp></code> H1')
})

test('should deeplyParseHeaders transformed as expected', () => {
expect(deeplyParseHeaders('# `H1` <Comp></Comp>')).toBe('# H1')
expect(deeplyParseHeaders('# *H1* <Comp/>')).toBe('# H1')
expect(deeplyParseHeaders('# *H1* <Badge text="test"/>')).toBe('# H1')
expect(deeplyParseHeaders('# `<Comp/>` `H1`')).toBe('# <code><Comp></code> H1')
expect(deeplyParseHeaders('# <Comp/> `H1` <Comp/>')).toBe('# H1')
expect(deeplyParseHeaders('# `<Comp/>` `H1` `<Comp/>`')).toBe('# <code><Comp></code> H1 <code><Comp></code>')
})
})