From c992e38cd8240ff1f44c6d2c4f61da9b559522dc Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Fri, 13 Jul 2018 01:07:31 +0800 Subject: [PATCH] fix: wrong sidebar slugs and anchor link at content (close: #645) --- lib/markdown/index.js | 5 ++--- lib/util/parseHeaders.js | 6 ++++-- test/markdown/slugify.spec.js | 32 ++++++++++++++++++++++++++++++++ test/util/parseHeaders.spec.js | 13 +++++++++++-- 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 test/markdown/slugify.spec.js 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..b37a8d94c1 100644 --- a/lib/util/parseHeaders.js +++ b/lib/util/parseHeaders.js @@ -21,14 +21,16 @@ 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 need to support tailed badge in headers. +// See: https://vuepress.vuejs.org/guide/using-vue.html#badge exports.deeplyParseHeaders = compose( 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') }) })