Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ packages through `pnpm-workspace.yaml`.

- [`firekylin-plugin-embed`](packages/embed): automatically embed supported standalone media and code URLs.

- [`firekylin-plugin-simply-static`](packages/simply-static): export public sites as static HTML/ZIP, with a durable worker, static search, forms/comments integration, and hosting adapters.

## Publishing

Packages are published automatically through npm trusted publishing after their
Expand Down
57 changes: 57 additions & 0 deletions packages/simply-static/IMPLEMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Feature mapping and validation

Independent Firekylin implementation of the publishing capabilities described by
[Simply Static](https://wordpress.org/plugins/simply-static/), including the public
Pro feature categories. No Firekylin core files are changed. WordPress-specific
page builders, WP-CLI, form plugins and language plugins are replaced by the
Firekylin rendered-page crawler, CLI and explicit integration settings. Commercial
support and hosted Studio are services, not features provided by this package.

| Capability | Implementation / boundary |
| --- | --- |
| Full site / theme export | Anonymous HTML, linked pages, pagination, CSS imports, scripts, images, srcset, fonts; additional URL seeds for unlinked/plugin/language routes |
| ZIP / directory / URLs | Complete snapshot and ZIP; absolute or relative links; Unicode filenames, canonical/meta URL rewriting |
| Sitemap / feeds / robots / errors | XML link rewriting; robots Sitemap rewrite; explicit error-page URL produces 404.html |
| Incremental / single page | ETag response reuse; baseline checkpoints refresh selected pages and direct referrers; full snapshots still published |
| Admin / CLI | Settings, enqueue, progress, request logs, ZIP download, single-page form; standalone worker/status/cancel/recover commands |
| Automation | Debounced content events and Cron while the web process runs; durable filesystem queue with serialized execution |
| Destinations | Local managed symlink, Git/GitHub branch, Netlify ZIP API, Cloudflare Wrangler, S3-compatible AWS CLI, Bunny Storage API, SFTP |
| Search | Local JSON index and browser search UI; built-in search routes/forms adapted; no Algolia account required |
| Forms | CSS-selector mappings to HTTPS POST services; arbitrary AJAX form handlers require theme-specific adaptation |
| Comments | Existing external scripts retained; Firekylin Waline containers can target an external Waline server while preserving original page identity |
| Multilingual | Crawl linked language routes, preserve hreflang links; explicitly seed unlinked language entry points |
| Optimization | Optional HTML/CSS/JS minification and optional sharp PNG/JPEG/WebP optimization |
| Recovery | Per-resource checksummed checkpoints, same-host dead-worker recovery; uncertain remote publication is not automatically repeated |

## Publication semantics

Local publication switches a managed symlink after staging. Git makes a snapshot
commit without force pushes. Netlify waits for the deployment to become ready;
Cloudflare reports the official command result. S3 and Bunny sync individual
objects and are not atomic. SFTP stages a directory and performs two renames with
rollback on switch failure, leaving a brief gap. Credentials come from the worker
environment, SSH keys or provider tooling, not generated pages.

## Validation

- Unit/integration tests cover crawler boundaries, private responses, redirects,
size limits, XML/CSS/HTML transforms, jobs, recovery, scheduling, deployment
failures, forms/comments mapping and browser search execution in jsdom.
- CLI tests use a real local HTTP server and separate worker process, including
single-page updates. Local and Git adapters use real directories and a local
bare Git remote.
- Optional real-core tests register the admin declarations and export an isolated
SQLite-backed Firekylin website with the default theme. Private, draft, future
and password-protected content must not occur in the generated pages/index.
- Remote hosting adapters use mocked transports/commands; real account deployments
and browser visual comparison are not claimed.

## Operational boundaries

A persistent shared filesystem and separate worker are required for admin jobs.
Serverless/ephemeral web deployments should execute exports in a persistent CI or
worker environment. Search, forms and comments do not turn authentication,
payments, arbitrary JavaScript APIs or server-side business logic into static HTML.
JavaScript runtime imports/requests are not recursively interpreted by the crawler.
Full export is required after global theme/navigation/visibility changes. Export
memory, provider-specific non-atomic publication and cleanup are documented in README.
248 changes: 248 additions & 0 deletions packages/simply-static/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# Firekylin Simply Static

将 Firekylin 公开网站导出为独立静态文件。复用源站匿名访客看到的
HTML 和主题资源,不修改 Firekylin core。

> 要求 Firekylin 2.19.4+、Node.js 22.13+。这是独立实现,非 Simply Static 官方产品。
> 功能对照与验证边界见 [IMPLEMENTATION.md](IMPLEMENTATION.md)。

## 安装与后台

在网站项目中安装此包并启用插件,后台会出现「静态发布」。
配置源站地址、静态网站地址,然后选择「生成静态网站」。
源站需要能从工作进程访问。后台发起的导出包含 sitemap.xml、rss.xml
以及自行添加的页面地址。使用源站 sitemap 中的公开链接补充内容发现。

源站地址可以包含子目录;目标地址也可包含子目录。相对链接模式允许
把同一份输出移动到不同的域名或子目录。

外部图片、字体和 CSS 默认保留远程引用;如需下载,逐行填写允许的
外部资源源站,例如 `https://cdn.example.com`。不要把私有服务加入此列表。

## 工作进程

后台只负责创建任务。请在同一网站项目目录运行独立工作进程:

```sh
npx --no-install firekylin-static worker --store .firekylin/simply-static/jobs
```

使用进程管理器保持其运行。工作进程和 Web 进程需要共享同一持久化目录。
纯临时文件系统的部署不能依赖这个本地队列;应在持久化执行环境运行
CLI,例如 CI 中直接执行下面的 `export` 命令。

定时生成使用 Cron 表达式与 IANA 时区。定时触发器运行在 Firekylin
进程内,网站进程停止期间不会补跑。自动生成合并短时间内的内容更新,
多个实例会复用相同的待执行任务。

## 独立命令行

创建 `static.config.json`:

```json
{
"sourceUrl": "https://cms.example.com/",
"destinationUrl": "https://www.example.com/",
"relative": false,
"incremental": true,
"additionalUrls": ["/sitemap.xml", "/rss.xml"],
"exclude": ["/private"],
"assetOrigins": ["https://cdn.example.com"]
}
```

```sh
npx --no-install firekylin-static export --config static.config.json
npx --no-install firekylin-static enqueue --config static.config.json
npx --no-install firekylin-static worker --once
npx --no-install firekylin-static status --id JOB_ID
npx --no-install firekylin-static cancel --id JOB_ID
```

CLI 默认任务目录是 `.firekylin/static-jobs`,可通过 `--store` 显式指定。
每个任务保存状态和进度,成功后生成 `output/` 和 `output.zip`。
取消任务会保留诊断信息,不会将未完成的输出发布出去。

## 本地发布

配置 `localDestination` 为绝对路径。插件在目标旁创建完整发布目录,
通过替换符号链接切换版本。目标必须不存在或为插件创建的发布链接,
不会覆盖已有普通目录。旧版本保留以便人工恢复,暂不自动清理。
Web 服务器须允许跟随此发布链接。此方式依赖支持符号链接的文件系统。

## Git 发布

配置 `gitRepository` 与 `gitBranch`(默认 `gh-pages`)。工作进程使用
Git 凭据助手或 SSH 身份验证;不要把密码或 Token 放到仓库 URL。
插件创建完整静态快照提交,清理分支上旧的静态文件,不使用强制推送。
发布分支应专用于静态输出,不能指向网站源码分支。

每个任务只能配置一个发布目标。GitHub Pages 或其他托管平台仍需配置为读取
指定发布分支;提交分支成功不代表托管平台已经部署完成。

## 搜索和动态功能

输出包含 `static-search.html`、脚本、样式和公开页面搜索索引。可将导航
链接指向这个页面。搜索在浏览器端执行,不依赖 Firekylin 后台。

现有独立评论服务脚本会随页面保留,但仍需要评论服务可访问。依赖源站
API 的登录和动态请求不会自动变成静态功能。普通表单可配置外部端点;
内置评论可通过 `commentServerUrl` 指向独立 Waline 服务(HTTPS)。插件保留
原页面 pathname 作为评论标识,移动静态站子目录不会产生新的评论线程。
服务端须允许静态站域名的跨域请求,并配置验证码域名;原站登录状态不会
传递到独立评论服务。默认保留既有第三方评论脚本,未适配的内置评论会记录警告。

## 目前的验证范围

测试覆盖 URL 映射、下载限制和重定向、HTML/CSS/XML 解析、目录和 ZIP
生成、条件请求缓存、持久化队列、定时触发、真实本地 HTTP 抓取、本地
发布切换及真实本地 Git 远端发布。可使用真实 core 后台注册器验证声明:

```sh
FIREKYLIN_CORE_PATH=/absolute/path/to/firekylin node --test test/*.test.js
```

另有真实 SQLite Firekylin 默认主题导出测试,验证草稿、私密、未来发布和
密码保护内容不泄露。搜索 UI 通过 jsdom 执行测试;未进行真实浏览器外观验收。
远程平台使用模拟传输/进程测试,未使用真实账号部署。

## Netlify 发布

后台填写 `netlifySiteId`,或在 CLI 配置中设置同名字段。工作进程环境
需要 `NETLIFY_AUTH_TOKEN`。不要将该令牌放进配置 JSON、任务记录或代码仓库。
插件上传完整 ZIP 并轮询部署状态,只有 Netlify 返回 `ready` 才判定成功。
如果超过等待次数,错误中保留部署 ID,应先检查已有部署,避免重复上传。
每个任务只能配置一个发布目标。当前只有模拟接口测试,尚未在真实账号验收。

## 中断任务恢复

工作进程异常退出后,可在原执行主机使用:

```sh
npx --no-install firekylin-static recover --id JOB_ID --store .firekylin/simply-static/jobs
```

恢复前会检查锁文件中的主机和进程,活跃进程、其他主机或无法确认的
所有者不会被抢占。恢复重新排队执行抓取,启用增量缓存时可以复用条件
请求结果。若中断发生在发布阶段,必须先核对远端结果,不会自动重试发布。

## 额外文件与表单适配

CLI 配置可添加未被页面引用的文件或目录:

```json
{
"additionalFiles": [{"source": "public-downloads", "target": "downloads"}],
"searchSelectors": ["form.site-search"],
"forms": [{"selector": "form.contact", "endpoint": "https://forms.example/submit"}]
}
```

`source` 相对于工作进程的工作目录,`target` 相对于导出根目录。额外文件
拒绝隐藏文件、符号链接、路径越界和已生成文件冲突;仍需确认指定目录
只包含可公开资料。

搜索适配会将匹配表单的查询字段改为 `q` 并指向静态搜索页。表单适配
仅适用于普通 HTML POST 表单,外部服务需接受对应字段;第三方脚本绑定
的 AJAX 表单可能需要另行适配。未匹配表单会在导出清单中记录警告。

## 输出优化

`minify` 启用保守的 HTML/CSS/JavaScript 压缩,不改变量名、不合并 HTML
文本空白。`optimizeImages` 使用可选依赖 sharp,保持图片格式、尺寸及 URL,
仅在结果更小时替换。PNG 使用无损压缩;JPEG/WebP 使用质量 85 的有损压缩。
动画、带方向信息或 ICC 配置的图片保持原样。缺少 sharp 时开启图片优化
会明确报错,关闭此功能仍可正常导出。

## Cloudflare Pages 发布

后台配置 `cloudflareProject` 和 `cloudflareBranch`。工作进程 PATH 中需
预先安装 Wrangler,环境提供 `CLOUDFLARE_API_TOKEN` 与 `CLOUDFLARE_ACCOUNT_ID`。
分支匹配 Pages 项目的生产分支时发布生产版本,否则发布预览版本。
插件在隔离工作目录中运行官方 `wrangler pages deploy`,不会自动安装 CLI。
返回 `command-completed` 表示 Wrangler 成功退出,仍应查看 Pages 部署详情
核对自定义域名的上线情况。当前测试使用模拟执行器,未操作真实账号。

恢复任务会读取任务专属抓取检查点,复用成功下载的原始响应,并重新解析
链接来重建未完成队列。检查点代表该次导出看到的版本;若源站已发生较大
变化,应创建新任务而非恢复旧任务,以获取最新内容。

## S3 兼容存储

配置 `s3Destination`(例如 `s3://example/site/`),可选 `s3Endpoint` 指向
HTTPS 兼容服务。工作进程 PATH 需安装 AWS CLI,使用它的标准环境或配置
文件提供凭据。目标前缀应专用于静态网站;同步会删除该前缀中源目录不存在
的文件。当前为逐对象同步,非原子切换,中断可能留下混合版本,需重试修复。
需要原子发布时请使用本地版本切换、Git 或支持版本化部署的托管目标。

## Bunny Storage 发布

配置 `bunnyZone`、不含首尾斜杠的专用目录 `bunnyPrefix`,以及可选的
区域存储主机 `bunnyHostname`。工作进程通过 `BUNNY_STORAGE_PASSWORD`
读取存储密码,不能使用普通账户 API Key 替代存储密码。
全部上传成功后才枚举并删除专用目录中的旧文件。上传使用 SHA-256 校验,
不跟随 HTTP 重定向。同步不是原子操作;需另行清除 CDN 缓存并核对线上结果,
当前完成状态只代表存储同步,不能代表所有边缘节点已更新。

## SFTP 发布

配置 `sftpHost`、`sftpUsername`、`sftpDestination`,可选 `sftpPort`(默认 22)。
`sftpFingerprint` 必须为通过可信渠道确认的服务器主机公钥 SHA-256
十六进制摘要(64 个字符)。未匹配的主机密钥会拒绝连接。

工作进程环境通过 `STATIC_SFTP_KEY_FILE` 指定私钥文件,可选
`STATIC_SFTP_KEY_PASSPHRASE`,或使用 `STATIC_SFTP_PASSWORD`。凭据不保存在
后台设置中。目标路径必须为专用目录,父目录需已存在且允许写入。

上传先写入同级暂存目录,完成后将旧目录改名为备份,再将新目录改名到
目标路径。第二次改名失败时尝试恢复旧目录。两次改名之间有短暂不可用
窗口,所以不能称为原子发布;旧备份和失败暂存目录暂时保留,便于恢复。
目前测试使用模拟客户端,尚未在真实 SFTP 服务完成验收。

## 单页、增量与完整快照

后台「更新单页」填写一个已完成任务 ID 和页面地址,或执行:

```sh
npx --no-install firekylin-static update-page --id BASELINE_ID --url /post/example.html --store .firekylin/simply-static/jobs
```

任务沿用基线配置,更新目标、首页、直接引用目标的页面、Feed/Sitemap 和
资源文件;复用其余 HTML 下载检查点,然后重新生成完整快照、搜索索引和 ZIP。
目标明确返回 404/410 时移除该页。主题、菜单、全局挂件、权限或列表布局变化
应执行全站导出,不应使用单页模式。发布仍传输完整快照,单页模式优化抓取阶段。

`incremental` 使用 HTTP ETag 条件请求减少下载;源站未提供 ETag 时正常下载。
每次全站导出重新发现链接,因此不会沿用上次已删除的页面。CLI 默认只以首页
为入口,`additionalUrls` 可加入 Sitemap、RSS、robots.txt、多语言首页和插件路由。
隐藏在脚本运行结果中、未被 HTML/Sitemap 引用的地址也应明确添加。

可选 `errorPageUrl` 指向源站的一个不存在页面,允许其返回 404 并导出为
`404.html`(不加入搜索索引);静态托管平台需要配置使用这个错误页。
robots.txt 的 Sitemap 地址随目标地址改写,其他爬虫规则保持原样。

## 执行与资源边界

同一个任务目录只允许一个工作进程执行导出/发布,避免旧快照覆盖新版本。
中断抓取可恢复;中断发布必须先检查远端是否已生效。不同任务目录不共享锁,
不要让它们同时发布到同一目标。取消发布可能发生在远端已接受请求之后,应核对
远端状态;取消不等于回滚。

默认限制为 10,000 个资源、单资源 32 MiB、总下载 512 MiB,每次下载 60 秒,
暂时性错误最多重试两次。可通过 CLI 配置 `maxPages`、`maxResourceBytes`、
`maxTotalBytes`、`timeout`。导出器在内存中解析并压缩 ZIP,内存应明显大于
总下载限制;大型站点应降低单次范围或提高工作进程内存。额外文件有独立大小限制。

队列、检查点、日志和本地历史发布不会自动清理,请按保留策略清理已结束任务。
若进程恰在入队时崩溃留下 `.enqueue-lock`,停止所有使用该目录的 Web/worker
进程后再删除该锁;不要在进程仍运行时强行解锁。

## 运行测试

仓库根目录执行 `pnpm test`。可选真实 Firekylin 集成测试:

```sh
FIREKYLIN_CORE_PATH=/absolute/path/to/waterkylin STATIC_LIVE_TEST=1 pnpm --filter firekylin-plugin-simply-static test
```

集成测试创建临时 SQLite 网站与独立服务,不使用线上管理员 Cookie 或线上数据。
18 changes: 18 additions & 0 deletions packages/simply-static/assets/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

for (const container of document.querySelectorAll('.fk-comment-container[data-comment-config]')) {
const config = JSON.parse(container.dataset.commentConfig);
const resources = config.resources;
delete config.resources;
config.el = container;
const style = document.createElement('link');
style.rel = 'stylesheet';
style.href = resources.style;
document.head.appendChild(style);
import(resources.script).then(client => {
container.textContent = '';
client.init(config);
}).catch(() => {
container.textContent = config.loadError || '评论加载失败';
});
}
30 changes: 30 additions & 0 deletions packages/simply-static/assets/search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
:root {
color-scheme: light dark;
font-family: system-ui, sans-serif;
line-height: 1.6;
}
main {
max-width: 48rem;
margin: 3rem auto;
padding: 0 1rem;
}
form {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
align-items: center;
}
input {
flex: 1;
min-width: 10rem;
padding: 0.6rem;
}
button {
padding: 0.6rem 1rem;
}
li {
margin-bottom: 1.5rem;
}
p {
overflow-wrap: anywhere;
}
22 changes: 22 additions & 0 deletions packages/simply-static/assets/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>站内搜索</title>
<link rel="stylesheet" href="static-search.css">
<script src="static-search.js" defer></script>
</head>
<body>
<main>
<h1>站内搜索</h1>
<form data-static-search role="search">
<label for="query">搜索文章</label>
<input id="query" name="q" type="search" autocomplete="off">
<button type="submit">搜索</button>
</form>
<p data-search-status role="status">正在加载搜索索引…</p>
<ol data-search-results></ol>
</main>
</body>
</html>
Loading
Loading