1
0
Fork 0
freesewing/sites/shared/mdx/remark-github-images.mjs
2023-10-09 11:42:16 +02:00

28 lines
832 B
JavaScript

/*
* This is a remark plugin that will update the src of local images to
* load them from Github. It is used when we load markdown/mdx dynamically
* from Github rather than from disk.
*/
import { visit } from 'unist-util-visit'
export const ghPrefix = 'https://raw.githubusercontent.com/freesewing/freesewing/develop/markdown'
const convertUrl = ({ site, slug, url }) => {
if (url.slice(0, 1) === 'http://') return url
if (url.slice(0, 7) === 'http://') return url
if (url.slice(0, 8) === 'https://') return url
return `${ghPrefix}/${site}/${slug}/${url}`
}
export function remarkGithubImages({ site, slug }) {
return (tree) => {
visit(tree, function (node, index, parent) {
if (node.type === 'image') node.url = convertUrl({ site, slug, url: node.url })
return node
})
return tree
}
}