|
1 | 1 | /** biome-ignore-all lint/suspicious/noExplicitAny: Use of any is ok for test files */ |
2 | 2 |
|
3 | | -import { describe, test } from "vitest"; |
| 3 | +import { describe, expect, test } from "vitest"; |
| 4 | +import { |
| 5 | + extractFrontmatterNode, |
| 6 | + frontmatterPlugin, |
| 7 | + parseFrontmatter, |
| 8 | +} from "./frontmatter"; |
4 | 9 |
|
5 | | -describe("m2dFrontmatter", () => { |
6 | | - test("should pass", ({ expect }) => { |
7 | | - expect(true).toBe(true); |
| 10 | +describe("parseFrontmatter", () => { |
| 11 | + test("should parse YAML with lists, strings, and numbers", () => { |
| 12 | + const raw = ` |
| 13 | +title: "Modern Architecture" |
| 14 | +author: "Mayank Chaudhari" |
| 15 | +revision: 3 |
| 16 | +published: true |
| 17 | +tags: |
| 18 | + - docx |
| 19 | + - markdown |
| 20 | + - typescript |
| 21 | +`; |
| 22 | + const res = parseFrontmatter(raw); |
| 23 | + expect(res.title).toBe("Modern Architecture"); |
| 24 | + expect(res.author).toBe("Mayank Chaudhari"); |
| 25 | + expect(res.revision).toBe(3); |
| 26 | + expect(res.published).toBe(true); |
| 27 | + expect(res.tags).toEqual(["docx", "markdown", "typescript"]); |
| 28 | + }); |
| 29 | + |
| 30 | + test("should preserve '#' in quotes and URLs without comment truncation", () => { |
| 31 | + const raw = ` |
| 32 | +title: "C# Guide" |
| 33 | +url: "https://example.com/#section" |
| 34 | +commented: Value # this is actual comment |
| 35 | +`; |
| 36 | + const res = parseFrontmatter(raw); |
| 37 | + expect(res.title).toBe("C# Guide"); |
| 38 | + expect(res.url).toBe("https://example.com/#section"); |
| 39 | + expect(res.commented).toBe("Value"); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should parse inline bracketed arrays with quoted commas", () => { |
| 43 | + const raw = `keywords: ["React, Next.js", TypeScript]`; |
| 44 | + const res = parseFrontmatter(raw); |
| 45 | + expect(res.keywords).toEqual(["React, Next.js", "TypeScript"]); |
| 46 | + }); |
| 47 | + |
| 48 | + test("should parse TOML front-matter when smol-toml or custom parser is present", () => { |
| 49 | + const raw = `title = "TOML Title"`; |
| 50 | + const res = parseFrontmatter(raw, "toml"); |
| 51 | + // If smol-toml is hoisted, it parses successfully; otherwise returns empty object |
| 52 | + if (res.title) { |
| 53 | + expect(res.title).toBe("TOML Title"); |
| 54 | + } else { |
| 55 | + expect(res).toEqual({}); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + test("should parse TOML when custom parser is provided", () => { |
| 60 | + const raw = `title = "TOML Title"`; |
| 61 | + const plugin = frontmatterPlugin(undefined, { |
| 62 | + parser: (input) => ({ |
| 63 | + title: input.includes("TOML") ? "Custom TOML" : "", |
| 64 | + }), |
| 65 | + }); |
| 66 | + |
| 67 | + const docxProps: any = {}; |
| 68 | + const ast: any = { |
| 69 | + type: "root", |
| 70 | + children: [{ type: "toml", value: raw }], |
| 71 | + }; |
| 72 | + |
| 73 | + plugin.preprocess?.(ast, {}, docxProps); |
| 74 | + expect(docxProps.title).toBe("Custom TOML"); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("extractFrontmatterNode", () => { |
| 79 | + test("should find yaml or toml node in AST", () => { |
| 80 | + const ast: any = { |
| 81 | + type: "root", |
| 82 | + children: [{ type: "yaml", value: "title: Test" }], |
| 83 | + }; |
| 84 | + const node = extractFrontmatterNode(ast); |
| 85 | + expect(node?.value).toBe("title: Test"); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("frontmatterPlugin metadata normalization", () => { |
| 90 | + test("should populate document properties from AST during preprocess", () => { |
| 91 | + const plugin = frontmatterPlugin(); |
| 92 | + const props: any = {}; |
| 93 | + const ast: any = { |
| 94 | + type: "root", |
| 95 | + children: [ |
| 96 | + { |
| 97 | + type: "yaml", |
| 98 | + value: ` |
| 99 | +title: "Plugin Title" |
| 100 | +author: "Plugin Author" |
| 101 | +tags: ["Docx, OpenXML", Markdown] |
| 102 | +department: "Engineering" |
| 103 | +`, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }; |
| 107 | + |
| 108 | + plugin.preprocess?.(ast, {}, props); |
| 109 | + |
| 110 | + expect(props.title).toBe("Plugin Title"); |
| 111 | + expect(props.creator).toBe("Plugin Author"); |
| 112 | + expect(props.keywords).toBe("Docx, OpenXML, Markdown"); |
| 113 | + expect(props.customProperties).toContainEqual({ |
| 114 | + name: "department", |
| 115 | + value: "Engineering", |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + test("should skip null, undefined, objects, and forbidden prototype keys", () => { |
| 120 | + const plugin = frontmatterPlugin(undefined, { |
| 121 | + parser: () => ({ |
| 122 | + title: "Valid", |
| 123 | + __proto__: "evil", |
| 124 | + nested: { child: true }, |
| 125 | + emptyVal: null, |
| 126 | + undefVal: undefined, |
| 127 | + }), |
| 128 | + }); |
| 129 | + const props: any = {}; |
| 130 | + const ast: any = { |
| 131 | + type: "root", |
| 132 | + children: [ |
| 133 | + { |
| 134 | + type: "yaml", |
| 135 | + value: "title: Valid", |
| 136 | + }, |
| 137 | + ], |
| 138 | + }; |
| 139 | + |
| 140 | + plugin.preprocess?.(ast, {}, props); |
| 141 | + |
| 142 | + expect(props.title).toBe("Valid"); |
| 143 | + expect(Object.getPrototypeOf(props)).not.toBe("evil"); |
| 144 | + expect( |
| 145 | + props.customProperties?.some((p: any) => p.name === "__proto__"), |
| 146 | + ).toBeFalsy(); |
| 147 | + expect( |
| 148 | + props.customProperties?.some((p: any) => p.name === "nested"), |
| 149 | + ).toBeFalsy(); |
| 150 | + expect( |
| 151 | + props.customProperties?.some((p: any) => p.name === "emptyVal"), |
| 152 | + ).toBeFalsy(); |
| 153 | + }); |
| 154 | + |
| 155 | + test("should support custom transform function", () => { |
| 156 | + const plugin = frontmatterPlugin(undefined, { |
| 157 | + transform: (data, props) => { |
| 158 | + props.title = `Transformed: ${data.title}`; |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const props: any = {}; |
| 163 | + const ast: any = { |
| 164 | + type: "root", |
| 165 | + children: [{ type: "yaml", value: "title: Custom" }], |
| 166 | + }; |
| 167 | + |
| 168 | + plugin.preprocess?.(ast, {}, props); |
| 169 | + expect(props.title).toBe("Transformed: Custom"); |
8 | 170 | }); |
9 | 171 | }); |
0 commit comments