-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
209 additions
and
124 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,54 @@ | ||
import {normalizeTheme} from "./config.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 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
Oops, something went wrong.