Skip to content

Commit ed2fed5

Browse files
authored
fix head, header, footer front matter (#1159)
1 parent 1ae59fc commit ed2fed5

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

docs/markdown.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ toc: false
1515

1616
The front matter supports the following options:
1717

18-
- **title** — the page title; defaults to the (first) first-level heading of the page, if any
19-
- **toc** — if false, disables the [table of contents](./config#toc)
20-
- **index** — whether to index this page if [search](./search) is enabled; defaults to true for listed pages
18+
- **title** - the page title; defaults to the (first) first-level heading of the page, if any
19+
- **index** - whether to index this page if [search](./search) is enabled; defaults to true for listed pages
2120
- **keywords** <a href="https://github.com/observablehq/framework/releases/tag/v1.1.0" class="observablehq-version-badge" data-version="^1.1.0" title="Added in v1.1.0"></a> - additional words to index for [search](./search); boosted at the same weight as the title
22-
- **draft** <a href="https://github.com/observablehq/framework/releases/tag/v1.1.0" class="observablehq-version-badge" data-version="^1.1.0" title="Added in v1.1.0"></a> — whether to skip this page during build; drafts are also not listed in the default sidebar
23-
- **sql** <a href="https://github.com/observablehq/framework/releases/tag/v1.2.0" class="observablehq-version-badge" data-version="^1.2.0" title="Added in v1.2.0"></a> — table definitions for [SQL code blocks](./sql)
21+
- **draft** <a href="https://github.com/observablehq/framework/releases/tag/v1.1.0" class="observablehq-version-badge" data-version="^1.1.0" title="Added in v1.1.0"></a> - whether to skip this page during build; drafts are also not listed in the default sidebar
22+
- **sql** <a href="https://github.com/observablehq/framework/releases/tag/v1.2.0" class="observablehq-version-badge" data-version="^1.2.0" title="Added in v1.2.0"></a> - table definitions for [SQL code blocks](./sql)
23+
24+
The front matter can also override the following [project configuration](./config) options:
25+
26+
- **toc** - the [table of contents](./config#toc)
27+
- **style** - the [custom stylesheet](./config#style)
28+
- **theme** - the [theme](./config#theme)
29+
- **head** - the [head](./config#head)
30+
- **header** - the [header](./config#header)
31+
- **footer** - the [footer](./config#footer)
32+
- **sidebar** - whether to show the [sidebar](./config#sidebar)
2433

2534
## Headings
2635

src/frontMatter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface FrontMatter {
77
toc?: {show?: boolean; label?: string};
88
style?: string | null;
99
theme?: string[];
10+
head?: string | null;
11+
header?: string | null;
12+
footer?: string | null;
1013
index?: boolean;
1114
keywords?: string[];
1215
draft?: boolean;
@@ -30,21 +33,24 @@ export function readFrontMatter(input: string): {content: string; data: FrontMat
3033
export function normalizeFrontMatter(spec: any = {}): FrontMatter {
3134
const frontMatter: FrontMatter = {};
3235
if (spec == null || typeof spec !== "object") return frontMatter;
33-
const {title, sidebar, toc, index, keywords, draft, sql, style, theme} = spec;
36+
const {title, sidebar, toc, index, keywords, draft, sql, head, header, footer, style, theme} = spec;
3437
if (title !== undefined) frontMatter.title = stringOrNull(title);
3538
if (sidebar !== undefined) frontMatter.sidebar = Boolean(sidebar);
3639
if (toc !== undefined) frontMatter.toc = normalizeToc(toc);
3740
if (index !== undefined) frontMatter.index = Boolean(index);
3841
if (keywords !== undefined) frontMatter.keywords = normalizeKeywords(keywords);
3942
if (draft !== undefined) frontMatter.draft = Boolean(draft);
4043
if (sql !== undefined) frontMatter.sql = normalizeSql(sql);
44+
if (head !== undefined) frontMatter.head = stringOrNull(head);
45+
if (header !== undefined) frontMatter.header = stringOrNull(header);
46+
if (footer !== undefined) frontMatter.footer = stringOrNull(footer);
4147
if (style !== undefined) frontMatter.style = stringOrNull(style);
4248
if (theme !== undefined) frontMatter.theme = normalizeTheme(theme);
4349
return frontMatter;
4450
}
4551

4652
function stringOrNull(spec: unknown): string | null {
47-
return spec == null ? null : String(spec);
53+
return spec == null || spec === false ? null : String(spec);
4854
}
4955

5056
function normalizeToc(spec: unknown): {show?: boolean; label?: string} {

src/markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export function parseMarkdownMetadata(input: string, options: ParseOptions): Pic
359359

360360
function getHtml(
361361
key: "head" | "header" | "footer",
362-
data: Record<string, any>,
362+
data: FrontMatter,
363363
{path, [key]: defaultValue}: ParseOptions
364364
): string | null {
365365
return data[key] !== undefined

test/frontMatter-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe("normalizeFrontMatter(spec)", () => {
1515
assert.deepStrictEqual(normalizeFrontMatter({title: 42}), {title: "42"});
1616
assert.deepStrictEqual(normalizeFrontMatter({title: undefined}), {});
1717
assert.deepStrictEqual(normalizeFrontMatter({title: null}), {title: null});
18+
assert.deepStrictEqual(normalizeFrontMatter({title: false}), {title: null});
1819
assert.deepStrictEqual(normalizeFrontMatter({title: ""}), {title: ""});
20+
assert.deepStrictEqual(normalizeFrontMatter({title: 0}), {title: "0"});
1921
assert.deepStrictEqual(normalizeFrontMatter({title: "foo"}), {title: "foo"});
2022
assert.deepStrictEqual(normalizeFrontMatter({title: {toString: () => "foo"}}), {title: "foo"});
2123
});

0 commit comments

Comments
 (0)