Files
up/scripts/sync-word-lists.mjs

84 lines
2.9 KiB
JavaScript

#!/usr/bin/env node
import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
import { basename, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const SOURCE = join(ROOT, "docs/threads/word-list");
const TARGET = join(ROOT, "docs/en/threads/word-list");
const checkOnly = process.argv.includes("--check");
function stripFrontmatter(source) {
return source
.replace(/^---\n[\s\S]*?\n---\n+/, "")
.split("\n本页是查阅清单")[0]
.trim();
}
function chinesePage(source) {
const body = stripFrontmatter(source);
const title = body.match(/^#\s+(.+)$/m)?.[1] || "Word List";
return `---
title: ${title} 词表
description: ${title} 主题的高价值英文术语参考表;应从真实任务中选择词条并练习发音、搭配、回忆与语境使用。
updated: 2026-08-16
---
${body}
本页是查阅清单,不是学习数量目标。请结合 [词汇篇](../part-1/2-vocabulary.md) 从真实任务中选择词条,并练习发音、释义、搭配、主动回忆和语境使用。
`;
}
function englishPage(source) {
const body = stripFrontmatter(source);
const title = body.match(/^#\s+(.+)$/m)?.[1] || "Word List";
return `---
title: ${title} Word List
description: An English reference list of high-value ${title} terms. The terms are shared with the Chinese edition from one maintained source.
updated: 2026-08-16
---
<!-- Generated by scripts/sync-word-lists.mjs; edit docs/threads/word-list instead. -->
${body}
This is a reference list, not a learning target. Select terms from a real task, then practise pronunciation, meaning, collocation, retrieval, and contextual use with the [Vocabulary chapter](../part-1/2-vocabulary.md).
`;
}
mkdirSync(TARGET, { recursive: true });
let changed = false;
for (const name of readdirSync(SOURCE).filter((file) => file.endsWith(".md"))) {
const sourceFile = join(SOURCE, name);
const targetFile = join(TARGET, basename(name));
const source = readFileSync(sourceFile, "utf8");
const expectedSource = chinesePage(source);
const expected = englishPage(expectedSource);
if (source !== expectedSource) {
changed = true;
if (checkOnly) {
console.error(`${sourceFile.replace(`${ROOT}/`, "")}: 中文词表元数据未同步`);
} else {
writeFileSync(sourceFile, expectedSource);
console.log(`updated ${sourceFile.replace(`${ROOT}/`, "")}`);
}
}
let actual = "";
try {
actual = readFileSync(targetFile, "utf8");
} catch {}
if (actual === expected) continue;
changed = true;
if (checkOnly) {
console.error(`${targetFile.replace(`${ROOT}/`, "")}: 英文词表未同步`);
} else {
writeFileSync(targetFile, expected);
console.log(`updated ${targetFile.replace(`${ROOT}/`, "")}`);
}
}
if (checkOnly && changed) process.exit(1);
if (!changed) console.log("English word lists are in sync");