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) {
const ogSlug = simplifySlug(file.data.slug!)
const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!))
let aliases: FullSlug[] = file.data.frontmatter?.aliases ?? file.data.frontmatter?.alias ?? []
if (typeof aliases === "string") {
aliases = [aliases]
}
const aliases = file.data.frontmatter?.aliases ?? []
const slugs: FullSlug[] = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug)
const permalink = file.data.frontmatter?.permalink
if (typeof permalink === "string") {

View File

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

View File

@ -5,17 +5,56 @@ import yaml from "js-yaml"
import toml from "toml"
import { slugTag } from "../../util/path"
import { QuartzPluginData } from "../vfile"
import chalk from "chalk"
export interface Options {
delims: string | string[]
language: "yaml" | "toml"
oneLineTagDelim: string
}
const defaultOptions: Options = {
delims: "---",
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) => {
@ -23,12 +62,11 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
return {
name: "FrontMatter",
markdownPlugins() {
const { oneLineTagDelim } = opts
return [
[remarkFrontmatter, ["yaml", "toml"]],
() => {
return (_, file) => {
const fp = file.data.filePath!
const { data } = matter(Buffer.from(file.value), {
...opts,
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) {
data.title = data.title.toString()
} else if (data.title === null || data.title === undefined) {
data.title = file.stem ?? "Untitled"
}
if (data.tags) {
// coerce to array
if (!Array.isArray(data.tags)) {
data.tags = data.tags
.toString()
.split(oneLineTagDelim)
.map((tag: string) => tag.trim())
}
const tags = coerceToArray(coalesceAliases(data, ["tags", "tag"]))
if (tags) data.tags = [...new Set(tags.map((tag: string) => slugTag(tag)))]
// remove all non-string tags
data.tags = data.tags
.filter((tag: unknown) => typeof tag === "string" || typeof tag === "number")
.map((tag: string | number) => tag.toString())
}
const aliases = coerceToArray(coalesceAliases(data, ["aliases", "alias"]))
if (aliases) data.aliases = aliases
const cssclasses = coerceToArray(coalesceAliases(data, ["cssclasses", "cssclass"]))
if (cssclasses) data.cssclasses = cssclasses
const created = coerceDate(fp, coalesceAliases(data, ["created", "date"]))
// slug them all!!
data.tags = [...new Set(data.tags?.map((tag: string) => slugTag(tag)))]
if (created) data.created = created
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
file.data.frontmatter = data as QuartzPluginData["frontmatter"]
@ -78,9 +110,19 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
declare module "vfile" {
interface DataMap {
frontmatter: { [key: string]: any } & {
frontmatter: { [key: string]: unknown } & {
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"],
}
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> = (
userOpts,
) => {
@ -38,23 +23,21 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
() => {
let repo: Repository | undefined = undefined
return async (_tree, file) => {
let created: MaybeDate = undefined
let modified: MaybeDate = undefined
let published: MaybeDate = undefined
let created: Date | undefined = undefined
let modified: Date | undefined = undefined
let published: Date | undefined = undefined
const fp = file.data.filePath!
const fullFp = path.posix.join(file.cwd, fp)
for (const source of opts.priority) {
if (source === "filesystem") {
const st = await fs.promises.stat(fullFp)
created ||= st.birthtimeMs
modified ||= st.mtimeMs
created ||= new Date(st.birthtimeMs)
modified ||= new Date(st.mtimeMs)
} else if (source === "frontmatter" && file.data.frontmatter) {
created ||= file.data.frontmatter.date
modified ||= file.data.frontmatter.lastmod
modified ||= file.data.frontmatter.updated
modified ||= file.data.frontmatter["last-modified"]
published ||= file.data.frontmatter.publishDate
created ||= file.data.frontmatter.created
modified ||= file.data.frontmatter.modified
published ||= file.data.frontmatter.published
} else if (source === "git") {
if (!repo) {
// Get a reference to the main git repo.
@ -64,7 +47,9 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
}
try {
modified ||= await repo.getFileLatestModifiedDateAsync(file.data.filePath!)
modified ||= new Date(
await repo.getFileLatestModifiedDateAsync(file.data.filePath!),
)
} catch {
console.log(
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 = {
created: coerceDate(fp, created),
modified: coerceDate(fp, modified),
published: coerceDate(fp, published),
created,
modified,
published,
}
}
},

View File

@ -318,7 +318,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
}
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)
}