quartz-research-note/quartz/components/ContentMeta.tsx
松浦 知也 Matsuura Tomoya 490679e0d2
All checks were successful
Build / build (push) Successful in 1m54s
[obsidian] vault backup: 2024-02-08 15:53:17[
2024-02-08 15:53:17 +09:00

53 lines
1.4 KiB
TypeScript

import { formatDate, getDate } from "./Date"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
import readingTime from "reading-time"
import { classNames } from "../util/lang"
interface ContentMetaOptions {
/**
* Whether to display reading time
*/
showReadingTime: boolean
}
const defaultOptions: ContentMetaOptions = {
showReadingTime: true,
}
export default ((opts?: Partial<ContentMetaOptions>) => {
// Merge options with defaults
const options: ContentMetaOptions = { ...defaultOptions, ...opts }
function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
const text = fileData.text
if (text) {
const segments: string[] = []
if (fileData.dates?.created) {
segments.push('created: ' + formatDate(fileData.dates.created, cfg.locale))
}
if (fileData.dates?.modified) {
segments.push('last updated: ' + formatDate(fileData.dates.modified, cfg.locale))
}
// Display reading time if enabled
if (options.showReadingTime) {
const { text: timeTaken, words: _words } = readingTime(text)
segments.push(timeTaken)
}
return <p class={classNames(displayClass, "content-meta")}>{segments.join(", ")}</p>
} else {
return null
}
}
ContentMetadata.css = `
.content-meta {
margin-top: 0;
color: var(--gray);
}
`
return ContentMetadata
}) satisfies QuartzComponentConstructor