Skip to content

Commit

Permalink
Escaping html in search results (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored Mar 17, 2020
1 parent a4b5c8b commit 5c79267
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions source/js/local-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,12 @@ window.addEventListener('DOMContentLoaded', () => {
const input = document.querySelector('.search-input');
const resultContent = document.getElementById('search-result');

// Ref: https://github.com/ForbesLindesay/unescape-html
const unescapeHtml = html => {
return String(html)
.replace(/"/g, '"')
.replace(/'/g, '\'')
.replace(/:/g, ':')
// Replace all the other &#x; chars
.replace(/&#(\d+);/g, (m, p) => {
return String.fromCharCode(p);
})
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&');
};

const getIndexByWord = (word, text, caseSensitive) => {
if (CONFIG.localsearch.unescape) {
let div = document.createElement('div');
div.innerText = word;
word = div.innerHTML;
}
let wordLen = word.length;
if (wordLen === 0) return [];
let startPosition = 0;
Expand Down Expand Up @@ -238,9 +228,6 @@ window.addEventListener('DOMContentLoaded', () => {
datas = datas.filter(data => data.title).map(data => {
data.title = data.title.trim();
data.content = data.content ? data.content.trim().replace(/<[^>]+>/g, '') : '';
if (CONFIG.localsearch.unescape) {
data.content = unescapeHtml(data.content);
}
data.url = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
return data;
});
Expand Down

0 comments on commit 5c79267

Please sign in to comment.