|
| 1 | +import path from 'node:path' |
| 2 | +import { visit } from 'unist-util-visit' |
| 3 | +import type { Plugin } from 'unified' |
| 4 | +import type { Image, Html } from 'mdast' |
| 5 | +import type { Element } from 'hast' |
| 6 | +import type { MdxJsxFlowElement, MdxJsxTextElement, MdxJsxAttribute } from 'mdast-util-mdx-jsx' |
| 7 | + |
| 8 | +function resolveUrl(src: string, dir: string): string { |
| 9 | + if (/^[a-z][a-z0-9+\-.]*:/i.test(src)) return src |
| 10 | + if (src.startsWith('//')) return src |
| 11 | + if (src.startsWith('#')) return src |
| 12 | + if (src.startsWith('/_content/')) return src |
| 13 | + |
| 14 | + if (src.startsWith('/')) return `/_content${src}` |
| 15 | + return `/_content/${path.posix.normalize(path.posix.join(dir, src))}` |
| 16 | +} |
| 17 | + |
| 18 | +const remarkResolveImages: Plugin = () => { |
| 19 | + return (tree, file) => { |
| 20 | + const filePath = file.path |
| 21 | + if (!filePath) return |
| 22 | + |
| 23 | + const contentIdx = filePath.lastIndexOf('/content/') |
| 24 | + if (contentIdx === -1) return |
| 25 | + |
| 26 | + const relative = filePath.slice(contentIdx + '/content/'.length) |
| 27 | + const dir = path.posix.dirname(relative) |
| 28 | + |
| 29 | + visit(tree, 'image', (node: Image) => { |
| 30 | + if (!node.url) return |
| 31 | + node.url = resolveUrl(node.url, dir) |
| 32 | + }) |
| 33 | + |
| 34 | + visit(tree, 'html', (node: Html) => { |
| 35 | + node.value = node.value.replace( |
| 36 | + /(<img\b[^>]*\bsrc=["'])([^"']+)(["'])/gi, |
| 37 | + (_, before, src, after) => `${before}${resolveUrl(src, dir)}${after}` |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + visit(tree, (node) => { |
| 42 | + if (node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxJsxTextElement') return |
| 43 | + const jsx = node as MdxJsxFlowElement | MdxJsxTextElement |
| 44 | + if (jsx.name !== 'img') return |
| 45 | + const srcAttr = jsx.attributes.find((a): a is MdxJsxAttribute => a.type === 'mdxJsxAttribute' && a.name === 'src') |
| 46 | + if (!srcAttr?.value || typeof srcAttr.value !== 'string') return |
| 47 | + srcAttr.value = resolveUrl(srcAttr.value, dir) |
| 48 | + }) |
| 49 | + |
| 50 | + visit(tree, 'element', (node: Element) => { |
| 51 | + if (node.tagName !== 'img') return |
| 52 | + const src = node.properties?.src |
| 53 | + if (typeof src !== 'string') return |
| 54 | + node.properties.src = resolveUrl(src, dir) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export default remarkResolveImages |
0 commit comments