'use strict'; /** * frontmatter.js — minimal YAML-frontmatter reader for the Nomad scripts/ package. * * This is NOT a general YAML parser, and deliberately narrower than the * production structure-sentinel parser it is conceptually descended from * (that one also parses tags arrays and links object-arrays; those tools are * not part of this portable package). This module only answers the questions * frontmatter-touch.js needs: * * 1. Does this file open with a complete `--- ... ---` frontmatter block? * 2. What are the block's COLUMN-ZERO scalar fields (`key: value`)? * 3. On which physical line does a given scalar field sit (for surgical * single-line replace/insert that never reflows the rest of the file)? * * Scope decisions (all deliberate): * - Only column-zero `key: value` lines count as scalar fields. Indented * lines (e.g. the ` target: ...` rows inside a `links:` object-array) * are structurally ignored — this makes it impossible to mistake a * nested mapping value for a top-level field, without needing to * understand the nested structure at all. * - Unknown/unparsable lines are skipped, never fatal — same tolerance * contract as the production parser: one odd line must not make the * whole file unreadable to tooling. * - BOM-aware (Windows editors routinely add one; charter-style files in * the wild have it) and newline-style preserving: parsing never * normalizes anything, callers get enough position info to edit the * original text losslessly. */ /** * Locate the frontmatter block. Returns null when the file doesn't open with * `---` on its first line; `{complete:false}` when the opening fence exists * but no closing fence was found (broken block — treat as unparseable). * * @param {string} content full file text (may carry a UTF-8 BOM) * @returns {null|{complete:boolean, blockLines:string[], headerLineCount:number, hasBom:boolean}} * headerLineCount = number of physical lines from line 0 through the * closing `---` fence inclusive (i.e. body starts at logical line index * headerLineCount). */ function extractBlock(content) { if (typeof content !== 'string') return null; const hasBom = content.charCodeAt(0) === 0xfeff; const text = hasBom ? content.slice(1) : content; const lines = text.split(/\r\n|\r|\n/); if (lines[0] === undefined || lines[0].trim() !== '---') return null; for (let i = 1; i < lines.length; i++) { if (lines[i].trim() === '---') { return { complete: true, blockLines: lines.slice(1, i), headerLineCount: i + 1, hasBom }; } } return { complete: false, blockLines: [], headerLineCount: 0, hasBom }; } const SCALAR_LINE_RE = /^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/; /** * Parse column-zero scalar fields out of a block's lines. * A line only counts when it starts at column zero (no leading whitespace) — * indented continuation/nested lines are ignored by design (see header). * First occurrence wins for duplicate keys (a duplicate is a file bug this * module has no business resolving; stable first-wins keeps edits predictable). * * @param {string[]} blockLines lines between the fences (exclusive) * @returns {{fields:Object., lineIndexByKey:Object.}} * lineIndexByKey values are indexes INTO blockLines (0-based). */ function parseScalarFields(blockLines) { const fields = {}; const lineIndexByKey = {}; for (let i = 0; i < blockLines.length; i++) { const line = blockLines[i]; if (!line || /^\s/.test(line)) continue; // indented or empty — not a top-level scalar const m = line.match(SCALAR_LINE_RE); if (!m) continue; const key = m[1]; if (Object.prototype.hasOwnProperty.call(fields, key)) continue; // first occurrence wins fields[key] = m[2].trim(); lineIndexByKey[key] = i; } return { fields, lineIndexByKey }; } /** * One-call convenience: block + scalar fields. * @returns {null|{complete:boolean, hasBom:boolean, headerLineCount:number, * blockLines:string[], fields:Object, lineIndexByKey:Object}} */ function parse(content) { const block = extractBlock(content); if (!block) return null; if (!block.complete) { return { complete: false, hasBom: block.hasBom, headerLineCount: 0, blockLines: [], fields: {}, lineIndexByKey: {} }; } const scalars = parseScalarFields(block.blockLines); return { complete: true, hasBom: block.hasBom, headerLineCount: block.headerLineCount, blockLines: block.blockLines, fields: scalars.fields, lineIndexByKey: scalars.lineIndexByKey, }; } module.exports = { extractBlock, parseScalarFields, parse, SCALAR_LINE_RE, };