/** * A markdown frontmatter reader: the leading `---` … `---` block, read as a small, dependency-free subset of YAML. * * - `key: scalar`, where a scalar is a bare word, a double-quoted JSON string, or a single-quoted YAML string (`''` is * one `'`). Bare `true` / `false` and plain decimal numbers are typed unless `typedScalars` is `false`. * - `key: [flow, list]` of scalars. * - `key:` alone opens ONE level of block: indented `- item` rows make a list of scalars, indented `key: value` rows * make a map. With nothing under it the key reads as `null`. * - `# comments`, on their own line or after a value (a bare value's comment starts at ` #`). A BOM is dropped, and * `\r\n` and lone `\r` line endings become `\n`. * * Every refusal names its 1-based file line, and a refused row is left out of the values. */ import { type Maybe } from '../value/maybe.type'; /** * One frontmatter scalar: a string, or a typed `true` / `false` / number. */ export type MarkdownFrontmatterScalar = string | number | boolean; /** * A frontmatter list, written `[a, b]` or as indented `- item` rows. */ export type MarkdownFrontmatterList = ReadonlyArray; /** * A frontmatter scalar or list. */ export type MarkdownFrontmatterLeaf = MarkdownFrontmatterScalar | MarkdownFrontmatterList; /** * A one-level map of indented `key: value` rows. */ export type MarkdownFrontmatterMap = Readonly>; /** * One frontmatter value. `null` is a block-opening `key:` with no indented rows under it (YAML's null); the reader * never stores `undefined`. */ export type MarkdownFrontmatterValue = Maybe; /** * One row the reader refused, with its 1-based file line. */ export interface MarkdownFrontmatterProblem { readonly line: number; readonly message: string; } /** * The result of {@link splitMarkdownFrontmatter}. */ export interface MarkdownFrontmatterSplit { /** * Whether the text opens with a `---` line that a later `---` line closes. */ readonly hasFrontmatter: boolean; /** * Whether the text opens with a `---` line that is never closed. */ readonly unclosed: boolean; /** * The lines between the fences. Line `i` is file line `i + 2`. Empty unless `hasFrontmatter`. */ readonly frontmatterLines: ReadonlyArray; /** * The text after the closing fence, else the whole text. The BOM is dropped and line endings are `\n`. */ readonly body: string; } /** * The result of {@link parseMarkdownFrontmatter}. */ export interface MarkdownFrontmatter extends Omit { /** * The values by key, in row order. A refused row is left out. */ readonly values: Readonly>; /** * The 1-based file line of each read key. A nested map row is keyed `parent.child`. */ readonly lines: ReadonlyMap; /** * Every refused row, in line order. */ readonly problems: ReadonlyArray; } /** * The config of {@link parseMarkdownFrontmatter}. */ export interface ParseMarkdownFrontmatterConfig { /** * Whether a bare `true` / `false` reads as a boolean and a bare `-?digits(.digits)?` as a number. Defaults to `true`. * When `false`, every scalar is a string. */ readonly typedScalars?: Maybe; } /** * Splits a markdown text into its leading `---` frontmatter lines and its body. * * The first line must be the opening fence and a later line the closing fence; each is `---` once trimmed. A BOM is * dropped and `\r\n` / `\r` line endings become `\n`, in the body too. An unclosed block leaves the whole text as the body. * * @param text - The whole markdown text. * @returns The frontmatter lines and the body. * * @dbxUtil * @dbxUtilCategory string * @dbxUtilTags markdown, frontmatter, split, fence, body, yaml, docs * @dbxUtilRelated parse-markdown-frontmatter * * @example * ```ts * splitMarkdownFrontmatter('---\ntitle: Setup\n---\n# Setup'); * // { hasFrontmatter: true, unclosed: false, frontmatterLines: ['title: Setup'], body: '# Setup' } * splitMarkdownFrontmatter('# Setup'); // { hasFrontmatter: false, unclosed: false, frontmatterLines: [], body: '# Setup' } * ``` */ export declare function splitMarkdownFrontmatter(text: string): MarkdownFrontmatterSplit; /** * Parses the leading `---` frontmatter block of a markdown text. * * The grammar is a small YAML subset: `key: scalar` (bare, double-quoted JSON, or single-quoted with `''` for `'`), * `key: [flow, list]`, and a bare `key:` opening one level of indented `- item` rows (a list) or `key: value` rows (a * map), with `#` comments. No problem is reported for a text with no opening fence. An unclosed block reports one * problem at line 1 and leaves the whole text as the body. Each refused row is reported at its line and left out of the * values: a malformed row, tab indentation, a duplicate key (the first is kept), an indented row with no block above * it, a block mixing list and map rows, an empty list item, and an unterminated quote or list. * * @param text - The whole markdown text. * @param config - Whether bare scalars are typed (default `true`). * @returns The values, their lines, the body and every problem. * * @dbxUtil * @dbxUtilCategory string * @dbxUtilTags markdown, frontmatter, parse, yaml, metadata, docs, note * @dbxUtilRelated split-markdown-frontmatter, scan-json-string-literal * * @example * ```ts * const parsed = parseMarkdownFrontmatter([ * '---', * 'id: apparel-plain', * 'title: Plain apparel pages', * 'sites: [product-page]', * 'where:', * ' category: [apparel]', * 'ask: "`brief` describes a garment with no logo."', * 'tags:', * ' - apparel', * " - 'plain cut'", * '---', * 'Name the garment and the colour.' * ].join('\n')); * // parsed.values: { id: 'apparel-plain', title: 'Plain apparel pages', sites: ['product-page'], * // where: { category: ['apparel'] }, ask: '`brief` describes a garment with no logo.', tags: ['apparel', 'plain cut'] } * // parsed.lines.get('where.category'): 6 * // parsed.body: 'Name the garment and the colour.' * ``` */ export declare function parseMarkdownFrontmatter(text: string, config?: Maybe): MarkdownFrontmatter;