// `MarkdownParseOptions.bom`: under `"skip"` one leading U+FEFF is stepped // over, recorded as the document's `bom`, and counted in every offset; the // default reads it as text. import { assert, describe, it } from "@effect/vitest"; import { Effect, Result } from "effect"; import { Markdown } from "../src/Markdown.js"; import { MarkdownDocument } from "../src/MarkdownDocument.js"; import { MarkdownEdit } from "../src/MarkdownEdit.js"; import { MarkdownFormat, MarkdownFormattingOptions } from "../src/MarkdownFormat.js"; const BOM = "\uFEFF"; const parse = (text: string, options?: Parameters[1]): MarkdownDocument => { const result = MarkdownDocument.parseResult(text, options); if (Result.isFailure(result)) throw new Error(result.failure.message); return result.success; }; const skip = (text: string, frontmatter = false): MarkdownDocument => parse(text, { bom: "skip", ...(frontmatter ? { frontmatter } : {}) }); interface PointShape { readonly line: number; readonly column: number; readonly offset: number; } /** Every node of the tree, in order, as `[type, start, end, valueStart]` with plain-object points. */ const shape = (node: unknown, out: Array = []): Array => { if (node === null || typeof node !== "object") return out; const record = node as Record; const position = record.position as { start: PointShape; end: PointShape } | undefined; if (typeof record.type === "string" && position !== undefined) { const point = (p: PointShape) => ({ line: p.line, column: p.column, offset: p.offset }); out.push([record.type, point(position.start), point(position.end), record.valueStart]); } for (const key of ["children", "contents"]) { const children = record[key]; if (Array.isArray(children)) for (const child of children) shape(child, out); } return out; }; /** The shape of a markless parse moved past one leading mark: offset +1, and column +1 on line 1. */ const shifted = (bare: Array): Array => bare.map((entry) => { const [type, start, end, valueStart] = entry as [string, PointShape, PointShape, number | undefined]; const move = (p: PointShape) => ({ line: p.line, column: p.line === 1 ? p.column + 1 : p.column, offset: p.offset + 1, }); return [type, move(start), move(end), valueStart === undefined ? undefined : valueStart + 1]; }); const corpus = [ "# Title\n\nBody *text*.\n", "# Title\r\n\r\nBody\r\n", "- a\n- b\n\n> quote\n", "| a | b |\n| - | - |\n| 1 | 2 |\n", "See [ref][a] and [^n].\n\n[a]: /a\n\n[^n]: note\n", "Title\n=====\n", "```js\nx\n```\n", " indented\n", "plain", "", ]; describe("MarkdownDocument.parseResult without the bom option", () => { it("reads a leading BOM as text, so a mark-led heading is a paragraph and no bom field is recorded", () => { const source = `${BOM}# Title\n`; for (const doc of [parse(source), parse(source, { bom: "keep" })]) { assert.strictEqual(doc.root.children[0]?.type, "paragraph"); assert.strictEqual(doc.root.position.start.offset, 0); assert.isFalse("bom" in doc); } }); it("reads a mark-led fence as no frontmatter, and the accessors agree", () => { const doc = parse(`${BOM}---\ntitle: hi\n---\n`, { frontmatter: true }); assert.isTrue(doc.root.children[0]?.type !== "frontmatter"); assert.isFalse(doc.hasFrontmatterBlock); assert.strictEqual(doc.frontmatterFence, "absent"); }); }); describe('MarkdownDocument.parseResult with bom: "skip"', () => { it("parses past the mark and positions the first node at offset 1, column 2", () => { const source = `${BOM}# Title\n`; const doc = skip(source); assert.strictEqual(doc.bom, true); assert.strictEqual(doc.source, source); const heading = doc.root.children[0]; assert.strictEqual(heading?.type, "heading"); assert.deepStrictEqual( { line: heading?.position.start.line, column: heading?.position.start.column, offset: heading?.position.start.offset, }, { line: 1, column: 2, offset: 1 }, ); assert.strictEqual(doc.root.position.start.offset, 1); assert.strictEqual(doc.root.position.end.offset, source.length); assert.deepStrictEqual(doc.lineIndex.positionAt(1), { line: 0, character: 1 }); assert.strictEqual(source.slice(heading?.position.start.offset, heading?.position.end.offset), "# Title"); }); it("gives every node exactly its markless twin's position moved past the mark", () => { for (const bare of corpus) { assert.deepStrictEqual(shape(skip(`${BOM}${bare}`).root), shifted(shape(parse(bare).root)), JSON.stringify(bare)); } }); it("moves a frontmatter capture past the mark, LF and CRLF alike", () => { for (const bare of [ "---\ntitle: hi\n---\n# Body\n", "---\r\ntitle: hi\r\nn: 2\r\n---\r\n# Body\r\n", "+++\na = 1\n+++\n", ]) { const plain = parse(bare, { frontmatter: true }); const marked = skip(`${BOM}${bare}`, true); assert.strictEqual(marked.root.children[0]?.type, "frontmatter", JSON.stringify(bare)); assert.deepStrictEqual(shape(marked.root), shifted(shape(plain.root)), JSON.stringify(bare)); const capture = marked.root.children[0]; if (capture?.type !== "frontmatter") throw new Error("no capture"); const before = plain.root.children[0]; if (before?.type !== "frontmatter") throw new Error("no capture"); assert.strictEqual(capture.value, before.value); assert.strictEqual(capture.valueStart, (before.valueStart ?? -1) + 1); const valueStart = capture.valueStart ?? -1; assert.strictEqual(marked.source.slice(valueStart, valueStart + capture.value.length), capture.value); } }); it("places the CRLF frontmatter value and the body by source offsets", () => { const source = `${BOM}---\r\ntitle: hi\r\n---\r\n# Body\r\n`; const doc = skip(source, true); const [capture, heading] = doc.root.children; assert.strictEqual(capture?.type, "frontmatter"); assert.deepStrictEqual( [capture?.position.start.offset, capture?.position.end.offset], [1, source.indexOf("---", 2) + 3], ); assert.strictEqual(heading?.position.start.offset, source.indexOf("# Body")); assert.strictEqual(heading?.position.start.line, 4); assert.strictEqual(heading?.position.start.column, 1); }); it("records bom: false and parses exactly as keep does when the source has no mark", () => { for (const bare of corpus) { const doc = skip(bare); assert.strictEqual(doc.bom, false, JSON.stringify(bare)); assert.deepStrictEqual(shape(doc.root), shape(parse(bare).root), JSON.stringify(bare)); } }); it("parses a source that is only a mark as an empty document after it", () => { const doc = skip(BOM); assert.strictEqual(doc.bom, true); assert.strictEqual(doc.root.children.length, 0); assert.deepStrictEqual(shape(doc.root), [ ["root", { line: 1, column: 2, offset: 1 }, { line: 1, column: 2, offset: 1 }, undefined], ]); assert.strictEqual(parse(BOM).root.position.start.offset, 0); }); it("parses a mark followed only by a line ending as an empty document", () => { const doc = skip(`${BOM}\r\n`); assert.strictEqual(doc.root.children.length, 0); assert.strictEqual(doc.root.position.start.offset, 1); assert.strictEqual(doc.root.position.end.offset, 3); }); it("skips exactly one mark: a second is ordinary text", () => { const doc = skip(`${BOM}${BOM}# Title\n`); assert.strictEqual(doc.bom, true); const paragraph = doc.root.children[0]; assert.strictEqual(paragraph?.type, "paragraph"); assert.strictEqual(paragraph?.position.start.offset, 1); }); it("reads hasFrontmatterBlock and frontmatterFence past the mark, as the parse did", () => { const closed = skip(`${BOM}---\ntitle: hi\n---\n`); assert.isTrue(closed.hasFrontmatterBlock); assert.strictEqual(closed.frontmatterFence, "closed"); const unclosed = skip(`${BOM}---\ntitle: hi\n`); assert.isFalse(unclosed.hasFrontmatterBlock); assert.strictEqual(unclosed.frontmatterFence, "unclosed"); }); it("re-derives unresolved footnotes past the mark for a document built with make", () => { const source = `${BOM}---\nn: 1\n---\nSee [^a].\n`; const parsed = skip(source, true); const made = MarkdownDocument.make({ source: parsed.source, root: parsed.root, diagnostics: parsed.diagnostics, definitions: parsed.definitions, bom: true, }); const [marker] = made.unresolvedFootnoteReferences; assert.strictEqual(marker?.label, "a"); assert.strictEqual(marker?.position.start.offset, source.indexOf("[^a]")); assert.deepStrictEqual( made.unresolvedFootnoteReferences.map((m) => m.position.start.offset), parsed.unresolvedFootnoteReferences.map((m) => m.position.start.offset), ); }); it("round-trips an edit computed from node offsets without touching the mark", () => { const source = `${BOM}# Title\r\n\r\nBody\r\n`; const doc = skip(source); const heading = doc.root.children[0]; if (heading === undefined) throw new Error("no heading"); const start = heading.position.start.offset; const edited = MarkdownEdit.applyAll(source, [ MarkdownEdit.make({ offset: start, length: heading.position.end.offset - start, content: "# Renamed" }), ]); assert.strictEqual(edited, `${BOM}# Renamed\r\n\r\nBody\r\n`); const whole = MarkdownEdit.applyAll(source, [ MarkdownEdit.make({ offset: doc.root.position.start.offset, length: doc.root.position.end.offset - doc.root.position.start.offset, content: "new\n", }), ]); assert.strictEqual(whole, `${BOM}new\n`); assert.deepStrictEqual(shape(skip(edited).root), shifted(shape(parse("# Renamed\r\n\r\nBody\r\n").root))); }); it("gives Markdown.parseResult the same tree", () => { const source = `${BOM}---\na: 1\n---\n- x\n`; const root = Markdown.parseResult(source, { bom: "skip", frontmatter: true }); assert.isTrue(Result.isSuccess(root)); if (Result.isSuccess(root)) assert.deepStrictEqual(shape(root.success), shape(skip(source, true).root)); }); it.effect("gives MarkdownDocument.parse the same document", () => Effect.gen(function* () { const source = `${BOM}# Title\n`; const doc = yield* MarkdownDocument.parse(source, { bom: "skip" }); assert.strictEqual(doc.bom, true); assert.deepStrictEqual(shape(doc.root), shape(skip(source).root)); }), ); }); describe('MarkdownFormat with bom: "skip"', () => { it("normalizes a mark-led heading as a heading and keeps the mark", () => { const source = `${BOM}Title\n=====\n\n* a\n* b\n`; const options = MarkdownFormattingOptions.make({ bom: "skip", headingStyle: "atx", bulletChar: "-" }); assert.strictEqual(MarkdownFormat.formatToString(source, undefined, options), `${BOM}# Title\n\n- a\n- b\n`); for (const edit of MarkdownFormat.format(source, undefined, options)) assert.isAbove(edit.offset, 0); }); it("keeps the mark on a frontmatter document and on a CRLF one", () => { const options = MarkdownFormattingOptions.make({ bom: "skip", frontmatter: true, headingStyle: "atx" }); assert.strictEqual( MarkdownFormat.formatToString(`${BOM}---\na: 1\n---\nTitle\r\n=====\r\n`, undefined, options), `${BOM}---\na: 1\n---\n# Title\r\n`, ); }); it("reads the mark as text without the option, so a mark-led ATX heading is a paragraph and stays as written", () => { const source = `${BOM}# Title\n`; const setext = (bom?: "keep" | "skip") => MarkdownFormat.formatToString( source, undefined, MarkdownFormattingOptions.make({ headingStyle: "setext", ...(bom === undefined ? {} : { bom }) }), ); assert.strictEqual(setext(), source); assert.strictEqual(setext("keep"), source); assert.strictEqual(setext("skip"), `${BOM}Title\n=====\n`); }); it("formats a source that is only a mark to itself", () => { assert.strictEqual( MarkdownFormat.formatToString(BOM, undefined, MarkdownFormattingOptions.make({ bom: "skip" })), BOM, ); }); });