fix: generalize frontmatter parsing and coercing

This commit is contained in:
Jacky Zhao 2024-01-27 21:39:16 -08:00
parent b211d49922
commit 42ee069c1c
5 changed files with 90 additions and 70 deletions

View file

@ -15,12 +15,7 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
for (const [_tree, file] of content) { for (const [_tree, file] of content) {
const ogSlug = simplifySlug(file.data.slug!) const ogSlug = simplifySlug(file.data.slug!)
const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!)) const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!))
const aliases = file.data.frontmatter?.aliases ?? []
let aliases: FullSlug[] = file.data.frontmatter?.aliases ?? file.data.frontmatter?.alias ?? []
if (typeof aliases === "string") {
aliases = [aliases]
}
const slugs: FullSlug[] = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug) const slugs: FullSlug[] = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug)
const permalink = file.data.frontmatter?.permalink const permalink = file.data.frontmatter?.permalink
if (typeof permalink === "string") { if (typeof permalink === "string") {

View file

@ -3,11 +3,6 @@ import { QuartzFilterPlugin } from "../types"
export const ExplicitPublish: QuartzFilterPlugin = () => ({ export const ExplicitPublish: QuartzFilterPlugin = () => ({
name: "ExplicitPublish", name: "ExplicitPublish",
shouldPublish(_ctx, [_tree, vfile]) { shouldPublish(_ctx, [_tree, vfile]) {
const publishProperty = vfile.data?.frontmatter?.publish ?? false return vfile.data?.frontmatter?.publish ?? false
const publishFlag =
typeof publishProperty === "string"
? publishProperty.toLowerCase() === "true"
: Boolean(publishProperty)
return publishFlag
}, },
}) })

View file

@ -5,17 +5,56 @@ import yaml from "js-yaml"
import toml from "toml" import toml from "toml"
import { slugTag } from "../../util/path" import { slugTag } from "../../util/path"
import { QuartzPluginData } from "../vfile" import { QuartzPluginData } from "../vfile"
import chalk from "chalk"
export interface Options { export interface Options {
delims: string | string[] delims: string | string[]
language: "yaml" | "toml" language: "yaml" | "toml"
oneLineTagDelim: string
} }
const defaultOptions: Options = { const defaultOptions: Options = {
delims: "---", delims: "---",
language: "yaml", language: "yaml",
oneLineTagDelim: ",", }
function coerceDate(fp: string, d: unknown): Date | undefined {
if (d === undefined || d === null) return undefined
const dt = new Date(d as string | number)
const invalidDate = isNaN(dt.getTime()) || dt.getTime() === 0
if (invalidDate) {
console.log(
chalk.yellow(
`\nWarning: found invalid date "${d}" in \`${fp}\`. Supported formats: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format`,
),
)
return undefined
}
return dt
}
function coalesceAliases(data: { [key: string]: any }, aliases: string[]) {
for (const alias of aliases) {
if (data[alias] !== undefined && data[alias] !== null) return data[alias]
}
}
function coerceToArray(input: string | string[]): string[] | undefined {
if (input === undefined || input === null) return undefined
// coerce to array
if (!Array.isArray(input)) {
input = input
.toString()
.split(",")
.map((tag: string) => tag.trim())
}
// remove all non-strings
return input
.filter((tag: unknown) => typeof tag === "string" || typeof tag === "number")
.map((tag: string | number) => tag.toString())
} }
export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => { export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
@ -23,12 +62,11 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
return { return {
name: "FrontMatter", name: "FrontMatter",
markdownPlugins() { markdownPlugins() {
const { oneLineTagDelim } = opts
return [ return [
[remarkFrontmatter, ["yaml", "toml"]], [remarkFrontmatter, ["yaml", "toml"]],
() => { () => {
return (_, file) => { return (_, file) => {
const fp = file.data.filePath!
const { data } = matter(Buffer.from(file.value), { const { data } = matter(Buffer.from(file.value), {
...opts, ...opts,
engines: { engines: {
@ -37,35 +75,29 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
}, },
}) })
// tag is an alias for tags
if (data.tag) {
data.tags = data.tag
}
// coerce title to string
if (data.title) { if (data.title) {
data.title = data.title.toString() data.title = data.title.toString()
} else if (data.title === null || data.title === undefined) { } else if (data.title === null || data.title === undefined) {
data.title = file.stem ?? "Untitled" data.title = file.stem ?? "Untitled"
} }
if (data.tags) { const tags = coerceToArray(coalesceAliases(data, ["tags", "tag"]))
// coerce to array if (tags) data.tags = [...new Set(tags.map((tag: string) => slugTag(tag)))]
if (!Array.isArray(data.tags)) {
data.tags = data.tags
.toString()
.split(oneLineTagDelim)
.map((tag: string) => tag.trim())
}
// remove all non-string tags const aliases = coerceToArray(coalesceAliases(data, ["aliases", "alias"]))
data.tags = data.tags if (aliases) data.aliases = aliases
.filter((tag: unknown) => typeof tag === "string" || typeof tag === "number") const cssclasses = coerceToArray(coalesceAliases(data, ["cssclasses", "cssclass"]))
.map((tag: string | number) => tag.toString()) if (cssclasses) data.cssclasses = cssclasses
} const created = coerceDate(fp, coalesceAliases(data, ["created", "date"]))
// slug them all!! if (created) data.created = created
data.tags = [...new Set(data.tags?.map((tag: string) => slugTag(tag)))] const modified = coerceDate(
fp,
coalesceAliases(data, ["modified", "lastmod", "updated", "last-modified"]),
)
if (modified) data.modified = modified
const published = coerceDate(fp, coalesceAliases(data, ["published", "publishDate"]))
if (published) data.published = published
// fill in frontmatter // fill in frontmatter
file.data.frontmatter = data as QuartzPluginData["frontmatter"] file.data.frontmatter = data as QuartzPluginData["frontmatter"]
@ -78,9 +110,19 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
declare module "vfile" { declare module "vfile" {
interface DataMap { interface DataMap {
frontmatter: { [key: string]: any } & { frontmatter: { [key: string]: unknown } & {
title: string title: string
tags: string[] } & Partial<{
} tags: string[]
aliases: string[]
description: string
publish: boolean
draft: boolean
enableToc: string
cssclasses: string[]
created: Date
modified: Date
published: Date
}>
} }
} }

View file

@ -12,21 +12,6 @@ const defaultOptions: Options = {
priority: ["frontmatter", "git", "filesystem"], priority: ["frontmatter", "git", "filesystem"],
} }
function coerceDate(fp: string, d: any): Date {
const dt = new Date(d)
const invalidDate = isNaN(dt.getTime()) || dt.getTime() === 0
if (invalidDate && d !== undefined) {
console.log(
chalk.yellow(
`\nWarning: found invalid date "${d}" in \`${fp}\`. Supported formats: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format`,
),
)
}
return invalidDate ? new Date() : dt
}
type MaybeDate = undefined | string | number
export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | undefined> = ( export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | undefined> = (
userOpts, userOpts,
) => { ) => {
@ -38,23 +23,21 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
() => { () => {
let repo: Repository | undefined = undefined let repo: Repository | undefined = undefined
return async (_tree, file) => { return async (_tree, file) => {
let created: MaybeDate = undefined let created: Date | undefined = undefined
let modified: MaybeDate = undefined let modified: Date | undefined = undefined
let published: MaybeDate = undefined let published: Date | undefined = undefined
const fp = file.data.filePath! const fp = file.data.filePath!
const fullFp = path.posix.join(file.cwd, fp) const fullFp = path.posix.join(file.cwd, fp)
for (const source of opts.priority) { for (const source of opts.priority) {
if (source === "filesystem") { if (source === "filesystem") {
const st = await fs.promises.stat(fullFp) const st = await fs.promises.stat(fullFp)
created ||= st.birthtimeMs created ||= new Date(st.birthtimeMs)
modified ||= st.mtimeMs modified ||= new Date(st.mtimeMs)
} else if (source === "frontmatter" && file.data.frontmatter) { } else if (source === "frontmatter" && file.data.frontmatter) {
created ||= file.data.frontmatter.date created ||= file.data.frontmatter.created
modified ||= file.data.frontmatter.lastmod modified ||= file.data.frontmatter.modified
modified ||= file.data.frontmatter.updated published ||= file.data.frontmatter.published
modified ||= file.data.frontmatter["last-modified"]
published ||= file.data.frontmatter.publishDate
} else if (source === "git") { } else if (source === "git") {
if (!repo) { if (!repo) {
// Get a reference to the main git repo. // Get a reference to the main git repo.
@ -64,7 +47,9 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
} }
try { try {
modified ||= await repo.getFileLatestModifiedDateAsync(file.data.filePath!) modified ||= new Date(
await repo.getFileLatestModifiedDateAsync(file.data.filePath!),
)
} catch { } catch {
console.log( console.log(
chalk.yellow( chalk.yellow(
@ -76,10 +61,13 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
} }
} }
created ||= new Date()
modified ||= new Date()
published ||= new Date()
file.data.dates = { file.data.dates = {
created: coerceDate(fp, created), created,
modified: coerceDate(fp, modified), modified,
published: coerceDate(fp, published), published,
} }
} }
}, },

View file

@ -318,7 +318,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
} }
tag = slugTag(tag) tag = slugTag(tag)
if (file.data.frontmatter && !file.data.frontmatter.tags.includes(tag)) { if (file.data.frontmatter?.tags?.includes(tag)) {
file.data.frontmatter.tags.push(tag) file.data.frontmatter.tags.push(tag)
} }