1
0
Fork 0
freesewing/sites/shared/mdx/remark-github-images.mjs
2023-12-10 13:13:58 +01:00

29 lines
893 B
JavaScript

// __SDEFILE__ - This file is a dependency for the stand-alone environment
/*
* 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) {
if (node.type === 'image') node.url = convertUrl({ site, slug, url: node.url })
return node
})
return tree
}
}