From c9d4f7abc94a2cbc4bb558013775df380c1c8376 Mon Sep 17 00:00:00 2001 From: Koy Date: Thu, 15 Oct 2020 09:13:35 +0800 Subject: [PATCH] fix: the sidebar links to another site. (#1336) * fix : the sidebar links to another site --- src/core/render/compiler.js | 12 ++++++------ src/core/render/compiler/headline.js | 12 ++++++------ src/core/render/utils.js | 10 ++++++++++ test/unit/render-util.test.js | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 test/unit/render-util.test.js diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 17e0a8144..557507721 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -5,7 +5,7 @@ import { tree as treeTpl } from './tpl'; import { genTree } from './gen-tree'; import { slugify } from './slugify'; import { emojify } from './emojify'; -import { getAndRemoveConfig } from './utils'; +import { getAndRemoveConfig, removeAtag } from './utils'; import { imageCompiler } from './compiler/image'; import { highlightCodeCompiler } from './compiler/code'; import { paragraphCompiler } from './compiler/paragraph'; @@ -206,29 +206,29 @@ export class Compiler { */ origin.heading = renderer.heading = function(text, level) { let { str, config } = getAndRemoveConfig(text); - const nextToc = { level, title: str }; + const nextToc = { level, title: removeAtag(str) }; if (//g.test(str)) { str = str.replace('', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreSubHeading = true; } if (/{docsify-ignore}/g.test(str)) { str = str.replace('{docsify-ignore}', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreSubHeading = true; } if (//g.test(str)) { str = str.replace('', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreAllSubs = true; } if (/{docsify-ignore-all}/g.test(str)) { str = str.replace('{docsify-ignore-all}', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreAllSubs = true; } diff --git a/src/core/render/compiler/headline.js b/src/core/render/compiler/headline.js index 48ae9e8c9..61e4b3fb9 100644 --- a/src/core/render/compiler/headline.js +++ b/src/core/render/compiler/headline.js @@ -1,32 +1,32 @@ -import { getAndRemoveConfig } from '../utils'; +import { getAndRemoveConfig, removeAtag } from '../utils'; import { slugify } from './slugify'; export const headingCompiler = ({ renderer, router, _self }) => (renderer.code = (text, level) => { let { str, config } = getAndRemoveConfig(text); - const nextToc = { level, title: str }; + const nextToc = { level, title: removeAtag(str) }; if (//g.test(str)) { str = str.replace('', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreSubHeading = true; } if (/{docsify-ignore}/g.test(str)) { str = str.replace('{docsify-ignore}', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreSubHeading = true; } if (//g.test(str)) { str = str.replace('', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreAllSubs = true; } if (/{docsify-ignore-all}/g.test(str)) { str = str.replace('{docsify-ignore-all}', ''); - nextToc.title = str; + nextToc.title = removeAtag(str); nextToc.ignoreAllSubs = true; } diff --git a/src/core/render/utils.js b/src/core/render/utils.js index 100d595cd..bd892c653 100644 --- a/src/core/render/utils.js +++ b/src/core/render/utils.js @@ -38,3 +38,13 @@ export function getAndRemoveConfig(str = '') { return { str, config }; } + +/** + * Remove the tag from sidebar when the header with link, details see issue 1069 + * @param {string} str The string to deal with. + * + * @return {string} str The string after delete the element. + */ +export function removeAtag(str = '') { + return str.replace(/(<\/?a.*?>)/gi, ''); +} diff --git a/test/unit/render-util.test.js b/test/unit/render-util.test.js new file mode 100644 index 000000000..3a82a0f9d --- /dev/null +++ b/test/unit/render-util.test.js @@ -0,0 +1,15 @@ +const { removeAtag } = require(`${SRC_PATH}/core/render/utils`); + +// Suite +// ----------------------------------------------------------------------------- +describe('core/render/utils', () => { + // removeAtag() + // --------------------------------------------------------------------------- + describe('removeAtag()', () => { + test('removeAtag from a link', () => { + const result = removeAtag('content'); + + expect(result).toEqual('content'); + }); + }); +});