diff --git a/lib/markdown/index.js b/lib/markdown/index.js index 06779ac5ef..f4942f6ce6 100644 --- a/lib/markdown/index.js +++ b/lib/markdown/index.js @@ -11,12 +11,11 @@ const emoji = require('markdown-it-emoji') const anchor = require('markdown-it-anchor') const toc = require('markdown-it-table-of-contents') const _slugify = require('./slugify') -const { parseHeaders, removeTailHtml } = require('../util/parseHeaders') -const { compose } = require('../util/shared') +const { parseHeaders } = require('../util/parseHeaders') module.exports = ({ markdown = {}} = {}) => { // allow user config slugify - const slugify = markdown.slugify || compose(removeTailHtml, _slugify) + const slugify = markdown.slugify || _slugify const md = require('markdown-it')({ html: true, diff --git a/lib/util/parseHeaders.js b/lib/util/parseHeaders.js index 60b7cc0023..77b701c528 100644 --- a/lib/util/parseHeaders.js +++ b/lib/util/parseHeaders.js @@ -21,15 +21,17 @@ exports.removeTailHtml = (str) => { return String(str).replace(/\s*?<.*>\s*$/g, '') } -// only remove some md tokens. +// Only remove some md tokens. exports.parseHeaders = compose( unescapeHtml, parseEmojis, removeMarkdownToken ) -// also clean html in headers. +// Also clean the tail html in headers. +// Since we want to support tailed badge in headers. +// See: https://vuepress.vuejs.org/guide/using-vue.html#badge exports.deeplyParseHeaders = compose( + exports.removeTailHtml, exports.parseHeaders, - exports.removeTailHtml ) diff --git a/test/markdown/slugify.spec.js b/test/markdown/slugify.spec.js new file mode 100644 index 0000000000..e3ccb0b778 --- /dev/null +++ b/test/markdown/slugify.spec.js @@ -0,0 +1,32 @@ +import { Md } from './util' +import anchor from 'markdown-it-anchor' +import slugify from '@/markdown/slugify.js' + +const mdS = Md().use(anchor, { + slugify, + permalink: true, + permalinkBefore: true, + permalinkSymbol: '#' +}) + +const slugifyAsserts = { + /* markdown: id */ + '# a b': 'a-b', + '# a-b': 'a-b', + '# ``': 'a', + '# ``b': 'a-b', + '# `` b': 'a-b' +} + +describe('slugify', () => { + test('should convert headers correctly', () => { + for (const input in slugifyAsserts) { + const output = mdS.render(input) + expect(getHeading(output)).toBe(slugifyAsserts[input]) + } + }) +}) + +function getHeading (output) { + return output.match(/id=\\?"([^"]*)\\?"/)[1] +} diff --git a/test/util/parseHeaders.spec.js b/test/util/parseHeaders.spec.js index b16f5aedf2..3d1eb6a8be 100644 --- a/test/util/parseHeaders.spec.js +++ b/test/util/parseHeaders.spec.js @@ -1,6 +1,7 @@ import { parseHeaders, - removeTailHtml + removeTailHtml, + deeplyParseHeaders } from '@/util/parseHeaders' describe('parseHeaders', () => { @@ -32,6 +33,14 @@ describe('parseHeaders', () => { }) test('should remove tail html correctly', () => { - expect(removeTailHtml('# H1
>')).toBe('# H1') + expect(removeTailHtml('# H1 ')).toBe('# H1') + expect(removeTailHtml('# H1 ')).toBe('# H1') + expect(removeTailHtml('# H1 ')).toBe('# H1') + expect(removeTailHtml('# H1 ')).toBe('# H1') + }) + + test('should deeplyParseHeaders transformed as expected', () => { + expect(deeplyParseHeaders('# `H1` ')).toBe('# H1') + expect(deeplyParseHeaders('# *H1* ')).toBe('# H1') }) })