Skip to content

Commit 8ccc202

Browse files
authoredNov 22, 2020
fix: title error when sidebar link exists with html tag (#1404)
* fix: title error when sidebar link exists with image * fix #1408 * add test * Update
1 parent 0806f48 commit 8ccc202

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎src/core/render/tpl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {
9191

9292
let innerHTML = '';
9393
toc.forEach(node => {
94-
innerHTML += `<li><a class="section-link" href="${node.slug}" title="${node.title}">${node.title}</a></li>`;
94+
const title = node.title.replace(/(<([^>]+)>)/g, '');
95+
innerHTML += `<li><a class="section-link" href="${node.slug}" title="${title}">${node.title}</a></li>`;
9596
if (node.children) {
9697
innerHTML += tree(node.children, tpl);
9798
}

‎test/unit/render-util.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { removeAtag } = require(`${SRC_PATH}/core/render/utils`);
2+
const { tree } = require(`${SRC_PATH}/core/render/tpl`);
23

34
// Suite
45
// -----------------------------------------------------------------------------
@@ -13,3 +14,30 @@ describe('core/render/utils', () => {
1314
});
1415
});
1516
});
17+
18+
describe('core/render/tpl', () => {
19+
test('remove html tag in tree', () => {
20+
const result = tree([
21+
{
22+
level: 2,
23+
slug: '#/cover?id=basic-usage',
24+
title: '<span style="color:red">Basic usage</span>',
25+
},
26+
{
27+
level: 2,
28+
slug: '#/cover?id=custom-background',
29+
title: 'Custom background',
30+
},
31+
{
32+
level: 2,
33+
slug: '#/cover?id=test',
34+
title:
35+
'<img src="/docs/_media/favicon.ico" data-origin="/_media/favicon.ico" alt="ico">Test',
36+
},
37+
]);
38+
39+
expect(result).toEqual(
40+
`<ul class="app-sub-sidebar"><li><a class="section-link" href="#/cover?id=basic-usage" title="Basic usage"><span style="color:red">Basic usage</span></a></li><li><a class="section-link" href="#/cover?id=custom-background" title="Custom background">Custom background</a></li><li><a class="section-link" href="#/cover?id=test" title="Test"><img src="/docs/_media/favicon.ico" data-origin="/_media/favicon.ico" alt="ico">Test</a></li></ul>`
41+
);
42+
});
43+
});