mirror of
				https://github.com/zopiya/x-eden-quartz.git
				synced 2025-11-04 06:46:48 +08:00 
			
		
		
		
	Initial commit
This commit is contained in:
		
							
								
								
									
										60
									
								
								quartz/components/ContentMeta.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								quartz/components/ContentMeta.tsx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
import { formatDate, 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>) => {
 | 
			
		||||
  // Merge options with defaults
 | 
			
		||||
  const options: ContentMetaOptions = { ...defaultOptions, ...opts }
 | 
			
		||||
 | 
			
		||||
  function ContentMetadata({ cfg, fileData, displayClass }: QuartzComponentProps) {
 | 
			
		||||
    const text = fileData.text
 | 
			
		||||
 | 
			
		||||
    if (text) {
 | 
			
		||||
      const segments: (string | JSX.Element)[] = []
 | 
			
		||||
 | 
			
		||||
      if (fileData.dates) {
 | 
			
		||||
        segments.push(formatDate(getDate(cfg, fileData)!, cfg.locale))
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // 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>
 | 
			
		||||
      )
 | 
			
		||||
    } else {
 | 
			
		||||
      return null
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ContentMetadata.css = style
 | 
			
		||||
 | 
			
		||||
  return ContentMetadata
 | 
			
		||||
}) satisfies QuartzComponentConstructor
 | 
			
		||||
		Reference in New Issue
	
	Block a user