mirror of
https://github.com/m1ngsama/FUJI.git
synced 2025-12-25 02:56:38 +00:00
33 lines
1 KiB
TypeScript
33 lines
1 KiB
TypeScript
import path from "path"
|
|
import { type RehypePlugin } from "@astrojs/markdown-remark"
|
|
import { convertImageToBase64URL } from "../utils/image"
|
|
|
|
export const handleLocalCoverPlugin: RehypePlugin = () => {
|
|
return async (tree, file) => {
|
|
const filePath = file.history[0]
|
|
type AstroData = {
|
|
frontmatter: {
|
|
cover: { url: string } | string | undefined
|
|
}
|
|
}
|
|
const astroData = file.data.astro as AstroData
|
|
if (!astroData.frontmatter.cover) {
|
|
return
|
|
}
|
|
const coverUrl = typeof astroData.frontmatter.cover === "string" ? astroData.frontmatter.cover : astroData.frontmatter.cover.url
|
|
if (coverUrl.includes("http")) {
|
|
return
|
|
}
|
|
if (coverUrl.includes("base64")) {
|
|
return
|
|
}
|
|
const url = path.resolve(path.dirname(filePath), coverUrl)
|
|
const dataURL = await convertImageToBase64URL(url)
|
|
if (typeof astroData.frontmatter.cover === "string") {
|
|
astroData.frontmatter.cover = dataURL
|
|
}
|
|
else {
|
|
astroData.frontmatter.cover.url = dataURL
|
|
}
|
|
}
|
|
}
|