handle dates as tags

This commit is contained in:
Jacky Zhao 2023-07-06 18:45:38 -07:00
parent d3a52331ac
commit f5847d7eb2
4 changed files with 56 additions and 23 deletions

46
package-lock.json generated
View File

@ -23,6 +23,7 @@
"hast-util-to-jsx-runtime": "^1.2.0", "hast-util-to-jsx-runtime": "^1.2.0",
"hast-util-to-string": "^2.0.0", "hast-util-to-string": "^2.0.0",
"is-absolute-url": "^4.0.1", "is-absolute-url": "^4.0.1",
"js-yaml": "^4.1.0",
"mdast-util-find-and-replace": "^2.2.2", "mdast-util-find-and-replace": "^2.2.2",
"mdast-util-to-string": "^3.2.0", "mdast-util-to-string": "^3.2.0",
"micromorph": "^0.4.5", "micromorph": "^0.4.5",
@ -60,6 +61,7 @@
"@types/d3": "^7.4.0", "@types/d3": "^7.4.0",
"@types/flexsearch": "^0.7.3", "@types/flexsearch": "^0.7.3",
"@types/hast": "^2.3.4", "@types/hast": "^2.3.4",
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.1.2", "@types/node": "^20.1.2",
"@types/pretty-time": "^1.1.2", "@types/pretty-time": "^1.1.2",
"@types/serve-handler": "^6.1.1", "@types/serve-handler": "^6.1.1",
@ -983,6 +985,12 @@
"@types/unist": "*" "@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": { "node_modules/@types/katex": {
"version": "0.14.0", "version": "0.14.0",
"resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.14.0.tgz", "resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.14.0.tgz",
@ -1108,12 +1116,9 @@
} }
}, },
"node_modules/argparse": { "node_modules/argparse": {
"version": "1.0.10", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
"dependencies": {
"sprintf-js": "~1.0.2"
}
}, },
"node_modules/array-iterate": { "node_modules/array-iterate": {
"version": "2.0.1", "version": "2.0.1",
@ -2091,6 +2096,26 @@
"node": ">=6.0" "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": { "node_modules/has": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
@ -2539,12 +2564,11 @@
} }
}, },
"node_modules/js-yaml": { "node_modules/js-yaml": {
"version": "3.14.1", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"dependencies": { "dependencies": {
"argparse": "^1.0.7", "argparse": "^2.0.1"
"esprima": "^4.0.0"
}, },
"bin": { "bin": {
"js-yaml": "bin/js-yaml.js" "js-yaml": "bin/js-yaml.js"

View File

@ -40,6 +40,7 @@
"hast-util-to-jsx-runtime": "^1.2.0", "hast-util-to-jsx-runtime": "^1.2.0",
"hast-util-to-string": "^2.0.0", "hast-util-to-string": "^2.0.0",
"is-absolute-url": "^4.0.1", "is-absolute-url": "^4.0.1",
"js-yaml": "^4.1.0",
"mdast-util-find-and-replace": "^2.2.2", "mdast-util-find-and-replace": "^2.2.2",
"mdast-util-to-string": "^3.2.0", "mdast-util-to-string": "^3.2.0",
"micromorph": "^0.4.5", "micromorph": "^0.4.5",
@ -74,6 +75,7 @@
"@types/d3": "^7.4.0", "@types/d3": "^7.4.0",
"@types/flexsearch": "^0.7.3", "@types/flexsearch": "^0.7.3",
"@types/hast": "^2.3.4", "@types/hast": "^2.3.4",
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.1.2", "@types/node": "^20.1.2",
"@types/pretty-time": "^1.1.2", "@types/pretty-time": "^1.1.2",
"@types/serve-handler": "^6.1.1", "@types/serve-handler": "^6.1.1",

View File

@ -1,6 +1,7 @@
import matter from "gray-matter" import matter from "gray-matter"
import remarkFrontmatter from 'remark-frontmatter' import remarkFrontmatter from 'remark-frontmatter'
import { QuartzTransformerPlugin } from "../types" import { QuartzTransformerPlugin } from "../types"
import yaml from 'js-yaml'
export interface Options { export interface Options {
language: 'yaml' | 'toml', language: 'yaml' | 'toml',
@ -21,10 +22,15 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
remarkFrontmatter, remarkFrontmatter,
() => { () => {
return (_, file) => { 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') { if (data.tags && !Array.isArray(data.tags)) {
data.tags = data.tags.split(" ") data.tags = data.tags.toString().split(" ")
} }
// fill in frontmatter // fill in frontmatter

View File

@ -11,6 +11,7 @@ const defaultOptions: Options = {
priority: ['frontmatter', 'git', 'filesystem'] priority: ['frontmatter', 'git', 'filesystem']
} }
type MaybeDate = undefined | string | number
export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => { export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
const opts = { ...defaultOptions, ...userOpts } const opts = { ...defaultOptions, ...userOpts }
return { return {
@ -20,16 +21,16 @@ 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: undefined | Date = undefined let created: MaybeDate = undefined
let modified: undefined | Date = undefined let modified: MaybeDate = undefined
let published: undefined | Date = undefined let published: MaybeDate = undefined
const fp = path.join(file.cwd, file.data.filePath as string) const fp = path.join(file.cwd, file.data.filePath as string)
for (const source of opts.priority) { for (const source of opts.priority) {
if (source === "filesystem") { if (source === "filesystem") {
const st = await fs.promises.stat(fp) const st = await fs.promises.stat(fp)
created ||= new Date(st.birthtimeMs) created ||= st.birthtimeMs
modified ||= new Date(st.mtimeMs) modified ||= 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.date
modified ||= file.data.frontmatter.lastmod modified ||= file.data.frontmatter.lastmod
@ -40,14 +41,14 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options> | und
repo = new Repository(file.cwd) repo = new Repository(file.cwd)
} }
modified ||= new Date(await repo.getFileLatestModifiedDateAsync(file.data.filePath!)) modified ||= await repo.getFileLatestModifiedDateAsync(file.data.filePath!)
} }
} }
file.data.dates = { file.data.dates = {
created: created ?? new Date(), created: created ? new Date(created) : new Date(),
modified: modified ?? new Date(), modified: modified ? new Date(modified) : new Date(),
published: published ?? new Date() published: published ? new Date(published) : new Date(),
} }
} }
} }