From bc640834cdc5d7eec8dc679d5c99aa960fc06c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Vi=E1=BB=87t=20H=C6=B0ng?= Date: Fri, 6 Jul 2018 23:45:18 +0700 Subject: [PATCH] allow detect code block or not in escapeHtml function --- browser/components/MarkdownPreview.js | 2 +- browser/lib/utils.js | 32 ++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 31d7e631..c3d3a730 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -219,7 +219,7 @@ export default class MarkdownPreview extends React.Component { const {fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme, scrollPastEnd, theme, allowCustomCSS, customCSS} = this.getStyleParams() const inlineStyles = buildStyle(fontFamily, fontSize, codeBlockFontFamily, lineNumber, scrollPastEnd, theme, allowCustomCSS, customCSS) - let body = this.markdown.render(escapeHtmlCharacters(noteContent)) + let body = this.markdown.render(escapeHtmlCharacters(noteContent, { detectCodeBlock: true })) const files = [this.GetCodeThemeLink(codeBlockTheme), ...CSS_FILES] const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(noteContent, this.props.storagePath) diff --git a/browser/lib/utils.js b/browser/lib/utils.js index 6fc6ae27..564ed3d2 100644 --- a/browser/lib/utils.js +++ b/browser/lib/utils.js @@ -6,7 +6,7 @@ export function lastFindInArray (array, callback) { } } -export function escapeHtmlCharacters (html) { +export function escapeHtmlCharacters (html, opt = { detectCodeBlock: false }) { const matchHtmlRegExp = /["'&<>]/g const escapes = ['"', '&', ''', '<', '>'] let match = null @@ -18,20 +18,22 @@ export function escapeHtmlCharacters (html) { // detecting code block while ((match = matchHtmlRegExp.exec(html)) != null) { 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 + if (opt.detectCodeBlock) { + // 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 === '&') {