From 9cbacca2d4062e2b2e512f761ab580218c623b33 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Thu, 6 Jul 2023 18:45:38 -0700 Subject: [PATCH] handle dates as tags --- package-lock.json | 46 ++++++++++++++++------ package.json | 2 + quartz/plugins/transformers/frontmatter.ts | 12 ++++-- quartz/plugins/transformers/lastmod.ts | 19 ++++----- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6e8f7716..9a9769f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "hast-util-to-jsx-runtime": "^1.2.0", "hast-util-to-string": "^2.0.0", "is-absolute-url": "^4.0.1", + "js-yaml": "^4.1.0", "mdast-util-find-and-replace": "^2.2.2", "mdast-util-to-string": "^3.2.0", "micromorph": "^0.4.5", @@ -60,6 +61,7 @@ "@types/d3": "^7.4.0", "@types/flexsearch": "^0.7.3", "@types/hast": "^2.3.4", + "@types/js-yaml": "^4.0.5", "@types/node": "^20.1.2", "@types/pretty-time": "^1.1.2", "@types/serve-handler": "^6.1.1", @@ -983,6 +985,12 @@ "@types/unist": "*" } }, + "node_modules/@types/js-yaml": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.5.tgz", + "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", + "dev": true + }, "node_modules/@types/katex": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.14.0.tgz", @@ -1108,12 +1116,9 @@ } }, "node_modules/argparse": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", - "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dependencies": { - "sprintf-js": "~1.0.2" - } + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "node_modules/array-iterate": { "version": "2.0.1", @@ -2091,6 +2096,26 @@ "node": ">=6.0" } }, + "node_modules/gray-matter/node_modules/argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "dependencies": { + "sprintf-js": "~1.0.2" + } + }, + "node_modules/gray-matter/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -2539,12 +2564,11 @@ } }, "node_modules/js-yaml": { - "version": "3.14.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", - "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", "dependencies": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" + "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" diff --git a/package.json b/package.json index f6e78f8c..e045d2de 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "hast-util-to-jsx-runtime": "^1.2.0", "hast-util-to-string": "^2.0.0", "is-absolute-url": "^4.0.1", + "js-yaml": "^4.1.0", "mdast-util-find-and-replace": "^2.2.2", "mdast-util-to-string": "^3.2.0", "micromorph": "^0.4.5", @@ -74,6 +75,7 @@ "@types/d3": "^7.4.0", "@types/flexsearch": "^0.7.3", "@types/hast": "^2.3.4", + "@types/js-yaml": "^4.0.5", "@types/node": "^20.1.2", "@types/pretty-time": "^1.1.2", "@types/serve-handler": "^6.1.1", diff --git a/quartz/plugins/transformers/frontmatter.ts b/quartz/plugins/transformers/frontmatter.ts index 01f9608b..660523b2 100644 --- a/quartz/plugins/transformers/frontmatter.ts +++ b/quartz/plugins/transformers/frontmatter.ts @@ -1,6 +1,7 @@ import matter from "gray-matter" import remarkFrontmatter from 'remark-frontmatter' import { QuartzTransformerPlugin } from "../types" +import yaml from 'js-yaml' export interface Options { language: 'yaml' | 'toml', @@ -21,10 +22,15 @@ export const FrontMatter: QuartzTransformerPlugin | undefined> remarkFrontmatter, () => { return (_, file) => { - const { data } = matter(file.value, opts) + const { data } = matter(file.value, { + ...opts, + engines: { + yaml: s => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object + } + }) - if (typeof data.tags === 'string') { - data.tags = data.tags.split(" ") + if (data.tags && !Array.isArray(data.tags)) { + data.tags = data.tags.toString().split(" ") } // fill in frontmatter diff --git a/quartz/plugins/transformers/lastmod.ts b/quartz/plugins/transformers/lastmod.ts index f3a8904a..811fd476 100644 --- a/quartz/plugins/transformers/lastmod.ts +++ b/quartz/plugins/transformers/lastmod.ts @@ -11,6 +11,7 @@ const defaultOptions: Options = { priority: ['frontmatter', 'git', 'filesystem'] } +type MaybeDate = undefined | string | number export const CreatedModifiedDate: QuartzTransformerPlugin | undefined> = (userOpts) => { const opts = { ...defaultOptions, ...userOpts } return { @@ -20,16 +21,16 @@ export const CreatedModifiedDate: QuartzTransformerPlugin | und () => { let repo: Repository | undefined = undefined return async (_tree, file) => { - let created: undefined | Date = undefined - let modified: undefined | Date = undefined - let published: undefined | Date = undefined + let created: MaybeDate = undefined + let modified: MaybeDate = undefined + let published: MaybeDate = undefined const fp = path.join(file.cwd, file.data.filePath as string) for (const source of opts.priority) { if (source === "filesystem") { const st = await fs.promises.stat(fp) - created ||= new Date(st.birthtimeMs) - modified ||= new Date(st.mtimeMs) + created ||= st.birthtimeMs + modified ||= st.mtimeMs } else if (source === "frontmatter" && file.data.frontmatter) { created ||= file.data.frontmatter.date modified ||= file.data.frontmatter.lastmod @@ -40,14 +41,14 @@ export const CreatedModifiedDate: QuartzTransformerPlugin | und repo = new Repository(file.cwd) } - modified ||= new Date(await repo.getFileLatestModifiedDateAsync(file.data.filePath!)) + modified ||= await repo.getFileLatestModifiedDateAsync(file.data.filePath!) } } file.data.dates = { - created: created ?? new Date(), - modified: modified ?? new Date(), - published: published ?? new Date() + created: created ? new Date(created) : new Date(), + modified: modified ? new Date(modified) : new Date(), + published: published ? new Date(published) : new Date(), } } }