Skip to content

Commit 636f22e

Browse files
authored
chore: Make epub-export.ts Support Multi-Language (#258)
1 parent 9ec9d40 commit 636f22e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ jobs:
3333
nix develop .#default --ignore-environment --command bash -c '
3434
set -e
3535
pnpm install
36-
pnpm run export-epub
36+
pnpm run export-epub -- --lang=en
37+
pnpm run export-epub -- --lang=zh
3738
'
3839
# # For debugging, upload the pdfs as artifacts
3940
# - uses: actions/upload-artifact@v3
@@ -58,4 +59,5 @@ jobs:
5859
body_path: CHANGELOG
5960
files: |
6061
nixos-and-flakes-book.pdf
61-
nixos-and-flakes-book.epub
62+
nixos-and-flakes-book.en.epub
63+
nixos-and-flakes-book.zh.epub

epub-export.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import fs from "fs"
33
import path from "path"
44
import { exec } from "child_process"
55

6+
// Usage: tsx epub-export.ts --lang en
7+
// You can also set LANG env var (e.g., LANG=zh). Defaults to 'en'.
8+
const argLang = process.argv.find((a) => a.startsWith("--lang="))?.split("=")[1]
9+
const envLang = process.env.LANG?.split(".")[0] // LANG like en_US.UTF-8
10+
let lang =
11+
argLang || (envLang && /^[a-zA-Z]{2}$/.test(envLang) ? envLang : undefined) || "en"
12+
lang = lang.toLowerCase()
13+
if (!fs.existsSync(path.join("docs", lang))) {
14+
console.error(`❌ Language directory not found: docs/${lang}`)
15+
process.exit(1)
16+
}
17+
console.log(`🌐 Using language: ${lang}`)
18+
// Map for metadata (Pandoc expects BCP 47). For Simplified Chinese use zh-Hans.
19+
const metaLang = lang === "zh" ? "zh-Hans" : lang
20+
621
const sidebar: {
722
text: string
823
items?: { text: string; link: string }[]
@@ -104,7 +119,7 @@ for (const category of sidebar) {
104119
if (category.items) {
105120
for (const item of category.items) {
106121
if (item.link && item.link.endsWith(".md")) {
107-
const filePath = path.join("en", item.link).replace(/\\/g, "/")
122+
const filePath = path.join(lang, item.link).replace(/\\/g, "/")
108123
fileList.push(filePath)
109124
}
110125
}
@@ -146,7 +161,7 @@ pre > code.sourceCode { white-space: pre; } /* don’t pre-
146161
fs.writeFileSync(path.join(tempDir, "epub-fixes.css"), css)
147162

148163
// --- Run Pandoc ---
149-
const outputFileName = "../nixos-and-flakes-book.epub"
164+
const outputFileName = `../nixos-and-flakes-book.${lang}.epub`
150165
const pandocCommand = `pandoc ${fileList.join(" ")} \
151166
-o ${outputFileName} \
152167
--from=markdown+gfm_auto_identifiers+pipe_tables+raw_html+tex_math_dollars+fenced_code_blocks+fenced_code_attributes \
@@ -159,7 +174,8 @@ const pandocCommand = `pandoc ${fileList.join(" ")} \
159174
--css=epub-fixes.css \
160175
--metadata=title:"NixOS and Flakes Book" \
161176
--metadata=author:"Ryan Yin" \
162-
--resource-path=.:../docs/public:en`
177+
--metadata=lang:${metaLang} \
178+
--resource-path=.:../docs/public:${lang}`
163179

164180
console.log("🚀 Executing pandoc:", pandocCommand)
165181

0 commit comments

Comments
 (0)