astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
41 lines (40 loc) • 1.39 kB
JavaScript
import { fileURLToPath, pathToFileURL } from "node:url";
import { safeParseFrontmatter } from "../content/utils.js";
export const markdownContentEntryType = {
extensions: [".md"],
async getEntryInfo({ contents, fileUrl }) {
const parsed = safeParseFrontmatter(contents, fileURLToPath(fileUrl));
return {
data: parsed.frontmatter,
body: parsed.content.trim(),
slug: parsed.frontmatter.slug,
rawData: parsed.rawFrontmatter
};
},
// We need to handle propagation for Markdown because they support layouts which will bring in styles.
handlePropagation: true,
async getRenderFunction(config) {
const { markdown, image } = config;
const processor = await markdown.processor.createRenderer({
image,
syntaxHighlight: markdown.syntaxHighlight,
shikiConfig: markdown.shikiConfig,
gfm: markdown.gfm,
smartypants: markdown.smartypants
});
return async function renderToString(entry) {
// Process markdown even if it's empty as remark/rehype plugins may add content or frontmatter dynamically
const result = await processor.render(entry.body ?? "", {
frontmatter: entry.data,
fileURL: entry.filePath ? pathToFileURL(entry.filePath) : undefined
});
return {
html: result.code,
metadata: {
...result.metadata,
imagePaths: result.metadata.localImagePaths.concat(result.metadata.remoteImagePaths)
}
};
};
}
};