Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/render/tpl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { escapeHtml } from './utils';

/**
* Render github corner
* @param {Object} data URL for the View Source on Github link
Expand Down Expand Up @@ -91,7 +93,9 @@ export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {

let innerHTML = '';
toc.forEach(node => {
innerHTML += `<li><a class="section-link" href="${node.slug}" title="${node.title}">${node.title}</a></li>`;
innerHTML += `<li><a class="section-link" href="${
node.slug
}" title="${escapeHtml(node.title)}">${node.title}</a></li>`;
if (node.children) {
innerHTML += tree(node.children, tpl);
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,20 @@ export function getAndRemoveConfig(str = '') {
export function removeAtag(str = '') {
return str.replace(/(<\/?a.*?>)/gi, '');
}

/**
* Escape html
* @param {String} string html string
* @returns {string} Return escaped html string
*/
export function escapeHtml(string) {
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
};

return String(string).replace(/[&<>"']/g, s => entityMap[s]);
}
14 changes: 1 addition & 13 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-unused-vars */
import { getAndRemoveConfig } from '../../core/render/utils';
import { getAndRemoveConfig, escapeHtml } from '../../core/render/utils';

let INDEXS = {};

Expand All @@ -20,18 +20,6 @@ function resolveIndexKey(namespace) {
: LOCAL_STORAGE.INDEX_KEY;
}

function escapeHtml(string) {
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
};

return String(string).replace(/[&<>"']/g, s => entityMap[s]);
}

function getAllPaths(router) {
const paths = [];

Expand Down
14 changes: 13 additions & 1 deletion test/unit/render-util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { removeAtag } = require(`${SRC_PATH}/core/render/utils`);
const { removeAtag, escapeHtml } = require(`${SRC_PATH}/core/render/utils`);

// Suite
// -----------------------------------------------------------------------------
Expand All @@ -12,4 +12,16 @@ describe('core/render/utils', () => {
expect(result).toEqual('content');
});
});

// escapeHtml()
// ---------------------------------------------------------------------------
describe('escapeHtml()', () => {
test('escape html', () => {
const result = escapeHtml('<a href="www.example.com">content</a>');

expect(result).toEqual(
'&lt;a href=&quot;www.example.com&quot;&gt;content&lt;/a&gt;'
);
});
});
});