Skip to content

Commit fe1ab73

Browse files
committed
fix #2644 and #2662
1 parent 743c979 commit fe1ab73

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

browser/components/MarkdownPreview.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,16 @@ export default class MarkdownPreview extends React.Component {
625625
indentSize,
626626
showCopyNotification,
627627
storagePath,
628-
noteKey
628+
noteKey,
629+
sanitize
629630
} = this.props
630631
let { value, codeBlockTheme } = this.props
631632

632633
this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme)
634+
if (sanitize === 'NONE') {
635+
const splitWithCodeTag = value.split('```')
636+
value = attachmentManagement.escapeHtmlCharactersInCodeTag(splitWithCodeTag)
637+
}
633638
const renderedHTML = this.markdown.render(value)
634639
attachmentManagement.migrateAttachments(value, storagePath, noteKey)
635640
this.refs.root.contentWindow.document.body.innerHTML = attachmentManagement.fixLocalURLS(

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fse = require('fs-extra')
77
const escapeStringRegexp = require('escape-string-regexp')
88
const sander = require('sander')
99
import i18n from 'browser/lib/i18n'
10+
import { escapeHtmlCharacters } from '../../../lib/utils'
1011

1112
const STORAGE_FOLDER_PLACEHOLDER = ':storage'
1213
const DESTINATION_FOLDER = 'attachments'
@@ -220,6 +221,31 @@ function migrateAttachments (markdownContent, storagePath, noteKey) {
220221
}
221222
}
222223

224+
/**
225+
* @description Convert special characters between ```
226+
* @param {string[]} splitWithCodeTag Array of HTML strings separated by ```
227+
* @returns {string} HTML in which special characters between ``` have been converted
228+
*/
229+
function escapeHtmlCharactersInCodeTag (splitWithCodeTag) {
230+
for (let index = 0; index < splitWithCodeTag.length; index++) {
231+
const codeTagRequired = (splitWithCodeTag[index] !== '\`\`\`' && index < splitWithCodeTag.length - 1)
232+
if (codeTagRequired) {
233+
splitWithCodeTag.splice((index + 1), 0, '\`\`\`')
234+
}
235+
}
236+
let inCodeTag = false
237+
let result = ''
238+
for (let content of splitWithCodeTag) {
239+
if (content === '\`\`\`') {
240+
inCodeTag = !inCodeTag
241+
} else if (inCodeTag) {
242+
content = escapeHtmlCharacters(content)
243+
}
244+
result += content
245+
}
246+
return result
247+
}
248+
223249
/**
224250
* @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files.
225251
* @param {String} renderedHTML HTML in that the links should be fixed
@@ -574,6 +600,7 @@ function handleAttachmentLinkPaste (storageKey, noteKey, linkText) {
574600
module.exports = {
575601
copyAttachment,
576602
fixLocalURLS,
603+
escapeHtmlCharactersInCodeTag,
577604
generateAttachmentMarkdown,
578605
handleAttachmentDrop,
579606
handlePastImageEvent,

0 commit comments

Comments
 (0)