frontmatter.ts — astro Source File
Architecture documentation for frontmatter.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 527bec49_26ec_0a56_3702_bf73cfac4e0b["frontmatter.ts"] 38ee36f6_1b8f_5a62_1295_989b44329ca0["js-yaml"] 527bec49_26ec_0a56_3702_bf73cfac4e0b --> 38ee36f6_1b8f_5a62_1295_989b44329ca0 700c64be_9e12_6323_a727_8d7af053511b["smol-toml"] 527bec49_26ec_0a56_3702_bf73cfac4e0b --> 700c64be_9e12_6323_a727_8d7af053511b style 527bec49_26ec_0a56_3702_bf73cfac4e0b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import yaml from 'js-yaml';
import * as toml from 'smol-toml';
export function isFrontmatterValid(frontmatter: Record<string, any>) {
try {
// ensure frontmatter is JSON-serializable
JSON.stringify(frontmatter);
} catch {
return false;
}
return typeof frontmatter === 'object' && frontmatter !== null;
}
// Capture frontmatter wrapped with `---` or `+++`, including any characters and new lines within it.
// Only capture if `---` or `+++` exists near the top of the file, including:
// 1. Start of file (including if has BOM encoding)
// 2. Start of file with any whitespace (but `---` or `+++` must still start on a new line)
const frontmatterRE = /(?:^\uFEFF?|^\s*\n)(?:---|\+\+\+)([\s\S]*?\n)(?:---|\+\+\+)/;
const frontmatterTypeRE = /(?:^\uFEFF?|^\s*\n)(---|\+\+\+)/;
export function extractFrontmatter(code: string): string | undefined {
return frontmatterRE.exec(code)?.[1];
}
function getFrontmatterParser(code: string): [string, (str: string) => unknown] {
return frontmatterTypeRE.exec(code)?.[1] === '+++' ? ['+++', toml.parse] : ['---', yaml.load];
}
export 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 the frontmatter with empty spaces. (preserves sourcemap line/col/offset)
* - `empty-with-lines`: Replace the frontmatter with empty line breaks. (preserves sourcemap line/col)
*
* @default 'remove'
*/
frontmatter: 'preserve' | 'remove' | 'empty-with-spaces' | 'empty-with-lines';
}
export interface ParseFrontmatterResult {
frontmatter: Record<string, any>;
rawFrontmatter: string;
content: string;
}
export function parseFrontmatter(
code: string,
options?: ParseFrontmatterOptions,
): ParseFrontmatterResult {
const rawFrontmatter = extractFrontmatter(code);
if (rawFrontmatter == null) {
return { frontmatter: {}, rawFrontmatter: '', content: code };
}
const [delims, parser] = getFrontmatterParser(code);
const parsed = parser(rawFrontmatter);
const frontmatter = (parsed && typeof parsed === 'object' ? parsed : {}) as Record<string, any>;
let content: string;
switch (options?.frontmatter ?? 'remove') {
case 'preserve':
content = code;
break;
case 'remove':
content = code.replace(`${delims}${rawFrontmatter}${delims}`, '');
break;
case 'empty-with-spaces':
content = code.replace(
`${delims}${rawFrontmatter}${delims}`,
` ${rawFrontmatter.replace(/[^\r\n]/g, ' ')} `,
);
break;
case 'empty-with-lines':
content = code.replace(
`${delims}${rawFrontmatter}${delims}`,
rawFrontmatter.replace(/[^\r\n]/g, ''),
);
break;
}
return {
frontmatter,
rawFrontmatter,
content,
};
}
Domain
Subdomains
Dependencies
- js-yaml
- smol-toml
Source
Frequently Asked Questions
What does frontmatter.ts do?
frontmatter.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in frontmatter.ts?
frontmatter.ts defines 4 function(s): extractFrontmatter, getFrontmatterParser, isFrontmatterValid, parseFrontmatter.
What does frontmatter.ts depend on?
frontmatter.ts imports 2 module(s): js-yaml, smol-toml.
Where is frontmatter.ts in the architecture?
frontmatter.ts is located at packages/markdown/remark/src/frontmatter.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/markdown/remark/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free