mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-15 02:36:36 +00:00
improved escape function
This commit is contained in:
@@ -6,7 +6,7 @@ export function lastFindInArray (array, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeHtmlCharacters (html) {
|
export function escapeHtmlCharacters (html) {
|
||||||
const matchHtmlRegExp = /["'&<>]/g
|
const matchHtmlRegExp = /["'&<>]/g
|
||||||
const escapes = ['"', '&', ''', '<', '>']
|
const escapes = ['"', '&', ''', '<', '>']
|
||||||
let match = null
|
let match = null
|
||||||
@@ -15,8 +15,25 @@ function escapeHtmlCharacters (html) {
|
|||||||
replace +
|
replace +
|
||||||
str.substr(index + replace.length - (replace.length - 1))
|
str.substr(index + replace.length - (replace.length - 1))
|
||||||
|
|
||||||
|
// detecting code block
|
||||||
while ((match = matchHtmlRegExp.exec(html)) != null) {
|
while ((match = matchHtmlRegExp.exec(html)) != null) {
|
||||||
const current = { char: match[0], index: match.index }
|
const current = { char: match[0], index: match.index }
|
||||||
|
// position of the nearest line start
|
||||||
|
let previousLineEnd = current.index - 1
|
||||||
|
while (html[previousLineEnd] !== '\n' && previousLineEnd !== -1) {
|
||||||
|
previousLineEnd--
|
||||||
|
}
|
||||||
|
// 4 spaces means this character is in a code block
|
||||||
|
if (
|
||||||
|
html[previousLineEnd + 1] === ' ' &&
|
||||||
|
html[previousLineEnd + 2] === ' ' &&
|
||||||
|
html[previousLineEnd + 3] === ' ' &&
|
||||||
|
html[previousLineEnd + 4] === ' '
|
||||||
|
) {
|
||||||
|
// so skip it
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// otherwise, escape it !!!
|
||||||
if (current.char === '&') {
|
if (current.char === '&') {
|
||||||
let nextStr = ''
|
let nextStr = ''
|
||||||
let nextIndex = current.index
|
let nextIndex = current.index
|
||||||
|
|||||||
Reference in New Issue
Block a user