From 5ad8c2c339efd44fd341bfb4542655ec05634aaa Mon Sep 17 00:00:00 2001
From: koooooo-7 <369491420@qq.com>
Date: Sat, 15 Aug 2020 12:02:02 +0800
Subject: [PATCH 1/3] [fix 1069] the sidebar links to another site.
---
src/core/render/compiler.js | 10 +++++-----
src/core/render/utils.js | 10 ++++++++++
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js
index 17e0a8144..3e06ca4b5 100644
--- a/src/core/render/compiler.js
+++ b/src/core/render/compiler.js
@@ -1,17 +1,17 @@
-import marked from 'marked';
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
import { isFn, merge, cached, isPrimitive } from '../util/core';
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';
import { taskListCompiler } from './compiler/taskList';
import { taskListItemCompiler } from './compiler/taskListItem';
import { linkCompiler } from './compiler/link';
+import marked from 'marked';
const cachedLinks = {};
@@ -206,11 +206,11 @@ 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;
}
@@ -222,7 +222,7 @@ export class Compiler {
if (//g.test(str)) {
str = str.replace('', '');
- 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, '');
+}
From 2cbfb9ccd8df765f9aa4798722a2b9e89a1f45f8 Mon Sep 17 00:00:00 2001
From: Koy
Date: Wed, 14 Oct 2020 15:55:07 +0800
Subject: [PATCH 2/3] fix comments.
---
src/core/render/compiler.js | 6 +++---
src/core/render/compiler/headline.js | 12 ++++++------
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js
index 3e06ca4b5..557507721 100644
--- a/src/core/render/compiler.js
+++ b/src/core/render/compiler.js
@@ -1,3 +1,4 @@
+import marked from 'marked';
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
import { isFn, merge, cached, isPrimitive } from '../util/core';
import { tree as treeTpl } from './tpl';
@@ -11,7 +12,6 @@ import { paragraphCompiler } from './compiler/paragraph';
import { taskListCompiler } from './compiler/taskList';
import { taskListItemCompiler } from './compiler/taskListItem';
import { linkCompiler } from './compiler/link';
-import marked from 'marked';
const cachedLinks = {};
@@ -216,7 +216,7 @@ export class Compiler {
if (/{docsify-ignore}/g.test(str)) {
str = str.replace('{docsify-ignore}', '');
- nextToc.title = str;
+ nextToc.title = removeAtag(str);
nextToc.ignoreSubHeading = true;
}
@@ -228,7 +228,7 @@ export class Compiler {
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;
}
From ccc343ecc75f6cc4a2df6c0fb6a2862c3d75a131 Mon Sep 17 00:00:00 2001
From: Koy
Date: Wed, 14 Oct 2020 16:16:02 +0800
Subject: [PATCH 3/3] add test.
---
test/unit/render-util.test.js | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 test/unit/render-util.test.js
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');
+ });
+ });
+});