From a4d69a0382a0aa76cab3b45e3fc946b99f220479 Mon Sep 17 00:00:00 2001 From: Robert Hamel Date: Wed, 1 Aug 2018 21:55:48 -0400 Subject: [PATCH] feat(parsing): remove training markup from h1 header --- lib/util/parseHeaders.js | 13 ++++++++++--- test/util/parseHeaders.spec.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/util/parseHeaders.js b/lib/util/parseHeaders.js index 77b701c528..f999bd22bd 100644 --- a/lib/util/parseHeaders.js +++ b/lib/util/parseHeaders.js @@ -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, ' <$1>') + +exports.removeLeadingHtml = (str) => String(str) + .replace(/^(#\s)<.*\/?>./g, '$1') + .replace(/^(#)<.*\/?>./g, '$1 ') + .replace(/^(#\s)`<(.*?)\/?>`/g, '$1<$2>') + .replace(/^(#)`<(.*?)\/?>`/g, '$1 <$2>') // Only remove some md tokens. exports.parseHeaders = compose( @@ -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, ) diff --git a/test/util/parseHeaders.spec.js b/test/util/parseHeaders.spec.js index 3d1eb6a8be..ea3d089194 100644 --- a/test/util/parseHeaders.spec.js +++ b/test/util/parseHeaders.spec.js @@ -1,6 +1,7 @@ import { parseHeaders, removeTailHtml, + removeLeadingHtml, deeplyParseHeaders } from '@/util/parseHeaders' @@ -39,8 +40,20 @@ describe('parseHeaders', () => { expect(removeTailHtml('# H1 ')).toBe('# H1') }) + test('should strip leading html correctly', () => { + expect(removeLeadingHtml('# H1')).toBe('# H1') + expect(removeLeadingHtml('# H1')).toBe('# H1') + expect(removeLeadingHtml('# H1')).toBe('# H1') + expect(removeLeadingHtml('# `` H1')).toBe('# H1') + expect(removeLeadingHtml('#`` H1')).toBe('# H1') + }) + test('should deeplyParseHeaders transformed as expected', () => { expect(deeplyParseHeaders('# `H1` ')).toBe('# H1') expect(deeplyParseHeaders('# *H1* ')).toBe('# H1') + expect(deeplyParseHeaders('# *H1* ')).toBe('# H1') + expect(deeplyParseHeaders('# `` `H1`')).toBe('# H1') + expect(deeplyParseHeaders('# `H1` ')).toBe('# H1') + expect(deeplyParseHeaders('# `` `H1` ``')).toBe('# H1 ') }) })