Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ function getSourceLineNumberByElement(element) {
return parent.dataset.line !== undefined ? parseInt(parent.dataset.line) : -1
}

function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
}

function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace)
}

class MarkdownPreview extends React.Component {
constructor(props) {
super(props)
Expand Down Expand Up @@ -496,7 +504,13 @@ class MarkdownPreview extends React.Component {
let syntax = CodeMirror.findModeByName(convertModeName(el.className))
if (syntax == null) syntax = CodeMirror.findModeByName('Plain Text')
CodeMirror.requireMode(syntax.mode, () => {
const content = htmlTextHelper.decodeEntities(el.innerHTML)
let content = el.innerHTML
content = replaceAll(content, ''', "'")
content = replaceAll(content, '&lt;', '<')
content = replaceAll(content, '&gt;', '>')
content = replaceAll(content, '&#63;', '?')
content = replaceAll(content, '&#36;', '$')
content = replaceAll(content, '&amp;', '&')
const copyIcon = document.createElement('i')
copyIcon.innerHTML =
'<button class="clipboardButton"><svg width="13" height="13" viewBox="0 0 1792 1792" ><path d="M768 1664h896v-640h-416q-40 0-68-28t-28-68v-416h-384v1152zm256-1440v-64q0-13-9.5-22.5t-22.5-9.5h-704q-13 0-22.5 9.5t-9.5 22.5v64q0 13 9.5 22.5t22.5 9.5h704q13 0 22.5-9.5t9.5-22.5zm256 672h299l-299-299v299zm512 128v672q0 40-28 68t-68 28h-960q-40 0-68-28t-28-68v-160h-544q-40 0-68-28t-28-68v-1344q0-40 28-68t68-28h1088q40 0 68 28t28 68v328q21 13 36 28l408 408q28 28 48 76t20 88z"/></svg></button>'
Expand Down
9 changes: 5 additions & 4 deletions browser/lib/markdown-it-sanitize-html.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

import sanitizeHtml from 'sanitize-html'
import { escapeHtmlCharacters } from './utils'
import url from 'url'

module.exports = function sanitizePlugin(md, options) {
Expand All @@ -17,9 +16,11 @@ module.exports = function sanitizePlugin(md, options) {
}
if (state.tokens[tokenIdx].type.match(/.*_fence$/)) {
// escapeHtmlCharacters has better performance
state.tokens[tokenIdx].content = escapeHtmlCharacters(
state.tokens[tokenIdx].content,
{ skipSingleQuote: true }
state.tokens[tokenIdx].content = state.tokens[tokenIdx].content.replace(
/[\u00A0-\u9999<>\&]/gim,
function(i) {
return '&#' + i.charCodeAt(0) + ';'
}
)
}
if (state.tokens[tokenIdx].type === 'inline') {
Expand Down