1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

add href, id, name transform for secure navigation

This commit is contained in:
Rokt33r
2016-01-07 09:14:30 +09:00
parent 6698d15f20
commit c1b56e4cb6
5 changed files with 22 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ import markdown from '../lib/markdown'
import ReactDOM from 'react-dom'
import sanitizeHtml from '@rokt33r/sanitize-html'
import hljs from 'highlight.js'
import _ from 'lodash'
const electron = require('electron')
const shell = electron.shell
@@ -21,14 +22,29 @@ const sanitizeOpts = {
},
allowedAttributes: {
a: ['href', 'data-key'],
img: [ 'src' ]
img: [ 'src' ],
'*': ['id', 'name']
},
transformTags: {
'*': function (tagName, attribs) {
let href = attribs.href
if (_.isString(href) && href.match(/^#.+$/)) attribs.href = href.replace(/^#/, '#md-anchor-')
if (attribs.id) attribs.id = 'md-anchor-' + attribs.id
if (attribs.name) attribs.name = 'md-anchor-' + attribs.name
return {
tagName: tagName,
attribs: attribs
}
}
}
}
function handleAnchorClick (e) {
if (e.target.attributes.href && e.target.attributes.href.nodeValue.match(/#.+/)) {
return
}
e.preventDefault()
e.stopPropagation()
console.log(e.target.href)
shell.openExternal(e.target.href)
}