@kawapress/plugin-last-updated 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +155 -2
- package/README.zh-CN.md +156 -0
- package/package.json +49 -3
- package/src/components/LastUpdated.vue +81 -0
- package/src/edit-link.ts +36 -0
- package/src/format.ts +90 -0
- package/src/git.ts +23 -0
- package/src/index.ts +9 -0
- package/src/labels.ts +57 -0
- package/src/last-updated.css +33 -0
- package/src/name.ts +2 -0
- package/src/options-module.ts +60 -0
- package/src/plugin.ts +81 -0
- package/src/runtime-options.ts +8 -0
- package/src/runtime-plugin.ts +11 -0
- package/src/types.ts +51 -0
- package/src/virtual-modules.d.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) KawaPress contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo-dark.png">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo.png" alt="KawaPress" width="180">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
<h1 align="center">KawaPress</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>A Vue-first static site generator for documentation and content-focused websites.</strong>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
Write in Markdown. Develop with real SSR. Extend everything through public APIs.
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm version"></a>
|
|
20
|
+
<a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="documentation build"></a>
|
|
21
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT license"></a>
|
|
22
|
+
<img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 or newer">
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://shenqingchuan.github.io/kawapress/en/">Documentation</a>
|
|
27
|
+
·
|
|
28
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.zh-CN.md">简体中文</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
If you run into any problems while using KawaPress, please [open an issue](https://github.com/ShenQingchuan/kawapress/issues). Your feedback helps shape the project during its early development.
|
|
32
|
+
|
|
33
|
+
## Why KawaPress?
|
|
34
|
+
|
|
35
|
+
KawaPress turns Markdown into pre-rendered Vue websites. It keeps the familiar simplicity of a documentation generator while giving themes and plugins the same public extension points used by the official experience.
|
|
36
|
+
|
|
37
|
+
- **Real SSR during development** — every initial request is server-rendered, so SSR-only problems appear before build or deployment.
|
|
38
|
+
- **A complete default experience** — the built-in nagi (凪) preset includes responsive documentation layouts, dark mode, local search, syntax highlighting, Twoslash, code groups, multilingual navigation, and more.
|
|
39
|
+
- **Vue from end to end** — Markdown becomes Vue components, Vue Router handles navigation, and regular Vue SFCs fit naturally into content.
|
|
40
|
+
- **AI-ready output** — nagi generates page-level Markdown, locale-aware `llms.txt` and `llms-full.txt`, plus built-in copy actions.
|
|
41
|
+
- **One extension model** — Generator Plugins, Runtime Plugins, and Presets cover build-time behavior, the Vue runtime, themes, and reusable product setups without private theme hooks.
|
|
42
|
+
|
|
43
|
+
KawaPress is an independent project. It is not a VitePress fork or a drop-in replacement, and it intentionally does not use VitePress configuration.
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
You need [Node.js](https://nodejs.org/) 22.12 or newer.
|
|
48
|
+
|
|
49
|
+
Create a new site with the scaffolder:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm create kawapress@latest my-kawapress-site
|
|
53
|
+
cd my-kawapress-site
|
|
54
|
+
npm run dev
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`pnpm create kawapress my-kawapress-site` and `yarn create kawapress my-kawapress-site` do the same thing. The scaffolder asks for a site title and a default language, and can install dependencies for you.
|
|
58
|
+
|
|
59
|
+
If you would rather see every file from the start, set the site up by hand:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
mkdir my-kawapress-site
|
|
63
|
+
cd my-kawapress-site
|
|
64
|
+
pnpm init
|
|
65
|
+
pnpm add --save-dev kawapress
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Create `kawapress.config.ts`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { nagi } from 'kawapress/nagi'
|
|
72
|
+
|
|
73
|
+
export default nagi({
|
|
74
|
+
title: 'My Docs',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Create `index.md`:
|
|
79
|
+
|
|
80
|
+
```md
|
|
81
|
+
---
|
|
82
|
+
layout: home
|
|
83
|
+
title: My Docs
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# My Docs
|
|
87
|
+
|
|
88
|
+
Welcome to my KawaPress site.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Start the development server:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
pnpm exec kawapress dev
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Build static files into `dist`:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
pnpm exec kawapress build
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The deployed output is static and does not require a Node.js server. npm and Yarn commands are available in the [Getting Started guide](https://shenqingchuan.github.io/kawapress/en/guide/getting-started).
|
|
104
|
+
|
|
105
|
+
> If your site directly imports Vue APIs or Vue components, also install `vue` as a development dependency.
|
|
106
|
+
|
|
107
|
+
## What You Get
|
|
108
|
+
|
|
109
|
+
| Area | Included capability |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| Authoring | Markdown, frontmatter, Vue in Markdown, file-based routing, data loaders |
|
|
112
|
+
| Rendering | Shared development/build SSR semantics, hydration, Vue Router navigation |
|
|
113
|
+
| Documentation UI | nagi layouts, responsive sidebar, page outline, dark mode, i18n |
|
|
114
|
+
| Content | Shiki, Twoslash, code groups, code copy, containers, GitHub alerts |
|
|
115
|
+
| Discovery | Lazy local search and locale-aware navigation |
|
|
116
|
+
| AI consumption | Page `.md`, `llms.txt`, `llms-full.txt`, copy actions |
|
|
117
|
+
| Styling | UnoCSS integration with sensible nagi defaults |
|
|
118
|
+
|
|
119
|
+
The `kawapress` package includes nagi and its official plugins. A basic site only needs this one package; advanced users can compose the individual `@kawapress/*` packages directly.
|
|
120
|
+
|
|
121
|
+
## Extension Model
|
|
122
|
+
|
|
123
|
+
KawaPress keeps extension boundaries explicit:
|
|
124
|
+
|
|
125
|
+
- a **Generator Plugin** extends configuration, Markdown, page data, Vite, and build artifacts;
|
|
126
|
+
- a **Runtime Plugin** extends the real Vue App and Vue Router in both SSR and the browser;
|
|
127
|
+
- a **Preset** combines configuration and plugins into a ready-to-use experience such as nagi.
|
|
128
|
+
|
|
129
|
+
A logical plugin is installed once and can provide both generator and runtime behavior. Learn more in the [Plugin System guide](https://shenqingchuan.github.io/kawapress/en/guide/plugin-system).
|
|
130
|
+
|
|
131
|
+
## Documentation
|
|
132
|
+
|
|
133
|
+
- [Getting Started](https://shenqingchuan.github.io/kawapress/en/guide/getting-started)
|
|
134
|
+
- [What is KawaPress?](https://shenqingchuan.github.io/kawapress/en/guide/what-is-kawapress)
|
|
135
|
+
- [Markdown Extensions](https://shenqingchuan.github.io/kawapress/en/guide/markdown-extensions)
|
|
136
|
+
- [Internationalization](https://shenqingchuan.github.io/kawapress/en/guide/internationalization)
|
|
137
|
+
- [Plugin System](https://shenqingchuan.github.io/kawapress/en/guide/plugin-system)
|
|
138
|
+
- [Theme Customization](https://shenqingchuan.github.io/kawapress/en/guide/theme-customization)
|
|
139
|
+
|
|
140
|
+
## Contributing
|
|
141
|
+
|
|
142
|
+
Issues and pull requests are welcome. For a large feature, please open an issue first so the direction can be agreed on before implementation.
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
pnpm install
|
|
146
|
+
pnpm lint
|
|
147
|
+
pnpm typecheck
|
|
148
|
+
pnpm test
|
|
149
|
+
pnpm build
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
KawaPress uses Node.js 22.12 or newer and pnpm 11.22.0 for repository development.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
[MIT](https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE) © KawaPress contributors
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo-dark.png">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo.png" alt="KawaPress" width="180">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<h1 align="center">KawaPress</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>以 Vue 为核心,面向文档与内容型网站的静态站点生成器。</strong>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
用 Markdown 安心写作,在真实 SSR 中开发,通过公开能力自由扩展。
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm 版本"></a>
|
|
20
|
+
<a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="文档构建状态"></a>
|
|
21
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT 许可"></a>
|
|
22
|
+
<img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 或更高版本">
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://shenqingchuan.github.io/kawapress/">中文文档</a>
|
|
27
|
+
·
|
|
28
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.md">English</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
使用 KawaPress 时遇到任何问题,欢迎到 [提交 Issue](https://github.com/ShenQingchuan/kawapress/issues) 反馈,一起参与本项目的早期建设。
|
|
32
|
+
|
|
33
|
+
## 为什么选择 KawaPress?
|
|
34
|
+
|
|
35
|
+
KawaPress 会把 Markdown 编译成预渲染的 Vue 网站。它保留了文档生成器简单、轻快的写作方式,同时让官方主题和第三方扩展使用完全相同的公开能力。
|
|
36
|
+
|
|
37
|
+
- **开发时也执行真实 SSR**:每次首次请求都会在服务端渲染。只在 SSR 中出现的问题,不必等到构建或上线后才发现。
|
|
38
|
+
- **默认体验完整**:内置 nagi(凪)预设,包含响应式文档布局、深浅色模式、本地搜索、代码高亮、Twoslash、代码组和多语言导航等常用能力。
|
|
39
|
+
- **从头到尾使用 Vue**:Markdown 会变成 Vue 组件,页面跳转交给 Vue Router;需要交互时,也可以自然地使用普通 Vue SFC。
|
|
40
|
+
- **面向 AI 的文档产物**:nagi 默认生成每页 `.md`、各语言的 `llms.txt` 与 `llms-full.txt`,并提供复制正文和链接的操作。
|
|
41
|
+
- **统一的扩展模型**:Generator Plugin、Runtime Plugin 与 Preset 分别覆盖生成阶段、Vue 运行时和能力组合,不依赖主题私有入口。
|
|
42
|
+
|
|
43
|
+
KawaPress 是一个独立项目,不是 VitePress 的分支或配置兼容层,也不能直接使用 VitePress 配置。
|
|
44
|
+
|
|
45
|
+
## 快速开始
|
|
46
|
+
|
|
47
|
+
请先准备 [Node.js](https://nodejs.org/) 22.12 或更高版本。
|
|
48
|
+
|
|
49
|
+
用脚手架创建新站点:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm create kawapress@latest my-kawapress-site
|
|
53
|
+
cd my-kawapress-site
|
|
54
|
+
npm run dev
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`pnpm create kawapress my-kawapress-site` 和 `yarn create kawapress my-kawapress-site` 的作用完全一样。脚手架会询问站点标题和默认语言,也可以顺便帮你安装依赖。
|
|
58
|
+
|
|
59
|
+
如果你更想从零看清每个文件,可以手动创建:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
mkdir my-kawapress-site
|
|
63
|
+
cd my-kawapress-site
|
|
64
|
+
pnpm init
|
|
65
|
+
pnpm add --save-dev kawapress
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
创建 `kawapress.config.ts`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { nagi } from 'kawapress/nagi'
|
|
72
|
+
|
|
73
|
+
export default nagi({
|
|
74
|
+
title: 'My Docs',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
创建 `index.md`:
|
|
79
|
+
|
|
80
|
+
```md
|
|
81
|
+
---
|
|
82
|
+
layout: home
|
|
83
|
+
title: My Docs
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# My Docs
|
|
87
|
+
|
|
88
|
+
欢迎来到我的 KawaPress 站点。
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
启动开发服务器:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
pnpm exec kawapress dev
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
构建静态文件到 `dist`:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
pnpm exec kawapress build
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
部署产物是普通静态文件,不需要在服务器上继续运行 Node.js。npm 和 Yarn 的完整命令可以查看[快速开始](https://shenqingchuan.github.io/kawapress/guide/getting-started)。
|
|
104
|
+
|
|
105
|
+
> 如果站点会直接导入 Vue API 或 Vue 组件,还需要把 `vue` 安装为开发依赖。
|
|
106
|
+
|
|
107
|
+
## 默认包含什么?
|
|
108
|
+
|
|
109
|
+
| 范围 | 内置能力 |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| 内容编写 | Markdown、Frontmatter、在 Markdown 中使用 Vue、文件路由、Data Loader |
|
|
112
|
+
| 渲染 | 开发与构建共享 SSR 语义、Hydration、Vue Router 站内导航 |
|
|
113
|
+
| 文档界面 | nagi 布局、响应式 Sidebar、本页目录、深浅色模式、多语言 |
|
|
114
|
+
| 内容增强 | Shiki、Twoslash、代码组、代码复制、提示容器、GitHub Alerts |
|
|
115
|
+
| 内容发现 | 按需加载的本地搜索与多语言导航 |
|
|
116
|
+
| AI 读取 | 页面 `.md`、`llms.txt`、`llms-full.txt`、页面复制操作 |
|
|
117
|
+
| 样式 | UnoCSS 集成与 nagi 默认配置 |
|
|
118
|
+
|
|
119
|
+
`kawapress` 已经包含 nagi 和它使用的官方插件。普通文档站只需要安装这一个包;需要自行组合能力时,也可以直接使用独立的 `@kawapress/*` 包。
|
|
120
|
+
|
|
121
|
+
## 扩展模型
|
|
122
|
+
|
|
123
|
+
KawaPress 把扩展边界分得很清楚:
|
|
124
|
+
|
|
125
|
+
- **Generator Plugin** 负责配置、Markdown、页面数据、Vite 与构建产物;
|
|
126
|
+
- **Runtime Plugin** 在 SSR 和浏览器中扩展真实的 Vue App 与 Vue Router;
|
|
127
|
+
- **Preset** 把配置和一组插件组合成可以直接使用的产品体验,例如 nagi。
|
|
128
|
+
|
|
129
|
+
一个逻辑插件只需要安装一次,同时可以提供生成侧和运行侧能力。更多内容请查看[插件体系](https://shenqingchuan.github.io/kawapress/guide/plugin-system)。
|
|
130
|
+
|
|
131
|
+
## 文档
|
|
132
|
+
|
|
133
|
+
- [快速开始](https://shenqingchuan.github.io/kawapress/guide/getting-started)
|
|
134
|
+
- [KawaPress 是什么?](https://shenqingchuan.github.io/kawapress/guide/what-is-kawapress)
|
|
135
|
+
- [Markdown 语法扩展](https://shenqingchuan.github.io/kawapress/guide/markdown-extensions)
|
|
136
|
+
- [国际化](https://shenqingchuan.github.io/kawapress/guide/internationalization)
|
|
137
|
+
- [插件体系](https://shenqingchuan.github.io/kawapress/guide/plugin-system)
|
|
138
|
+
- [主题定制](https://shenqingchuan.github.io/kawapress/guide/theme-customization)
|
|
139
|
+
|
|
140
|
+
## 参与贡献
|
|
141
|
+
|
|
142
|
+
欢迎提交 Issue 和 Pull Request。如果准备增加较大的功能,请先创建 Issue,把目标和方案聊清楚后再开始实现。
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
pnpm install
|
|
146
|
+
pnpm lint
|
|
147
|
+
pnpm typecheck
|
|
148
|
+
pnpm test
|
|
149
|
+
pnpm build
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
KawaPress 仓库使用 Node.js 22.12 或更高版本,以及 pnpm 11.22.0 进行开发。
|
|
153
|
+
|
|
154
|
+
## 开源许可
|
|
155
|
+
|
|
156
|
+
[MIT](https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE) © KawaPress 贡献者
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kawapress/plugin-last-updated",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Last updated time and edit link for every KawaPress page",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://shenqingchuan.github.io/kawapress/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/ShenQingchuan/kawapress.git",
|
|
11
|
+
"directory": "packages/plugin-last-updated"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/ShenQingchuan/kawapress/issues"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./src/index.ts",
|
|
19
|
+
"import": "./src/index.ts",
|
|
20
|
+
"default": "./src/index.ts"
|
|
21
|
+
},
|
|
22
|
+
"./runtime-plugin": {
|
|
23
|
+
"types": "./src/runtime-plugin.ts",
|
|
24
|
+
"import": "./src/runtime-plugin.ts",
|
|
25
|
+
"default": "./src/runtime-plugin.ts"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/**/!(*.test.*)"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.12.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"kawapress": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"consola": "^3.4.2"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"happy-dom": "^20.11.6",
|
|
47
|
+
"typescript": "^6.0.3",
|
|
48
|
+
"vite": "^8.2.2",
|
|
49
|
+
"vue": "^3.5.41",
|
|
50
|
+
"kawapress": "0.1.0"
|
|
51
|
+
}
|
|
6
52
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { LastUpdatedPageData } from '../types'
|
|
3
|
+
import { usePageData, useSite } from 'kawapress/client'
|
|
4
|
+
import { computed, onMounted, shallowRef } from 'vue'
|
|
5
|
+
import { formatLastUpdatedDate } from '../format'
|
|
6
|
+
import { resolveLastUpdatedTexts } from '../labels'
|
|
7
|
+
import { runtimeOptions } from '../runtime-options'
|
|
8
|
+
|
|
9
|
+
const site = useSite()
|
|
10
|
+
const page = usePageData()
|
|
11
|
+
/** Set after hydration, so server and first client render both use UTC. */
|
|
12
|
+
const mounted = shallowRef(false)
|
|
13
|
+
|
|
14
|
+
onMounted(() => {
|
|
15
|
+
mounted.value = true
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const lang = computed(() => site.value.lang ?? 'en')
|
|
19
|
+
const texts = computed(() => resolveLastUpdatedTexts({
|
|
20
|
+
lang: lang.value,
|
|
21
|
+
localeIndex: site.value.localeIndex,
|
|
22
|
+
labels: runtimeOptions.labels,
|
|
23
|
+
localeLabels: runtimeOptions.localeLabels,
|
|
24
|
+
}))
|
|
25
|
+
|
|
26
|
+
const frontmatter = computed<Record<string, unknown>>(
|
|
27
|
+
() => page.value?.frontmatter ?? {},
|
|
28
|
+
)
|
|
29
|
+
const lastUpdated = computed(() => {
|
|
30
|
+
const value = (page.value as LastUpdatedPageData | undefined)?.lastUpdated
|
|
31
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
32
|
+
})
|
|
33
|
+
const editLink = computed(() => {
|
|
34
|
+
const value = (page.value as LastUpdatedPageData | undefined)?.editLink
|
|
35
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const showDate = computed(() => (
|
|
39
|
+
lastUpdated.value !== undefined && frontmatter.value.lastUpdated !== false
|
|
40
|
+
))
|
|
41
|
+
const showEditLink = computed(() => (
|
|
42
|
+
editLink.value !== undefined && frontmatter.value.editLink !== false
|
|
43
|
+
))
|
|
44
|
+
const visible = computed(() => showDate.value || showEditLink.value)
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* An explicit `timeZone` in the options is a deliberate choice, so it wins over
|
|
48
|
+
* the visitor's zone. Otherwise the server renders in UTC and the browser
|
|
49
|
+
* re-renders in its own zone once mounted.
|
|
50
|
+
*/
|
|
51
|
+
const explicitTimeZone = typeof runtimeOptions.formatOptions?.timeZone === 'string'
|
|
52
|
+
? runtimeOptions.formatOptions.timeZone
|
|
53
|
+
: undefined
|
|
54
|
+
const timeZone = computed(() => (
|
|
55
|
+
explicitTimeZone ?? (mounted.value ? undefined : 'UTC')
|
|
56
|
+
))
|
|
57
|
+
|
|
58
|
+
const iso = computed(() => new Date(lastUpdated.value ?? 0).toISOString())
|
|
59
|
+
const formatted = computed(() => formatLastUpdatedDate(
|
|
60
|
+
lastUpdated.value ?? 0,
|
|
61
|
+
lang.value,
|
|
62
|
+
runtimeOptions.formatOptions,
|
|
63
|
+
timeZone.value,
|
|
64
|
+
))
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<template>
|
|
68
|
+
<p v-if="visible" class="kawa-last-updated">
|
|
69
|
+
<a
|
|
70
|
+
v-if="showEditLink"
|
|
71
|
+
class="kawa-last-updated__edit"
|
|
72
|
+
:href="editLink"
|
|
73
|
+
rel="noopener noreferrer"
|
|
74
|
+
target="_blank"
|
|
75
|
+
>{{ texts.editLink }}</a>
|
|
76
|
+
<span v-if="showDate" class="kawa-last-updated__text">
|
|
77
|
+
<span class="kawa-last-updated__label">{{ texts.lastUpdated }}</span>
|
|
78
|
+
<time class="kawa-last-updated__time" :datetime="iso">{{ formatted }}</time>
|
|
79
|
+
</span>
|
|
80
|
+
</p>
|
|
81
|
+
</template>
|
package/src/edit-link.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isAbsolute, relative, sep } from 'node:path'
|
|
2
|
+
|
|
3
|
+
/** Placeholder of `editLink.pattern` that stands for the source path. */
|
|
4
|
+
export const EDIT_LINK_PATH_PLACEHOLDER = ':path'
|
|
5
|
+
|
|
6
|
+
const EXAMPLE_PATTERN = 'https://github.com/user/repo/edit/main/:path'
|
|
7
|
+
|
|
8
|
+
/** Rejects a pattern that cannot place the source path, naming the option to fix. */
|
|
9
|
+
export function validateEditLinkPattern(pattern: string): string {
|
|
10
|
+
if (typeof pattern !== 'string' || !pattern.includes(EDIT_LINK_PATH_PLACEHOLDER)) {
|
|
11
|
+
throw new TypeError(
|
|
12
|
+
`KawaPress Last Updated: \`editLink.pattern\` must contain ${JSON.stringify(EDIT_LINK_PATH_PLACEHOLDER)} so the page source can be placed in it, for example ${JSON.stringify(EXAMPLE_PATTERN)}. Received ${JSON.stringify(pattern)}.`,
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
return pattern
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Path of `file` inside the repository, written with forward slashes like git
|
|
20
|
+
* does, or `undefined` when the file lives outside the repository.
|
|
21
|
+
*/
|
|
22
|
+
export function toRepositoryPath(
|
|
23
|
+
file: string,
|
|
24
|
+
repositoryRoot: string,
|
|
25
|
+
): string | undefined {
|
|
26
|
+
const path = relative(repositoryRoot, file)
|
|
27
|
+
if (!path || isAbsolute(path) || path === '..' || path.startsWith(`..${sep}`)) {
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
return path.split(sep).map(encodeURIComponent).join('/')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Fills `pattern` with a repository path, for example `docs/guide/intro.md`. */
|
|
34
|
+
export function createEditLink(pattern: string, repositoryPath: string): string {
|
|
35
|
+
return pattern.split(EDIT_LINK_PATH_PLACEHOLDER).join(repositoryPath)
|
|
36
|
+
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const DEFAULT_FORMAT_OPTIONS: Intl.DateTimeFormatOptions = {
|
|
2
|
+
dateStyle: 'medium',
|
|
3
|
+
timeStyle: 'short',
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options that describe the date field by field. `Intl.DateTimeFormat` rejects
|
|
8
|
+
* them together with `dateStyle`/`timeStyle`, so the defaults step aside.
|
|
9
|
+
*/
|
|
10
|
+
const FIELD_OPTION_KEYS = [
|
|
11
|
+
'weekday',
|
|
12
|
+
'era',
|
|
13
|
+
'year',
|
|
14
|
+
'month',
|
|
15
|
+
'day',
|
|
16
|
+
'dayPeriod',
|
|
17
|
+
'hour',
|
|
18
|
+
'minute',
|
|
19
|
+
'second',
|
|
20
|
+
'fractionalSecondDigits',
|
|
21
|
+
'timeZoneName',
|
|
22
|
+
] as const
|
|
23
|
+
|
|
24
|
+
/** Formatters are expensive to create and depend only on their arguments. */
|
|
25
|
+
const formatterCache = new Map<string, Intl.DateTimeFormat>()
|
|
26
|
+
const MAX_CACHED_FORMATTERS = 32
|
|
27
|
+
|
|
28
|
+
export function resolveFormatOptions(
|
|
29
|
+
formatOptions: Intl.DateTimeFormatOptions = {},
|
|
30
|
+
): Intl.DateTimeFormatOptions {
|
|
31
|
+
const usesFields = FIELD_OPTION_KEYS.some(key => formatOptions[key] !== undefined)
|
|
32
|
+
return usesFields
|
|
33
|
+
? { ...formatOptions }
|
|
34
|
+
: { ...DEFAULT_FORMAT_OPTIONS, ...formatOptions }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Formats `value` for `lang`. Without an explicit `timeZone` the visitor's zone
|
|
39
|
+
* is used, so server rendering has to pass `'UTC'` to stay deterministic.
|
|
40
|
+
*/
|
|
41
|
+
export function formatLastUpdatedDate(
|
|
42
|
+
value: number,
|
|
43
|
+
lang: string,
|
|
44
|
+
formatOptions?: Intl.DateTimeFormatOptions,
|
|
45
|
+
timeZone?: string,
|
|
46
|
+
): string {
|
|
47
|
+
return createDateFormatter(lang, formatOptions, timeZone).format(value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createDateFormatter(
|
|
51
|
+
lang: string,
|
|
52
|
+
formatOptions: Intl.DateTimeFormatOptions = {},
|
|
53
|
+
timeZone?: string,
|
|
54
|
+
): Intl.DateTimeFormat {
|
|
55
|
+
const options = resolveFormatOptions(formatOptions)
|
|
56
|
+
if (timeZone !== undefined) {
|
|
57
|
+
options.timeZone = timeZone
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const key = `${lang}\u0000${JSON.stringify(options)}`
|
|
61
|
+
const cached = formatterCache.get(key)
|
|
62
|
+
if (cached) {
|
|
63
|
+
return cached
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const formatter = buildFormatter(lang, options)
|
|
67
|
+
if (formatterCache.size >= MAX_CACHED_FORMATTERS) {
|
|
68
|
+
formatterCache.clear()
|
|
69
|
+
}
|
|
70
|
+
formatterCache.set(key, formatter)
|
|
71
|
+
return formatter
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function buildFormatter(
|
|
75
|
+
lang: string,
|
|
76
|
+
options: Intl.DateTimeFormatOptions,
|
|
77
|
+
): Intl.DateTimeFormat {
|
|
78
|
+
try {
|
|
79
|
+
return new Intl.DateTimeFormat(lang, options)
|
|
80
|
+
}
|
|
81
|
+
catch (cause) {
|
|
82
|
+
throw new RangeError(
|
|
83
|
+
[
|
|
84
|
+
`KawaPress Last Updated: cannot format dates for lang ${JSON.stringify(lang)} with ${JSON.stringify(options)}.`,
|
|
85
|
+
'Fix `lang` in the matching `locales` entry, or the `lastUpdated.formatOptions` passed to the plugin.',
|
|
86
|
+
].join(' '),
|
|
87
|
+
{ cause },
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/git.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
|
+
|
|
4
|
+
const execFileAsync = promisify(execFile)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Absolute path of the repository that contains `cwd`, or `undefined` when git
|
|
8
|
+
* cannot report it, for example outside a repository.
|
|
9
|
+
*/
|
|
10
|
+
export async function readGitTopLevel(cwd: string): Promise<string | undefined> {
|
|
11
|
+
try {
|
|
12
|
+
const { stdout } = await execFileAsync(
|
|
13
|
+
'git',
|
|
14
|
+
['rev-parse', '--show-toplevel'],
|
|
15
|
+
{ cwd },
|
|
16
|
+
)
|
|
17
|
+
const topLevel = stdout.trim()
|
|
18
|
+
return topLevel || undefined
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.ts
ADDED
package/src/labels.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { LastUpdatedLabels, LastUpdatedRuntimeOptions } from './types'
|
|
2
|
+
|
|
3
|
+
/** Text before the date, including the separator each language writes after it. */
|
|
4
|
+
export interface ResolvedLastUpdatedTexts {
|
|
5
|
+
lastUpdated: string
|
|
6
|
+
editLink: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ENGLISH_LABELS: LastUpdatedLabels = {
|
|
10
|
+
lastUpdated: 'Last updated',
|
|
11
|
+
editLink: 'Edit this page',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CHINESE_LABELS: LastUpdatedLabels = {
|
|
15
|
+
lastUpdated: '最后更新于',
|
|
16
|
+
editLink: '编辑此页',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Chinese writes a full-width colon and needs no space after it. */
|
|
20
|
+
const ENGLISH_SEPARATOR = ': '
|
|
21
|
+
const CHINESE_SEPARATOR = ':'
|
|
22
|
+
|
|
23
|
+
export interface ResolveLastUpdatedLabelsOptions {
|
|
24
|
+
/** `lang` of the current locale; anything starting with `zh` gets Chinese text. */
|
|
25
|
+
lang?: string
|
|
26
|
+
/** KawaPress locale key of the current page, for example `root` or `en`. */
|
|
27
|
+
localeIndex?: string
|
|
28
|
+
labels?: LastUpdatedRuntimeOptions['labels']
|
|
29
|
+
localeLabels?: LastUpdatedRuntimeOptions['localeLabels']
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Picks the visible strings for one page: the locale key wins over the global
|
|
34
|
+
* override, which wins over the built-in text of the language.
|
|
35
|
+
*/
|
|
36
|
+
export function resolveLastUpdatedTexts(
|
|
37
|
+
options: ResolveLastUpdatedLabelsOptions = {},
|
|
38
|
+
): ResolvedLastUpdatedTexts {
|
|
39
|
+
const localeIndex = options.localeIndex ?? 'root'
|
|
40
|
+
const builtIn = isChinese(options.lang) ? CHINESE_LABELS : ENGLISH_LABELS
|
|
41
|
+
const separator = isChinese(options.lang) ? CHINESE_SEPARATOR : ENGLISH_SEPARATOR
|
|
42
|
+
|
|
43
|
+
const read = (key: keyof LastUpdatedLabels): string => (
|
|
44
|
+
options.localeLabels?.[localeIndex]?.[key]
|
|
45
|
+
?? options.labels?.[key]
|
|
46
|
+
?? builtIn[key]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
lastUpdated: `${read('lastUpdated')}${separator}`,
|
|
51
|
+
editLink: read('editLink'),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isChinese(lang: string | undefined): boolean {
|
|
56
|
+
return typeof lang === 'string' && lang.toLocaleLowerCase().startsWith('zh')
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
@layer kawa.plugin {
|
|
2
|
+
.kawa-last-updated {
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-wrap: wrap;
|
|
5
|
+
gap: 0.25rem 1.25rem;
|
|
6
|
+
align-items: baseline;
|
|
7
|
+
margin: 0;
|
|
8
|
+
color: var(--kawa-c-text-3);
|
|
9
|
+
font-family: var(--kawa-font-sans);
|
|
10
|
+
font-size: 0.8125rem;
|
|
11
|
+
line-height: 1.6;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.kawa-last-updated__edit {
|
|
15
|
+
color: var(--kawa-c-text-2);
|
|
16
|
+
text-decoration: none;
|
|
17
|
+
border-radius: calc(var(--kawa-radius) / 2);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.kawa-last-updated__edit:hover {
|
|
21
|
+
color: var(--kawa-c-brand);
|
|
22
|
+
text-decoration: underline;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.kawa-last-updated__edit:focus-visible {
|
|
26
|
+
outline: 2px solid var(--kawa-c-brand);
|
|
27
|
+
outline-offset: 2px;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.kawa-last-updated__time {
|
|
31
|
+
font-variant-numeric: tabular-nums;
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/name.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Plugin } from 'vite'
|
|
2
|
+
import type {
|
|
3
|
+
LastUpdatedPluginOptions,
|
|
4
|
+
LastUpdatedRuntimeOptions,
|
|
5
|
+
} from './types'
|
|
6
|
+
import { stringifyJson } from 'kawapress'
|
|
7
|
+
|
|
8
|
+
export const LAST_UPDATED_OPTIONS_MODULE_ID = 'virtual:kawapress-last-updated-options'
|
|
9
|
+
const RESOLVED_ID = `\0${LAST_UPDATED_OPTIONS_MODULE_ID}`
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Keeps only what the browser needs. `editLink.text` becomes the weakest link
|
|
13
|
+
* text so the localized overrides above it keep their precedence.
|
|
14
|
+
*/
|
|
15
|
+
export function createRuntimeOptions(
|
|
16
|
+
options: LastUpdatedPluginOptions,
|
|
17
|
+
): LastUpdatedRuntimeOptions {
|
|
18
|
+
const labels = defined({
|
|
19
|
+
editLink: options.editLink?.text,
|
|
20
|
+
...options.labels,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
return defined({
|
|
24
|
+
formatOptions: options.lastUpdated?.formatOptions,
|
|
25
|
+
labels: Object.keys(labels).length > 0 ? labels : undefined,
|
|
26
|
+
localeLabels: options.localeLabels,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Exposes the plugin options as a virtual module, so the runtime component
|
|
32
|
+
* never imports generator code or shares module state with it.
|
|
33
|
+
*/
|
|
34
|
+
export function lastUpdatedOptionsPlugin(
|
|
35
|
+
options: LastUpdatedRuntimeOptions,
|
|
36
|
+
): Plugin {
|
|
37
|
+
return {
|
|
38
|
+
name: 'kawapress:last-updated-options',
|
|
39
|
+
resolveId(id) {
|
|
40
|
+
if (id === LAST_UPDATED_OPTIONS_MODULE_ID) {
|
|
41
|
+
return RESOLVED_ID
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
load(id) {
|
|
45
|
+
if (id !== RESOLVED_ID) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
return `export const lastUpdatedOptions = ${stringifyJson(options, {
|
|
49
|
+
label: 'the @kawapress/plugin-last-updated options passed to the runtime',
|
|
50
|
+
path: 'lastUpdatedOptions',
|
|
51
|
+
})}`
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function defined<T extends object>(value: T): T {
|
|
57
|
+
return Object.fromEntries(
|
|
58
|
+
Object.entries(value).filter(([, item]) => item !== undefined),
|
|
59
|
+
) as T
|
|
60
|
+
}
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { KawaPressPlugin } from 'kawapress'
|
|
2
|
+
import type { LastUpdatedPageData, LastUpdatedPluginOptions } from './types'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import process from 'node:process'
|
|
5
|
+
import { consola } from 'consola'
|
|
6
|
+
import { definePlugin } from 'kawapress'
|
|
7
|
+
import { readGitLastUpdated } from 'kawapress/generator'
|
|
8
|
+
import { createEditLink, toRepositoryPath, validateEditLinkPattern } from './edit-link'
|
|
9
|
+
import { readGitTopLevel } from './git'
|
|
10
|
+
import { PLUGIN_NAME } from './name'
|
|
11
|
+
import { createRuntimeOptions, lastUpdatedOptionsPlugin } from './options-module'
|
|
12
|
+
|
|
13
|
+
const WARNING_LABEL = 'KawaPress Last Updated'
|
|
14
|
+
|
|
15
|
+
export function lastUpdatedPlugin(
|
|
16
|
+
options: LastUpdatedPluginOptions = {},
|
|
17
|
+
): KawaPressPlugin {
|
|
18
|
+
const runtimeOptions = createRuntimeOptions(options)
|
|
19
|
+
const editLinkPattern = options.editLink
|
|
20
|
+
? validateEditLinkPattern(options.editLink.pattern)
|
|
21
|
+
: undefined
|
|
22
|
+
|
|
23
|
+
let root = process.cwd()
|
|
24
|
+
let timestamps: Promise<Map<string, number>> | undefined
|
|
25
|
+
let repositoryRoot: Promise<string | undefined> | undefined
|
|
26
|
+
let warnedMissingRepository = false
|
|
27
|
+
|
|
28
|
+
/** One `git log` per build or dev session, keyed by absolute source file. */
|
|
29
|
+
const loadTimestamps = (): Promise<Map<string, number>> => {
|
|
30
|
+
timestamps ??= readGitLastUpdated(root, {
|
|
31
|
+
label: WARNING_LABEL,
|
|
32
|
+
skipped: 'the last updated time',
|
|
33
|
+
}).then(dates => dates ?? new Map())
|
|
34
|
+
return timestamps
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const loadRepositoryRoot = (): Promise<string | undefined> => {
|
|
38
|
+
repositoryRoot ??= readGitTopLevel(root)
|
|
39
|
+
return repositoryRoot
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return definePlugin({
|
|
43
|
+
name: PLUGIN_NAME,
|
|
44
|
+
setup(api) {
|
|
45
|
+
api.vite((config) => {
|
|
46
|
+
root = config.root ? resolve(config.root) : process.cwd()
|
|
47
|
+
config.plugins ??= []
|
|
48
|
+
config.plugins.push(lastUpdatedOptionsPlugin(runtimeOptions))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
api.pageData(async (pageData, { file }) => {
|
|
52
|
+
const sourceFile = resolve(file)
|
|
53
|
+
const page = pageData as LastUpdatedPageData
|
|
54
|
+
|
|
55
|
+
const dates = await loadTimestamps()
|
|
56
|
+
const lastUpdated = dates.get(sourceFile)
|
|
57
|
+
if (lastUpdated !== undefined) {
|
|
58
|
+
page.lastUpdated = lastUpdated
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!editLinkPattern) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
const topLevel = await loadRepositoryRoot()
|
|
65
|
+
if (!topLevel) {
|
|
66
|
+
if (!warnedMissingRepository) {
|
|
67
|
+
warnedMissingRepository = true
|
|
68
|
+
consola.warn(`${WARNING_LABEL}: the site is not inside a git repository, the edit link is skipped.`)
|
|
69
|
+
}
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
const repositoryPath = toRepositoryPath(sourceFile, topLevel)
|
|
73
|
+
if (repositoryPath) {
|
|
74
|
+
page.editLink = createEditLink(editLinkPattern, repositoryPath)
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default lastUpdatedPlugin
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { LastUpdatedRuntimeOptions } from './types'
|
|
2
|
+
import { lastUpdatedOptions } from 'virtual:kawapress-last-updated-options'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options the generator side published as JSON. The virtual module is the only
|
|
6
|
+
* connection between the two sides; the runtime never imports generator code.
|
|
7
|
+
*/
|
|
8
|
+
export const runtimeOptions: LastUpdatedRuntimeOptions = lastUpdatedOptions
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineRuntimePlugin } from 'kawapress'
|
|
2
|
+
import LastUpdated from './components/LastUpdated.vue'
|
|
3
|
+
import { PLUGIN_NAME } from './name'
|
|
4
|
+
import './last-updated.css'
|
|
5
|
+
|
|
6
|
+
export default defineRuntimePlugin({
|
|
7
|
+
name: PLUGIN_NAME,
|
|
8
|
+
setup(api) {
|
|
9
|
+
api.addToOutlet('doc.footer', { id: 'last-updated', component: LastUpdated })
|
|
10
|
+
},
|
|
11
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { PageData } from 'kawapress'
|
|
2
|
+
|
|
3
|
+
/** User-visible strings of the last updated footer. */
|
|
4
|
+
export interface LastUpdatedLabels {
|
|
5
|
+
/** Text before the date, for example `Last updated`. */
|
|
6
|
+
lastUpdated: string
|
|
7
|
+
/** Text of the edit link. */
|
|
8
|
+
editLink: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface LastUpdatedEditLinkOptions {
|
|
12
|
+
/**
|
|
13
|
+
* URL template of the page source on the repository host. `:path` is replaced
|
|
14
|
+
* by the source path inside the repository, without a leading slash.
|
|
15
|
+
*/
|
|
16
|
+
pattern: string
|
|
17
|
+
/** Link text for every locale. `labels` and `localeLabels` win over it. */
|
|
18
|
+
text?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LastUpdatedDateOptions {
|
|
22
|
+
/** Formatting passed to `Intl.DateTimeFormat`; defaults to a medium date and a short time. */
|
|
23
|
+
formatOptions?: Intl.DateTimeFormatOptions
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface LastUpdatedPluginOptions {
|
|
27
|
+
lastUpdated?: LastUpdatedDateOptions
|
|
28
|
+
editLink?: LastUpdatedEditLinkOptions
|
|
29
|
+
/** Override the built-in strings for every locale. */
|
|
30
|
+
labels?: Partial<LastUpdatedLabels>
|
|
31
|
+
/** Override the built-in strings for one KawaPress locale key. */
|
|
32
|
+
localeLabels?: Record<string, Partial<LastUpdatedLabels>>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Options the runtime component receives as JSON through a virtual module. */
|
|
36
|
+
export interface LastUpdatedRuntimeOptions {
|
|
37
|
+
formatOptions?: Intl.DateTimeFormatOptions
|
|
38
|
+
labels?: Partial<LastUpdatedLabels>
|
|
39
|
+
localeLabels?: Record<string, Partial<LastUpdatedLabels>>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Page data with the fields this plugin adds. Read it from the runtime with
|
|
44
|
+
* `usePageData()` when a custom component wants to show the same values.
|
|
45
|
+
*/
|
|
46
|
+
export interface LastUpdatedPageData extends PageData {
|
|
47
|
+
/** Last commit time of the Markdown source file, in milliseconds since the epoch. */
|
|
48
|
+
lastUpdated?: number
|
|
49
|
+
/** Absolute URL of the page source on the repository host. */
|
|
50
|
+
editLink?: string
|
|
51
|
+
}
|