Skip to content

Commit

Permalink
Merge branch 'master' into feature/add-smartquotes-toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
rayou committed Mar 7, 2018
2 parents e89c0a3 + 10f5ad6 commit c633ba9
Show file tree
Hide file tree
Showing 26 changed files with 741 additions and 108 deletions.
37 changes: 35 additions & 2 deletions browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import copyImage from 'browser/main/lib/dataApi/copyImage'
import { findStorage } from 'browser/lib/findStorage'
import fs from 'fs'
import eventEmitter from 'browser/main/lib/eventEmitter'
import iconv from 'iconv-lite'
const { ipcRenderer } = require('electron')

CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'

Expand All @@ -32,8 +34,13 @@ export default class CodeEditor extends React.Component {
constructor (props) {
super(props)

this.scrollHandler = _.debounce(this.handleScroll.bind(this), 100, {leading: false, trailing: true})
this.changeHandler = (e) => this.handleChange(e)
this.focusHandler = () => {
ipcRenderer.send('editor:focused', true)
}
this.blurHandler = (editor, e) => {
ipcRenderer.send('editor:focused', false)
if (e == null) return null
let el = e.relatedTarget
while (el != null) {
Expand Down Expand Up @@ -81,7 +88,6 @@ export default class CodeEditor extends React.Component {
}
}
})
this.scrollHandler = _.debounce(this.handleScroll.bind(this), 100, {leading: false, trailing: true})
}

componentDidMount () {
Expand Down Expand Up @@ -139,6 +145,7 @@ export default class CodeEditor extends React.Component {

this.setMode(this.props.mode)

this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.editor.on('change', this.changeHandler)
this.editor.on('paste', this.pasteHandler)
Expand All @@ -162,6 +169,7 @@ export default class CodeEditor extends React.Component {
}

componentWillUnmount () {
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
this.editor.off('change', this.changeHandler)
this.editor.off('paste', this.pasteHandler)
Expand Down Expand Up @@ -317,7 +325,7 @@ export default class CodeEditor extends React.Component {
fetch(pastedTxt, {
method: 'get'
}).then((response) => {
return (response.text())
return this.decodeResponse(response)
}).then((response) => {
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
const value = editor.getValue()
Expand All @@ -335,6 +343,31 @@ export default class CodeEditor extends React.Component {
})
}

decodeResponse (response) {
const headers = response.headers
const _charset = headers.has('content-type')
? this.extractContentTypeCharset(headers.get('content-type'))
: undefined
return response.arrayBuffer().then((buff) => {
return new Promise((resolve, reject) => {
try {
const charset = _charset !== undefined && iconv.encodingExists(_charset) ? _charset : 'utf-8'
resolve(iconv.decode(new Buffer(buff), charset).toString())
} catch (e) {
reject(e)
}
})
})
}

extractContentTypeCharset (contentType) {
return contentType.split(';').filter((str) => {
return str.trim().toLowerCase().startsWith('charset')
}).map((str) => {
return str.replace(/['"]/g, '').split('=')[1]
})[0]
}

render () {
const { className, fontSize } = this.props
let fontFamily = this.props.fontFamily
Expand Down
1 change: 1 addition & 0 deletions browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export default class MarkdownPreview extends React.Component {

return `<html>
<head>
<meta charset="UTF-8">
<style id="style">${inlineStyles}</style>
${styles}
</head>
Expand Down
6 changes: 5 additions & 1 deletion browser/components/NoteItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ NoteItem.propTypes = {
title: PropTypes.string.isrequired,
tags: PropTypes.array,
isStarred: PropTypes.bool.isRequired,
isTrashed: PropTypes.bool.isRequired
isTrashed: PropTypes.bool.isRequired,
blog: {
blogLink: PropTypes.string,
blogId: PropTypes.number
}
}),
handleNoteClick: PropTypes.func.isRequired,
handleNoteContextMenu: PropTypes.func.isRequired,
Expand Down
23 changes: 23 additions & 0 deletions browser/lib/markdown-it-sanitize-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

import sanitizeHtml from 'sanitize-html'

module.exports = function sanitizePlugin (md, options) {
options = options || {}

md.core.ruler.after('linkify', 'sanitize_inline', state => {
for (let tokenIdx = 0; tokenIdx < state.tokens.length; tokenIdx++) {
if (state.tokens[tokenIdx].type === 'html_block') {
state.tokens[tokenIdx].content = sanitizeHtml(state.tokens[tokenIdx].content, options)
}
if (state.tokens[tokenIdx].type === 'inline') {
const inlineTokens = state.tokens[tokenIdx].children
for (let childIdx = 0; childIdx < inlineTokens.length; childIdx++) {
if (inlineTokens[childIdx].type === 'html_inline') {
inlineTokens[childIdx].content = sanitizeHtml(inlineTokens[childIdx].content, options)
}
}
}
}
})
}
13 changes: 13 additions & 0 deletions browser/lib/markdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import markdownit from 'markdown-it'
import sanitize from './markdown-it-sanitize-html'
import emoji from 'markdown-it-emoji'
import math from '@rokt33r/markdown-it-math'
import _ from 'lodash'
Expand Down Expand Up @@ -51,6 +52,18 @@ class Markdown {

const updatedOptions = Object.assign(defaultOptions, options)
this.md = markdownit(updatedOptions)

// Sanitize use rinput before other plugins
this.md.use(sanitize, {
allowedTags: ['img', 'iframe'],
allowedAttributes: {
'*': ['alt', 'style'],
'img': ['src', 'width', 'height'],
'iframe': ['src', 'width', 'height', 'frameborder', 'allowfullscreen']
},
allowedIframeHostnames: ['www.youtube.com']
})

this.md.use(emoji, {
shortcuts: {}
})
Expand Down
3 changes: 3 additions & 0 deletions browser/main/Detail/SnippetNoteDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class SnippetNoteDetail extends React.Component {

handleKeyDown (e) {
switch (e.keyCode) {
// tab key
case 9:
if (e.ctrlKey && !e.shiftKey) {
e.preventDefault()
Expand All @@ -355,6 +356,7 @@ class SnippetNoteDetail extends React.Component {
this.focusEditor()
}
break
// L key
case 76:
{
const isSuper = global.process.platform === 'darwin'
Expand All @@ -366,6 +368,7 @@ class SnippetNoteDetail extends React.Component {
}
}
break
// T key
case 84:
{
const isSuper = global.process.platform === 'darwin'
Expand Down
Loading

0 comments on commit c633ba9

Please sign in to comment.