1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

allow detect code block or not in escapeHtml function

This commit is contained in:
Nguyễn Việt Hưng
2018-07-06 23:45:18 +07:00
parent 9cc7b8bcc6
commit bc640834cd
2 changed files with 18 additions and 16 deletions

View File

@@ -219,7 +219,7 @@ export default class MarkdownPreview extends React.Component {
const {fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme, scrollPastEnd, theme, allowCustomCSS, customCSS} = this.getStyleParams() const {fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme, scrollPastEnd, theme, allowCustomCSS, customCSS} = this.getStyleParams()
const inlineStyles = buildStyle(fontFamily, fontSize, codeBlockFontFamily, lineNumber, scrollPastEnd, theme, allowCustomCSS, customCSS) 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 files = [this.GetCodeThemeLink(codeBlockTheme), ...CSS_FILES]
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(noteContent, this.props.storagePath) const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(noteContent, this.props.storagePath)

View File

@@ -6,7 +6,7 @@ export function lastFindInArray (array, callback) {
} }
} }
export function escapeHtmlCharacters (html) { export function escapeHtmlCharacters (html, opt = { detectCodeBlock: false }) {
const matchHtmlRegExp = /["'&<>]/g const matchHtmlRegExp = /["'&<>]/g
const escapes = ['&quot;', '&amp;', '&#39;', '&lt;', '&gt;'] const escapes = ['&quot;', '&amp;', '&#39;', '&lt;', '&gt;']
let match = null let match = null
@@ -18,6 +18,7 @@ export function escapeHtmlCharacters (html) {
// detecting code block // 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 }
if (opt.detectCodeBlock) {
// position of the nearest line start // position of the nearest line start
let previousLineEnd = current.index - 1 let previousLineEnd = current.index - 1
while (html[previousLineEnd] !== '\n' && previousLineEnd !== -1) { while (html[previousLineEnd] !== '\n' && previousLineEnd !== -1) {
@@ -33,6 +34,7 @@ export function escapeHtmlCharacters (html) {
// so skip it // so skip it
continue continue
} }
}
// otherwise, escape it !!! // otherwise, escape it !!!
if (current.char === '&') { if (current.char === '&') {
let nextStr = '' let nextStr = ''