-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* validate front matter * normalizeFrontMatter * fix title: null * pretty error in preview, crash on build * simplify * simplify 2 * treat invalid frontmatter as content --------- Co-authored-by: Mike Bostock <mbostock@gmail.com>
- Loading branch information
Showing
35 changed files
with
230 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import matter from "gray-matter"; | ||
import {normalizeTheme} from "./config.js"; | ||
import {yellow} from "./tty.js"; | ||
|
||
export interface FrontMatter { | ||
title?: string | null; | ||
toc?: {show?: boolean; label?: string}; | ||
style?: string | null; | ||
theme?: string[]; | ||
index?: boolean; | ||
keywords?: string[]; | ||
draft?: boolean; | ||
sidebar?: boolean; | ||
sql?: {[key: string]: string}; | ||
} | ||
|
||
export function readFrontMatter(input: string): {content: string; data: FrontMatter} { | ||
try { | ||
const {content, data} = matter(input, {}); | ||
return {content, data: normalizeFrontMatter(data)}; | ||
} catch (error: any) { | ||
if ("mark" in error) { | ||
console.warn(`${yellow("Invalid front matter")}: ${error.reason}`); | ||
return {data: {}, content: input}; | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
export function normalizeFrontMatter(spec: any = {}): FrontMatter { | ||
const frontMatter: FrontMatter = {}; | ||
if (spec == null || typeof spec !== "object") return frontMatter; | ||
const {title, sidebar, toc, index, keywords, draft, sql, style, theme} = spec; | ||
if (title !== undefined) frontMatter.title = stringOrNull(title); | ||
if (sidebar !== undefined) frontMatter.sidebar = Boolean(sidebar); | ||
if (toc !== undefined) frontMatter.toc = normalizeToc(toc); | ||
if (index !== undefined) frontMatter.index = Boolean(index); | ||
if (keywords !== undefined) frontMatter.keywords = normalizeKeywords(keywords); | ||
if (draft !== undefined) frontMatter.draft = Boolean(draft); | ||
if (sql !== undefined) frontMatter.sql = normalizeSql(sql); | ||
if (style !== undefined) frontMatter.style = stringOrNull(style); | ||
if (theme !== undefined) frontMatter.theme = normalizeTheme(theme); | ||
return frontMatter; | ||
} | ||
|
||
function stringOrNull(spec: unknown): string | null { | ||
return spec == null ? null : String(spec); | ||
} | ||
|
||
function normalizeToc(spec: unknown): {show?: boolean; label?: string} { | ||
if (spec == null) return {show: false}; | ||
if (typeof spec !== "object") return {show: Boolean(spec)}; | ||
const {show, label} = spec as {show: unknown; label: unknown}; | ||
const toc: FrontMatter["toc"] = {}; | ||
if (show !== undefined) toc.show = Boolean(show); | ||
if (label !== undefined) toc.label = String(label); | ||
return toc; | ||
} | ||
|
||
function normalizeKeywords(spec: unknown): string[] { | ||
return spec == null ? [] : typeof spec === "string" ? [spec] : Array.from(spec as any, String); | ||
} | ||
|
||
function normalizeSql(spec: unknown): {[key: string]: string} { | ||
if (spec == null || typeof spec !== "object") return {}; | ||
const sql: {[key: string]: string} = {}; | ||
for (const key in spec) sql[key] = String(spec[key]); | ||
return sql; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import assert from "node:assert"; | ||
import {normalizeFrontMatter} from "../src/frontMatter.js"; | ||
|
||
describe("normalizeFrontMatter(spec)", () => { | ||
it("returns the empty object for an undefined, null, empty spec", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter(), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter(undefined), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter(null), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter(false), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter(true), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter(42), {}); | ||
}); | ||
it("coerces the title to a string or null", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({title: 42}), {title: "42"}); | ||
assert.deepStrictEqual(normalizeFrontMatter({title: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({title: null}), {title: null}); | ||
assert.deepStrictEqual(normalizeFrontMatter({title: ""}), {title: ""}); | ||
assert.deepStrictEqual(normalizeFrontMatter({title: "foo"}), {title: "foo"}); | ||
assert.deepStrictEqual(normalizeFrontMatter({title: {toString: () => "foo"}}), {title: "foo"}); | ||
}); | ||
it("coerces the toc to {show?, label?}", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: false}), {toc: {show: false}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: true}), {toc: {show: true}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: null}), {toc: {show: false}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: ""}), {toc: {show: false}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: 42}), {toc: {show: true}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {}}), {toc: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {show: 1}}), {toc: {show: true}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {show: 0}}), {toc: {show: false}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {show: null}}), {toc: {show: false}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {show: undefined}}), {toc: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {label: null}}), {toc: {label: "null"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {label: false}}), {toc: {label: "false"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {label: 42}}), {toc: {label: "42"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({toc: {label: {toString: () => "foo"}}}), {toc: {label: "foo"}}); | ||
}); | ||
it("coerces index to a boolean", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({index: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({index: null}), {index: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({index: 0}), {index: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({index: 1}), {index: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({index: true}), {index: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({index: false}), {index: false}); | ||
}); | ||
it("coerces sidebar to a boolean", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: null}), {sidebar: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: 0}), {sidebar: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: 1}), {sidebar: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: true}), {sidebar: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sidebar: false}), {sidebar: false}); | ||
}); | ||
it("coerces draft to a boolean", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: null}), {draft: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: 0}), {draft: false}); | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: 1}), {draft: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: true}), {draft: true}); | ||
assert.deepStrictEqual(normalizeFrontMatter({draft: false}), {draft: false}); | ||
}); | ||
it("coerces keywords to an array of strings", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: null}), {keywords: []}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: []}), {keywords: []}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: [1, 2]}), {keywords: ["1", "2"]}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: "test"}), {keywords: ["test"]}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: ""}), {keywords: [""]}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: "foo, bar"}), {keywords: ["foo, bar"]}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: [1, "foo"]}), {keywords: ["1", "foo"]}); | ||
assert.deepStrictEqual(normalizeFrontMatter({keywords: new Set([1, "foo"])}), {keywords: ["1", "foo"]}); | ||
}); | ||
it("coerces sql to a Record<string, string>", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: undefined}), {}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: null}), {sql: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: 0}), {sql: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: 1}), {sql: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: false}), {sql: {}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: {foo: 1}}), {sql: {foo: "1"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: {foo: null}}), {sql: {foo: "null"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: {foo: "bar"}}), {sql: {foo: "bar"}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: {foo: []}}), {sql: {foo: ""}}); | ||
assert.deepStrictEqual(normalizeFrontMatter({sql: {foo: {toString: () => "bar"}}}), {sql: {foo: "bar"}}); | ||
}); | ||
it("ignores unknown properties", () => { | ||
assert.deepStrictEqual(normalizeFrontMatter({foo: 42}), {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
title: YAML | ||
style: | ||
style: custom.css | ||
keywords: | ||
- one | ||
- two | ||
--- | ||
|
Oops, something went wrong.