Skip to content

Commit

Permalink
fix: the sidebar links to another site. (#1336)
Browse files Browse the repository at this point in the history
* fix : the sidebar links to another site
  • Loading branch information
Koooooo-7 authored Oct 15, 2020
1 parent 25bc9b7 commit c9d4f7a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 (/<!-- {docsify-ignore} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore} -->', '');
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 (/<!-- {docsify-ignore-all} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore-all} -->', '');
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;
}

Expand Down
12 changes: 6 additions & 6 deletions src/core/render/compiler/headline.js
Original file line number Diff line number Diff line change
@@ -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 (/<!-- {docsify-ignore} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore} -->', '');
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 (/<!-- {docsify-ignore-all} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore-all} -->', '');
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;
}

Expand Down
10 changes: 10 additions & 0 deletions src/core/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ export function getAndRemoveConfig(str = '') {

return { str, config };
}

/**
* Remove the <a> 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 <a> element.
*/
export function removeAtag(str = '') {
return str.replace(/(<\/?a.*?>)/gi, '');
}
15 changes: 15 additions & 0 deletions test/unit/render-util.test.js
Original file line number Diff line number Diff line change
@@ -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('<a href="www.example.com">content</a>');

expect(result).toEqual('content');
});
});
});

1 comment on commit c9d4f7a

@vercel
Copy link

@vercel vercel bot commented on c9d4f7a Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.