@@ -3,6 +3,21 @@ import fs from "fs"
33import path from "path"
44import { 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 - z A - 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+
621const 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-
146161fs . 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`
150165const 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
164180console . log ( "🚀 Executing pandoc:" , pandocCommand )
165181
0 commit comments