添加最后修改时间
学习如何编写一个在你的 Markdown 或 MDX 文件的 frontmatter 中添加最后修改时间的 remark 插件。使用这个属性在你的页面中显示最后修改时间。
操作步骤
段落标题 操作步骤-
安装辅助包
安装用于修改和格式化时间的
Day.js
:终端窗口 npm install dayjs终端窗口 pnpm install dayjs终端窗口 yarn add dayjs -
创建一个 remark 插件
这个插件使用
execSync
运行一个 Git 命令获取最后一次提交的 ISO 8601 时间戳,然后将时间戳添加到文件的 frontmatter 中。remark-modified-time.mjs import { execSync } from "child_process";export function remarkModifiedTime() {return function (tree, file) {const filepath = file.history[0];const result = execSync(`git log -1 --pretty="format:%cI" ${filepath}`);file.data.astro.frontmatter.lastModified = result.toString();};}使用文件系统而不是 Git
虽然使用 Git 是获取文件的最后修改时间的推荐方式,但使用文件系统的修改时间也是可能的。 这个插件使用
statSync
获取文件的 ISO 8601 格式的mtime
(修改时间),然后将时间戳添加到文件的 frontmatter 中。remark-modified-time.mjs import { statSync } from "fs";export function remarkModifiedTime() {return function (tree, file) {const filepath = file.history[0];const result = statSync(filepath);file.data.astro.frontmatter.lastModified = result.mtime.toISOString();};} -
将插件添加到你的配置中
astro.config.mjs import { defineConfig } from 'astro/config';import { remarkModifiedTime } from './remark-modified-time.mjs';export default defineConfig({markdown: {remarkPlugins: [remarkModifiedTime],},});现在所有的 Markdown 文档的 frontmatter 中都会有一个
lastModified
属性。 -
显示最后修改时间
如果你的博客文章存储在内容集合中,通过
entry.render()
函数获取remarkPluginFrontmatter
,然后在模板中你喜欢的位置渲染lastModified
。src/pages/posts/[slug].astro ---import { CollectionEntry, getCollection } from 'astro:content';import dayjs from "dayjs";import utc from "dayjs/plugin/utc";dayjs.extend(utc);export async function getStaticPaths() {const blog = await getCollection('blog');return blog.map(entry => ({params: { slug: entry.slug },props: { entry },}));}const { entry } = Astro.props;const { Content, remarkPluginFrontmatter } = await entry.render();const lastModified = dayjs(remarkPluginFrontmatter.lastModified).utc().format("HH:mm:ss DD MMMM YYYY UTC");---<html><head>...</head><body>...<p>Last Modified: {lastModified}</p>...</body></html>如果你使用的是 Markdown 布局,在布局模板中通过
Astro.props
来获取 frontmatter 中的lastModified
属性。src/layouts/BlogLayout.astro ---import dayjs from "dayjs";import utc from "dayjs/plugin/utc";dayjs.extend(utc);const lastModified = dayjs().utc(Astro.props.frontmatter.lastModified).format("HH:mm:ss DD MMMM YYYY UTC");---<html><head>...</head><body><p>{lastModified}</p><slot /></body></html>
更多操作指南
-
状态共享
学习如何使用 Nano Stores 在框架组件之间共享状态
-
添加 RSS 摘要
通过向你的 Astro 网站添加 RSS 摘要让用户来订阅你的内容。
-
安装一个 Vite 或 Rollup 插件
了解如何通过向项目添加 Rollup 插件来导入 YAML 数据。
-
构建自定义图像组件
了解如何使用 getImage 函数构建支持媒体查询的自定义图像组件
-
使用 API 路由构建表单
了解如何使用 JavaScript 发送表单到 API 路由
-
在 Astro 页面中构建 HTML 表单
了解如何在 Astro 页面中构建 HTML 表单并在你的 frontmatter 中处理提交请求
-
在 Astro 中使用 Bun
了解如何在 Astro 站点中使用 Bun。
-
调用服务器端点
了解如何在 Astro 中调用服务器端点
-
校验验证码
了解如何创建一个 API 路由并从客户端进行获取。
-
用 Docker 来构建你的 Astro 网站
了解如何使用 Docker 来构建你的 Astro 网站。
-
Dynamically Import Images
Learn how to dynamically import images using Vite's import.meta.glob function
-
为链接添加图标
了解如何安装 rehype 插件并为你 Markdown 文件中的链接添加图标
-
添加 i18n 功能
通过动态路由和内容集合来让你的 Astro 站点支持国际化。
-
添加最后修改时间
编写一个在你的 Markdown 和 MDX 文件里添加最后修改时间的 remark 插件。
-
添加阅读时间
编写一个在你的 Markdown 和 MDX 文件里添加阅读所需时间的 remark 插件。
-
在 Astro 组件中共享状态
了解如何通过 Nano Stores 在 Astro 组件中共享状态
-
使用流式处理来提升页面性能
了解如何使用流式处理来提升页面性能
-
使用 Tailwind Typography 美化渲染后的 Markdown
了解如何使用 @tailwind/typography 美化你渲染后的 Markdown