Home / Function/ markdown() — astro Function Reference

markdown() — astro Function Reference

Architecture documentation for the markdown() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  f89ea60a_7636_5e89_5468_8dcdd2b8a480["markdown()"]
  3645ae03_e174_ac69_61f8_dccc7510bafb["index.ts"]
  f89ea60a_7636_5e89_5468_8dcdd2b8a480 -->|defined in| 3645ae03_e174_ac69_61f8_dccc7510bafb
  style f89ea60a_7636_5e89_5468_8dcdd2b8a480 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-markdown/index.ts lines 34–194

export default function markdown({ settings, logger }: AstroPluginOptions): Plugin {
	let processor: Promise<MarkdownProcessor> | undefined;

	return {
		enforce: 'pre',
		name: 'astro:markdown',
		buildEnd() {
			processor = undefined;
		},
		resolveId: {
			filter: {
				// Do not match sources that start with /
				id: /^[^/]/,
			},
			async handler(source, importer, options) {
				if (importer?.endsWith('.md')) {
					let resolved = await this.resolve(source, importer, options);
					if (!resolved) resolved = await this.resolve('./' + source, importer, options);
					return resolved;
				}
			},
		},
		// Why not the "transform" hook instead of "load" + readFile?
		// A: Vite transforms all "import.meta.env" references to their values before
		// passing to the transform hook. This lets us get the truly raw value
		// to escape "import.meta.env" ourselves.
		load: {
			filter: {
				id: {
					// Matches .md, .markdown, .md?, .markdown? etc
					include: new RegExp(
						`\\.(${SUPPORTED_MARKDOWN_FILE_EXTENSIONS.map((ext) => ext.slice(1)).join('|')})(\\?|$)`,
					),
					exclude: specialQueriesRE,
				},
			},
			async handler(id) {
				// The id filter also matches file extensions in search params which we do not want.
				// So we check another time, but at least it will run for less ids
				if (!isMarkdownFile(id)) {
					return;
				}
				const { fileId, fileUrl } = getFileInfo(id, settings.config);
				const rawFile = await fs.promises.readFile(fileId, 'utf-8');
				const raw = safeParseFrontmatter(rawFile, id);

				const fileURL = pathToFileURL(fileId);

				// Lazily initialize the Markdown processor
				if (!processor) {
					processor = createMarkdownProcessor({
						image: settings.config.image,
						...settings.config.markdown,
					});
				}

				const renderResult = await (await processor).render(raw.content, {
					fileURL,
					frontmatter: raw.frontmatter,
				});

				// Improve error message for invalid astro frontmatter
				if (!isFrontmatterValid(renderResult.metadata.frontmatter)) {
					throw new AstroError(AstroErrorData.InvalidFrontmatterInjectionError);
				}

				let html = renderResult.code;
				const {
					headings,
					localImagePaths: rawLocalImagePaths,
					remoteImagePaths,
					frontmatter,
				} = renderResult.metadata;

				// Add default charset for markdown pages
				const isMarkdownPage = isPage(fileURL, settings);
				const charset = isMarkdownPage ? '<meta charset="utf-8">' : '';

				// Resolve all the extracted images from the content
				const localImagePaths: MarkdownImagePath[] = [];
				for (const imagePath of rawLocalImagePaths) {

Domain

Subdomains

Frequently Asked Questions

What does markdown() do?
markdown() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-markdown/index.ts.
Where is markdown() defined?
markdown() is defined in packages/astro/src/vite-plugin-markdown/index.ts at line 34.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free