2023-08-24 22:56:40 +07:00
|
|
|
import { formatDate, getDate } from "./Date"
|
2023-08-09 11:28:09 +07:00
|
|
|
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
|
|
|
import readingTime from "reading-time"
|
2024-01-30 12:51:13 +07:00
|
|
|
import { classNames } from "../util/lang"
|
2024-02-12 01:43:08 +07:00
|
|
|
import { i18n } from "../i18n"
|
2024-03-10 23:57:10 +07:00
|
|
|
import { JSX } from "preact"
|
|
|
|
import style from "./styles/contentMeta.scss"
|
2023-08-09 11:28:09 +07:00
|
|
|
|
2024-01-21 04:18:35 +07:00
|
|
|
interface ContentMetaOptions {
|
|
|
|
/**
|
|
|
|
* Whether to display reading time
|
|
|
|
*/
|
|
|
|
showReadingTime: boolean
|
2024-03-10 23:57:10 +07:00
|
|
|
showComma: boolean
|
2024-01-21 04:18:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions: ContentMetaOptions = {
|
|
|
|
showReadingTime: true,
|
2024-03-10 23:57:10 +07:00
|
|
|
showComma: true,
|
2024-01-21 04:18:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ((opts?: Partial<ContentMetaOptions>) => {
|
|
|
|
// Merge options with defaults
|
|
|
|
const options: ContentMetaOptions = { ...defaultOptions, ...opts }
|
|
|
|
|
2023-10-02 07:20:55 +07:00
|
|
|
function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
|
2023-08-09 11:28:09 +07:00
|
|
|
const text = fileData.text
|
2024-01-21 04:18:35 +07:00
|
|
|
|
2023-08-09 11:28:09 +07:00
|
|
|
if (text) {
|
2024-03-10 23:57:10 +07:00
|
|
|
const segments: (string | JSX.Element)[] = []
|
2023-08-24 22:56:40 +07:00
|
|
|
|
|
|
|
if (fileData.dates) {
|
2024-01-29 13:13:59 +07:00
|
|
|
segments.push(formatDate(getDate(cfg, fileData)!, cfg.locale))
|
2023-08-09 11:28:09 +07:00
|
|
|
}
|
|
|
|
|
2024-01-21 04:18:35 +07:00
|
|
|
// Display reading time if enabled
|
|
|
|
if (options.showReadingTime) {
|
2024-02-12 01:43:08 +07:00
|
|
|
const { minutes, words: _words } = readingTime(text)
|
|
|
|
const displayedTime = i18n(cfg.locale).components.contentMeta.readingTime({
|
|
|
|
minutes: Math.ceil(minutes),
|
|
|
|
})
|
|
|
|
segments.push(displayedTime)
|
2024-01-21 04:18:35 +07:00
|
|
|
}
|
|
|
|
|
2024-03-10 23:57:10 +07:00
|
|
|
const segmentsElements = segments.map((segment) => <span>{segment}</span>)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<p show-comma={options.showComma} class={classNames(displayClass, "content-meta")}>
|
|
|
|
{segmentsElements}
|
|
|
|
</p>
|
|
|
|
)
|
2023-08-09 11:28:09 +07:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 23:57:10 +07:00
|
|
|
ContentMetadata.css = style
|
|
|
|
|
2023-08-09 11:28:09 +07:00
|
|
|
return ContentMetadata
|
|
|
|
}) satisfies QuartzComponentConstructor
|