74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { Date as DateComp, getDate } from "./Date"
|
||
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"
|
||
|
||
interface ContentMetaOptions {
|
||
/**
|
||
* Whether to display reading time
|
||
*/
|
||
showReadingTime: boolean
|
||
showComma: boolean
|
||
}
|
||
|
||
const defaultOptions: ContentMetaOptions = {
|
||
showReadingTime: true,
|
||
showComma: true,
|
||
}
|
||
|
||
export default ((opts?: Partial<ContentMetaOptions>) => {
|
||
// Объединяем пользовательские опции с дефолтными
|
||
const options: ContentMetaOptions = { ...defaultOptions, ...opts }
|
||
|
||
function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
|
||
const text = fileData.text
|
||
|
||
if (text) {
|
||
const segments: JSX.Element[] = []
|
||
|
||
if (fileData.dates) {
|
||
// Отображаем дату обновления и создания, как в твоей версии
|
||
if (fileData.dates.modified) {
|
||
segments.push(
|
||
<span>
|
||
Обновлена:{" "}
|
||
<DateComp date={fileData.dates.modified} locale={cfg.locale} />
|
||
</span>,
|
||
)
|
||
}
|
||
if (fileData.dates.created) {
|
||
segments.push(
|
||
<span>
|
||
Создана:{" "}
|
||
<DateComp date={fileData.dates.created} locale={cfg.locale} />
|
||
</span>,
|
||
)
|
||
}
|
||
}
|
||
|
||
// Отображаем время чтения, как у автора
|
||
if (options.showReadingTime) {
|
||
const { minutes } = readingTime(text)
|
||
const displayedTime = i18n(cfg.locale).components.contentMeta.readingTime({
|
||
minutes: Math.ceil(minutes),
|
||
})
|
||
segments.push(<span>{displayedTime}</span>)
|
||
}
|
||
|
||
return (
|
||
<p show-comma={options.showComma} class={classNames(displayClass, "content-meta")}>
|
||
{segments}
|
||
</p>
|
||
)
|
||
} else {
|
||
return null
|
||
}
|
||
}
|
||
|
||
ContentMetadata.css = style
|
||
|
||
return ContentMetadata
|
||
}) satisfies QuartzComponentConstructor |