import { colors } from 'vuepress/utils' import { logger } from './logger.js' import type { AppendDatePluginOptions } from './options.js' /** * Resolved plugin options * * 解析后的插件选项 */ export interface ResolvedAppendDatePluginOptions { /** * Frontmatter key to use when appending date * * 追加时间时使用的 frontmatter 键名 */ key: string /** * Format of the date value when appending date * * 追加时间时使用的日期格式 */ format: 'date' | 'full' | 'time' } /** * Normalize plugin options * * 归一化插件选项 * * @param options - Plugin options / 插件选项 * @returns Resolved plugin options / 解析后的插件选项 */ export const normalizeAppendDateOptions = ({ key, format = 'date', }: AppendDatePluginOptions): ResolvedAppendDatePluginOptions => { const resolvedKey = key ?? (format === 'time' ? 'time' : 'date') if (format === 'time' && resolvedKey === 'date') { logger.warn( `The ${colors.magenta('key')} option must not be ${colors.magenta( '"date"', )} when ${colors.magenta('format')} is ${colors.magenta( '"time"', )}, falling back to ${colors.magenta('"full"')} format.`, ) return { key: resolvedKey, format: 'full' } } return { key: resolvedKey, format } }