From fe1ab7381869768720b84f07290f70e759d12aae Mon Sep 17 00:00:00 2001 From: roottool Date: Sun, 16 Dec 2018 22:08:55 +0900 Subject: [PATCH 1/3] fix #2644 and #2662 --- browser/components/MarkdownPreview.js | 7 ++++- .../main/lib/dataApi/attachmentManagement.js | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 17d2cb82..7bfd8a10 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -625,11 +625,16 @@ export default class MarkdownPreview extends React.Component { indentSize, showCopyNotification, storagePath, - noteKey + noteKey, + sanitize } = this.props let { value, codeBlockTheme } = this.props this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme) + if (sanitize === 'NONE') { + const splitWithCodeTag = value.split('```') + value = attachmentManagement.escapeHtmlCharactersInCodeTag(splitWithCodeTag) + } const renderedHTML = this.markdown.render(value) attachmentManagement.migrateAttachments(value, storagePath, noteKey) this.refs.root.contentWindow.document.body.innerHTML = attachmentManagement.fixLocalURLS( diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 373efddc..43136bc0 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -7,6 +7,7 @@ const fse = require('fs-extra') const escapeStringRegexp = require('escape-string-regexp') const sander = require('sander') import i18n from 'browser/lib/i18n' +import { escapeHtmlCharacters } from '../../../lib/utils' const STORAGE_FOLDER_PLACEHOLDER = ':storage' const DESTINATION_FOLDER = 'attachments' @@ -220,6 +221,31 @@ function migrateAttachments (markdownContent, storagePath, noteKey) { } } +/** + * @description Convert special characters between ``` + * @param {string[]} splitWithCodeTag Array of HTML strings separated by ``` + * @returns {string} HTML in which special characters between ``` have been converted + */ +function escapeHtmlCharactersInCodeTag (splitWithCodeTag) { + for (let index = 0; index < splitWithCodeTag.length; index++) { + const codeTagRequired = (splitWithCodeTag[index] !== '\`\`\`' && index < splitWithCodeTag.length - 1) + if (codeTagRequired) { + splitWithCodeTag.splice((index + 1), 0, '\`\`\`') + } + } + let inCodeTag = false + let result = '' + for (let content of splitWithCodeTag) { + if (content === '\`\`\`') { + inCodeTag = !inCodeTag + } else if (inCodeTag) { + content = escapeHtmlCharacters(content) + } + result += content + } + return result +} + /** * @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files. * @param {String} renderedHTML HTML in that the links should be fixed @@ -574,6 +600,7 @@ function handleAttachmentLinkPaste (storageKey, noteKey, linkText) { module.exports = { copyAttachment, fixLocalURLS, + escapeHtmlCharactersInCodeTag, generateAttachmentMarkdown, handleAttachmentDrop, handlePastImageEvent, From 13857d43133c0429c2651fefe1ed9d53b188021a Mon Sep 17 00:00:00 2001 From: roottool Date: Mon, 17 Dec 2018 00:37:25 +0900 Subject: [PATCH 2/3] Modified position of escapeHtmlCharactersInCodeTag definition --- browser/main/lib/dataApi/attachmentManagement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 43136bc0..c0ada1f8 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -599,8 +599,8 @@ function handleAttachmentLinkPaste (storageKey, noteKey, linkText) { module.exports = { copyAttachment, - fixLocalURLS, escapeHtmlCharactersInCodeTag, + fixLocalURLS, generateAttachmentMarkdown, handleAttachmentDrop, handlePastImageEvent, From 071f7cb035c522ea5451362b24d5573a227f5bed Mon Sep 17 00:00:00 2001 From: tool root Date: Sun, 30 Dec 2018 16:20:19 +0900 Subject: [PATCH 3/3] rewrote the function code to MarkdownPreview.js #2644 #2662 --- browser/components/MarkdownPreview.js | 27 ++++++++++++++++++- .../main/lib/dataApi/attachmentManagement.js | 27 ------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 9f190773..4c638dc9 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -398,6 +398,31 @@ export default class MarkdownPreview extends React.Component { } } + /** + * @description Convert special characters between three ``` + * @param {string[]} splitWithCodeTag Array of HTML strings separated by three ``` + * @returns {string} HTML in which special characters between three ``` have been converted + */ + escapeHtmlCharactersInCodeTag (splitWithCodeTag) { + for (let index = 0; index < splitWithCodeTag.length; index++) { + const codeTagRequired = (splitWithCodeTag[index] !== '\`\`\`' && index < splitWithCodeTag.length - 1) + if (codeTagRequired) { + splitWithCodeTag.splice((index + 1), 0, '\`\`\`') + } + } + let inCodeTag = false + let result = '' + for (let content of splitWithCodeTag) { + if (content === '\`\`\`') { + inCodeTag = !inCodeTag + } else if (inCodeTag) { + content = escapeHtmlCharacters(content) + } + result += content + } + return result + } + getScrollBarStyle () { const { theme } = this.props @@ -640,7 +665,7 @@ export default class MarkdownPreview extends React.Component { this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme) if (sanitize === 'NONE') { const splitWithCodeTag = value.split('```') - value = attachmentManagement.escapeHtmlCharactersInCodeTag(splitWithCodeTag) + value = this.escapeHtmlCharactersInCodeTag(splitWithCodeTag) } const renderedHTML = this.markdown.render(value) attachmentManagement.migrateAttachments(value, storagePath, noteKey) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index d4cebded..6a0315f7 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -7,7 +7,6 @@ const fse = require('fs-extra') const escapeStringRegexp = require('escape-string-regexp') const sander = require('sander') import i18n from 'browser/lib/i18n' -import { escapeHtmlCharacters } from '../../../lib/utils' const STORAGE_FOLDER_PLACEHOLDER = ':storage' const DESTINATION_FOLDER = 'attachments' @@ -221,31 +220,6 @@ function migrateAttachments (markdownContent, storagePath, noteKey) { } } -/** - * @description Convert special characters between ``` - * @param {string[]} splitWithCodeTag Array of HTML strings separated by ``` - * @returns {string} HTML in which special characters between ``` have been converted - */ -function escapeHtmlCharactersInCodeTag (splitWithCodeTag) { - for (let index = 0; index < splitWithCodeTag.length; index++) { - const codeTagRequired = (splitWithCodeTag[index] !== '\`\`\`' && index < splitWithCodeTag.length - 1) - if (codeTagRequired) { - splitWithCodeTag.splice((index + 1), 0, '\`\`\`') - } - } - let inCodeTag = false - let result = '' - for (let content of splitWithCodeTag) { - if (content === '\`\`\`') { - inCodeTag = !inCodeTag - } else if (inCodeTag) { - content = escapeHtmlCharacters(content) - } - result += content - } - return result -} - /** * @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files. * @param {String} renderedHTML HTML in that the links should be fixed @@ -607,7 +581,6 @@ function handleAttachmentLinkPaste (storageKey, noteKey, linkText) { module.exports = { copyAttachment, - escapeHtmlCharactersInCodeTag, fixLocalURLS, generateAttachmentMarkdown, handleAttachmentDrop,