Created
April 24, 2017 19:05
-
-
Save saibotsivad/4c00bf03c553d83ae5ae8d724a3a966d to your computer and use it in GitHub Desktop.
remarkable link fixer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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