Rewrites absolute URLs to relative ones with remark
yarn add remark-relative-links
const html = require('remark-html');
const remark = require('remark');
const relativeLinks = require('remark-relative-links');
remark()
.use(relativeLinks, {
domainRegex: /http[s]*:\/\/[www.]*yoursite\.com[/]?/,
})
.use(html)
.process('[Blog](https://yoursite.com/blog/article/)', (err, file) => {
if (err) throw err
console.log(String(file))
});
<p><a href="/blog/article/">Blog</a></p>
Add target and rel attributes to external links.
Regex used to decipher what domain to "relative-ize". The example provided should handle most cases.