Skip to content

Fix incorrect tag completion in code block #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 29, 2017
Merged
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
29 changes: 11 additions & 18 deletions browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,7 @@ import flowchart from 'flowchart'
import SequenceDiagram from 'js-sequence-diagrams'
import eventEmitter from 'browser/main/lib/eventEmitter'
import fs from 'fs'

function decodeHTMLEntities (text) {
var entities = [
['apos', '\''],
['amp', '&'],
['lt', '<'],
['gt', '>']
]

for (var i = 0, max = entities.length; i < max; ++i) {
text = text.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1])
}

return text
}
import htmlTextHelper from 'browser/lib/htmlTextHelper'

const { remote } = require('electron')
const { app } = remote
Expand Down Expand Up @@ -241,6 +227,13 @@ export default class MarkdownPreview extends React.Component {
let { value, theme, indentSize, codeBlockTheme } = this.props

this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme)

const codeBlocks = value.match(/(```)(.|[\n])*?(```)/g)
if (codeBlocks !== null) {
codeBlocks.forEach((codeBlock) => {
value = value.replace(codeBlock, htmlTextHelper.encodeEntities(codeBlock))
})
}
this.refs.root.contentWindow.document.body.innerHTML = markdown.render(value)

_.forEach(this.refs.root.contentWindow.document.querySelectorAll('.taskListItem'), (el) => {
Expand All @@ -263,7 +256,7 @@ export default class MarkdownPreview extends React.Component {
let syntax = CodeMirror.findModeByName(el.className)
if (syntax == null) syntax = CodeMirror.findModeByName('Plain Text')
CodeMirror.requireMode(syntax.mode, () => {
let content = decodeHTMLEntities(el.innerHTML)
let content = htmlTextHelper.decodeEntities(el.innerHTML)
el.innerHTML = ''
el.parentNode.className += ` cm-s-${codeBlockTheme} CodeMirror`
CodeMirror.runMode(content, syntax.mime, el, {
Expand All @@ -281,7 +274,7 @@ export default class MarkdownPreview extends React.Component {
_.forEach(this.refs.root.contentWindow.document.querySelectorAll('.flowchart'), (el) => {
Raphael.setWindow(this.getWindow())
try {
let diagram = flowchart.parse(decodeHTMLEntities(el.innerHTML))
let diagram = flowchart.parse(htmlTextHelper.decodeEntities(el.innerHTML))
el.innerHTML = ''
diagram.drawSVG(el, opts)
_.forEach(el.querySelectorAll('a'), (el) => {
Expand All @@ -297,7 +290,7 @@ export default class MarkdownPreview extends React.Component {
_.forEach(this.refs.root.contentWindow.document.querySelectorAll('.sequence'), (el) => {
Raphael.setWindow(this.getWindow())
try {
let diagram = SequenceDiagram.parse(decodeHTMLEntities(el.innerHTML))
let diagram = SequenceDiagram.parse(htmlTextHelper.decodeEntities(el.innerHTML))
el.innerHTML = ''
diagram.drawSVG(el, {theme: 'simple'})
_.forEach(el.querySelectorAll('a'), (el) => {
Expand Down
43 changes: 43 additions & 0 deletions browser/lib/htmlTextHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @fileoverview Text trimmer for html.
*/

/**
* @param {string} text
* @return {string}
*/

export function decodeEntities (text) {
var entities = [
['apos', '\''],
['amp', '&'],
['lt', '<'],
['gt', '>'],
['#63', '\\?']
]

for (var i = 0, max = entities.length; i < max; ++i) {
text = text.replace(new RegExp(`&${entities[i][0]};`, 'g'), entities[i][1])
}

return text
}

export function encodeEntities (text) {
const entities = [
['\'', 'apos'],
['<', 'lt'],
['>', 'gt'],
['\\?', '#63']
]

entities.forEach((entity) => {
text = text.replace(new RegExp(entity[0], 'g'), `&${entity[1]};`)
})
return text
}

export default {
decodeEntities,
encodeEntities
}
52 changes: 52 additions & 0 deletions tests/lib/html-text-helper-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @fileoverview Unit test for browser/lib/htmlTextHelper
*/
const test = require('ava')
const htmlTextHelper = require('browser/lib/htmlTextHelper')

// Unit test
test('htmlTextHelper#decodeEntities should return encoded text (string)', t => {
// [input, expected]
const testCases = [
['&lt;a href=', '<a href='],
['var test = &apos;test&apos;', 'var test = \'test\''],
['&lt;a href=&apos;https://boostnote.io&apos;&gt;Boostnote', '<a href=\'https://boostnote.io\'>Boostnote'],
['&lt;\\\\?php\n var = &apos;hoge&apos;;', '<\\\\?php\n var = \'hoge\';'],
['&amp;', '&']
]

testCases.forEach(testCase => {
const [input, expected] = testCase
t.is(htmlTextHelper.decodeEntities(input), expected, `Test for decodeEntities() input: ${input} expected: ${expected}`)
})
})

test('htmlTextHelper#decodeEntities() should return decoded text (string)', t => {
// [input, expected]
const testCases = [
['<a href=', '&lt;a href='],
['var test = \'test\'', 'var test = &apos;test&apos;'],
['<a href=\'https://boostnote.io\'>Boostnote', '&lt;a href=&apos;https://boostnote.io&apos;&gt;Boostnote'],
['<?php\n var = \'hoge\';', '&lt;&#63;php\n var = &apos;hoge&apos;;']
]

testCases.forEach(testCase => {
const [input, expected] = testCase
t.is(htmlTextHelper.encodeEntities(input), expected, `Test for encodeEntities() input: ${input} expected: ${expected}`)
})
})

// Integration test
test(t => {
const testCases = [
'var test = \'test\'',
'<a href=\'https://boostnote.io\'>Boostnote',
'<Component styleName=\'test\' />'
]

testCases.forEach(testCase => {
const encodedText = htmlTextHelper.encodeEntities(testCase)
const decodedText = htmlTextHelper.decodeEntities(encodedText)
t.is(decodedText, testCase, 'Integration test through encodedText() and decodedText()')
})
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are good JavaScript Engineer 👍