2023-08-24 15:56:40 +00:00
|
|
|
import { formatDate, getDate } from "./Date"
|
2023-08-09 04:28:09 +00:00
|
|
|
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
|
|
|
import readingTime from "reading-time"
|
2024-02-05 10:45:36 +00:00
|
|
|
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 }
|
2023-08-09 04:28:09 +00:00
|
|
|
|
2023-10-02 00:20:55 +00:00
|
|
|
function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
|
2023-08-09 04:28:09 +00:00
|
|
|
const text = fileData.text
|
2024-02-05 10:45:36 +00:00
|
|
|
|
2023-08-09 04:28:09 +00:00
|
|
|
if (text) {
|
|
|
|
const segments: string[] = []
|
2023-08-24 15:56:40 +00:00
|
|
|
|
2024-02-08 06:53:17 +00:00
|
|
|
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))
|
2024-02-05 10:45:36 +00:00
|
|
|
}
|
|
|
|
// Display reading time if enabled
|
|
|
|
if (options.showReadingTime) {
|
|
|
|
const { text: timeTaken, words: _words } = readingTime(text)
|
|
|
|
segments.push(timeTaken)
|
2023-08-09 04:28:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 10:45:36 +00:00
|
|
|
return <p class={classNames(displayClass, "content-meta")}>{segments.join(", ")}</p>
|
2023-08-09 04:28:09 +00:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentMetadata.css = `
|
|
|
|
.content-meta {
|
|
|
|
margin-top: 0;
|
|
|
|
color: var(--gray);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
return ContentMetadata
|
|
|
|
}) satisfies QuartzComponentConstructor
|