You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.4 KiB
113 lines
2.4 KiB
const { marked } = require('marked')
|
|
|
|
const configStore = require('./../config.js')
|
|
const Media = require('./../factories/media.js')
|
|
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
|
|
// copy from @marked/src/helpers.js, no export possible
|
|
function cleanUrl(sanitize, base, href) {
|
|
if (sanitize) {
|
|
|
|
let prot
|
|
|
|
try {
|
|
prot = decodeURIComponent(unescape(href))
|
|
.replace(nonWordAndColonTest, '')
|
|
.toLowerCase()
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
|
|
if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
if (base && !originIndependentUrl.test(href)) {
|
|
href = resolveUrl(base, href)
|
|
}
|
|
|
|
try {
|
|
href = encodeURI(href).replace(/%25/g, '%')
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
|
|
return href
|
|
}
|
|
|
|
const renderer = {
|
|
|
|
/**
|
|
*
|
|
* @param {string} href
|
|
* @param {string} title
|
|
* @param {string} text
|
|
*/
|
|
link(href, title, text) {
|
|
|
|
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href)
|
|
|
|
if (href === null) {
|
|
return text
|
|
}
|
|
|
|
let out = '<a href="' + href + '"'
|
|
|
|
if (title) {
|
|
out += ' title="' + title + '"'
|
|
}
|
|
|
|
// check if url is external and add target
|
|
if (href.match(/^(http|https):\/\//)) {
|
|
out += ' target="_blank" rel="noopener" '
|
|
}
|
|
|
|
out += '>' + text + '</a>'
|
|
|
|
return out
|
|
},
|
|
|
|
/**
|
|
*
|
|
* @param {string} href
|
|
* @param {string} title
|
|
* @param {string} text
|
|
*/
|
|
image(href, title, text) {
|
|
|
|
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href)
|
|
|
|
if (href === null) {
|
|
return text
|
|
}
|
|
|
|
// check if href for image is relative
|
|
if (!href.match(/^(http|https):\/\//)) {
|
|
const markedDirPath = configStore.get('markedDirPath')
|
|
|
|
// check if dirPath are exists from options
|
|
if (markedDirPath || markedDirPath === '') {
|
|
const media = new Media(markedDirPath)
|
|
href = media.resolve(href)
|
|
}
|
|
}
|
|
|
|
let out = `<img src="${href}" alt="${text}"`
|
|
|
|
if (title) {
|
|
out += ` title="${title}"`
|
|
}
|
|
|
|
out += this.options.xhtml ? '/>' : '>'
|
|
|
|
return out
|
|
}
|
|
}
|
|
|
|
module.exports = renderer
|