Astro's internal markdown processing library that provides a unified markdown processor built on remark and rehype ecosystem plugins with syntax highlighting, frontmatter parsing, and image processing capabilities.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive frontmatter parsing functionality with support for YAML and TOML formats, including validation and flexible content handling modes.
Parses frontmatter from markdown content with configurable handling of the frontmatter section.
/**
* Parse frontmatter (YAML or TOML) from markdown content
* @param code - The markdown content containing frontmatter
* @param options - Optional configuration for frontmatter handling
* @returns Parsed frontmatter data, raw frontmatter, and processed content
*/
function parseFrontmatter(
code: string,
options?: ParseFrontmatterOptions
): ParseFrontmatterResult;
interface ParseFrontmatterOptions {
/**
* How the frontmatter should be handled in the returned content string
* - 'preserve': Keep the frontmatter
* - 'remove': Remove the frontmatter
* - 'empty-with-spaces': Replace with spaces (preserves sourcemap line/col/offset)
* - 'empty-with-lines': Replace with line breaks (preserves sourcemap line/col)
*/
frontmatter: 'preserve' | 'remove' | 'empty-with-spaces' | 'empty-with-lines';
}
interface ParseFrontmatterResult {
/** Parsed frontmatter object */
frontmatter: Record<string, any>;
/** Raw frontmatter string (without delimiters) */
rawFrontmatter: string;
/** Content with frontmatter handled according to options */
content: string;
}Usage Examples:
import { parseFrontmatter } from "@astrojs/markdown-remark";
const markdown = `---
title: "My Blog Post"
tags: ["javascript", "tutorial"]
published: true
date: 2023-12-01
---
# Hello World
This is the content of my blog post.`;
// Default behavior (remove frontmatter)
const result = parseFrontmatter(markdown);
console.log(result.frontmatter); // { title: "My Blog Post", tags: [...], published: true, date: 2023-12-01 }
console.log(result.content); // "# Hello World\n\nThis is the content..."
// Preserve frontmatter in content
const preserved = parseFrontmatter(markdown, { frontmatter: 'preserve' });
console.log(preserved.content); // Original markdown with frontmatter intact
// Replace with spaces (useful for sourcemaps)
const withSpaces = parseFrontmatter(markdown, { frontmatter: 'empty-with-spaces' });
console.log(withSpaces.content); // Frontmatter area replaced with spaces
// TOML frontmatter support
const tomlMarkdown = `+++
title = "TOML Example"
tags = ["toml", "config"]
+++
# TOML Content`;
const tomlResult = parseFrontmatter(tomlMarkdown);
console.log(tomlResult.frontmatter); // { title: "TOML Example", tags: [...] }Extracts raw frontmatter content without parsing it.
/**
* Extract raw frontmatter content from markdown
* @param code - The markdown content
* @returns Raw frontmatter string without delimiters, or undefined if no frontmatter
*/
function extractFrontmatter(code: string): string | undefined;Usage Examples:
import { extractFrontmatter } from "@astrojs/markdown-remark";
const markdown = `---
title: "Example"
date: 2023-12-01
---
# Content`;
const raw = extractFrontmatter(markdown);
console.log(raw); // "title: \"Example\"\ndate: 2023-12-01\n"
// No frontmatter case
const noFrontmatter = extractFrontmatter("# Just a heading");
console.log(noFrontmatter); // undefinedValidates that a frontmatter object can be safely serialized.
/**
* Validate if frontmatter object is JSON-serializable
* @param frontmatter - The frontmatter object to validate
* @returns true if valid, false if not serializable
*/
function isFrontmatterValid(frontmatter: Record<string, any>): boolean;Usage Examples:
import { isFrontmatterValid } from "@astrojs/markdown-remark";
// Valid frontmatter
const validFrontmatter = {
title: "My Post",
tags: ["tag1", "tag2"],
published: true,
date: new Date().toISOString()
};
console.log(isFrontmatterValid(validFrontmatter)); // true
// Invalid frontmatter (circular reference)
const invalidFrontmatter: any = { title: "Test" };
invalidFrontmatter.self = invalidFrontmatter;
console.log(isFrontmatterValid(invalidFrontmatter)); // false
// Invalid frontmatter (function)
const withFunction = {
title: "Test",
callback: () => console.log("hello")
};
console.log(isFrontmatterValid(withFunction)); // falseThe package supports both YAML and TOML frontmatter formats:
Uses --- delimiters and js-yaml for parsing:
---
title: "YAML Example"
tags:
- yaml
- frontmatter
metadata:
author: "John Doe"
published: true
date: 2023-12-01T10:00:00Z
---Uses +++ delimiters and smol-toml for parsing:
+++
title = "TOML Example"
tags = ["toml", "frontmatter"]
[metadata]
author = "John Doe"
published = true
date = 2023-12-01T10:00:00Z
+++The frontmatter option in ParseFrontmatterOptions controls how the frontmatter section is handled in the returned content:
Keeps the original frontmatter in the content string:
const result = parseFrontmatter(markdown, { frontmatter: 'preserve' });
// result.content includes the original frontmatter delimiters and contentCompletely removes the frontmatter section:
const result = parseFrontmatter(markdown); // default behavior
// result.content starts with the first line after frontmatterReplaces the frontmatter with spaces, preserving exact character positions:
const result = parseFrontmatter(markdown, { frontmatter: 'empty-with-spaces' });
// Useful for source maps and error reporting with accurate line/column numbersReplaces the frontmatter with line breaks, preserving line numbers:
const result = parseFrontmatter(markdown, { frontmatter: 'empty-with-lines' });
// Preserves line numbers but not column positions