wiki/quartz/components/ContentMeta.tsx

61 lines
1.6 KiB
TypeScript
Raw Normal View History

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"
import { classNames } from "../util/lang"
import { i18n } from "../i18n"
import { JSX } from "preact"
import style from "./styles/contentMeta.scss"
2023-08-09 04:28:09 +00:00
interface ContentMetaOptions {
/**
* Whether to display reading time
*/
showReadingTime: boolean
showComma: boolean
}
const defaultOptions: ContentMetaOptions = {
showReadingTime: true,
showComma: true,
}
export default ((opts?: Partial<ContentMetaOptions>) => {
// Merge options with defaults
const options: ContentMetaOptions = { ...defaultOptions, ...opts }
function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
2023-08-09 04:28:09 +00:00
const text = fileData.text
2023-08-09 04:28:09 +00:00
if (text) {
const segments: (string | JSX.Element)[] = []
2023-08-24 15:56:40 +00:00
if (fileData.dates) {
segments.push(formatDate(getDate(cfg, fileData)!, cfg.locale))
2023-08-09 04:28:09 +00:00
}
// Display reading time if enabled
if (options.showReadingTime) {
const { minutes, words: _words } = readingTime(text)
const displayedTime = i18n(cfg.locale).components.contentMeta.readingTime({
minutes: Math.ceil(minutes),
})
segments.push(displayedTime)
}
const segmentsElements = segments.map((segment) => <span>{segment}</span>)
return (
<p show-comma={options.showComma} class={classNames(displayClass, "content-meta")}>
{segmentsElements}
</p>
)
2023-08-09 04:28:09 +00:00
} else {
return null
}
}
ContentMetadata.css = style
2023-08-09 04:28:09 +00:00
return ContentMetadata
}) satisfies QuartzComponentConstructor