Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: title error when sidebar link exists with html tag #1404

Merged
merged 7 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/render/tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {
}

let innerHTML = '';
let title = '';
toc.forEach(node => {
innerHTML += `<li><a class="section-link" href="${node.slug}" title="${node.title}">${node.title}</a></li>`;
title = node.title.replace(/(<([^>]+)>)/g, '');
sy-records marked this conversation as resolved.
Show resolved Hide resolved
innerHTML += `<li><a class="section-link" href="${node.slug}" title="${title}">${node.title}</a></li>`;
if (node.children) {
innerHTML += tree(node.children, tpl);
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/render-util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { removeAtag } = require(`${SRC_PATH}/core/render/utils`);
const { tree } = require(`${SRC_PATH}/core/render/tpl`);

// Suite
// -----------------------------------------------------------------------------
Expand All @@ -13,3 +14,32 @@ describe('core/render/utils', () => {
});
});
});

describe('core/render/tpl', () => {
describe('remove html tag in tree', () => {
test('remove span and img', () => {
sy-records marked this conversation as resolved.
Show resolved Hide resolved
const result = tree([
{
level: 2,
slug: '#/cover?id=basic-usage',
title: '<span style="color:red">Basic usage</span>',
},
{
level: 2,
slug: '#/cover?id=custom-background',
title: 'Custom background',
},
{
level: 2,
slug: '#/cover?id=test',
title:
'<img src="/docs/_media/favicon.ico" data-origin="/_media/favicon.ico" alt="ico">Test',
},
]);

expect(result).toEqual(
`<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>`
);
});
});
});