Skip to content

Instantly share code, notes, and snippets.

@saibotsivad
Created April 24, 2017 19:05
Show Gist options
  • Save saibotsivad/4c00bf03c553d83ae5ae8d724a3a966d to your computer and use it in GitHub Desktop.
Save saibotsivad/4c00bf03c553d83ae5ae8d724a3a966d to your computer and use it in GitHub Desktop.
remarkable link fixer
// with inspiration from: https://github.com/vitaliy-bobrov/remarkable-extlink
// for use with: https://github.com/jonschlinkert/remarkable
const md = new Remarkable()
function externalize(mark) {
const linkRegex = /href="([^"]*)"/
const fix = (link = '') => {
if (link.indexOf('http') !== 0) {
return 'http://' + link
} else {
return link
}
}
const originalRenderer = mark.renderer.rules.link_open
mark.renderer.rules.link_open = function() {
const result = originalRenderer.apply(null, arguments)
const linkParts = linkRegex.exec(result)
const link = linkParts[1]
return result
.replace(`href="${link}"`, `href="${fix(link)}"`)
.replace('>', ' target="_blank">')
}
}
md.use(externalize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment