-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbuild.js
More file actions
51 lines (41 loc) · 1.5 KB
/
Copy pathbuild.js
File metadata and controls
51 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require("fs");
const path = require("path");
const BOOKS_DIR = path.join(__dirname, "books/");
const TEMPLATE_FILE = "template.html";
// 1. Repo URL Logic - Accurate and filtered
let repoUrl = "#";
if (process.env.GITHUB_REPOSITORY) {
repoUrl = `https://github.com/${process.env.GITHUB_REPOSITORY}`;
} else if (process.env.REPOSITORY_URL) {
repoUrl = process.env.REPOSITORY_URL;
}
try {
if (!fs.existsSync(TEMPLATE_FILE)) throw new Error("template.html missing");
const TEMPLATE = fs.readFileSync(TEMPLATE_FILE, "utf8");
// 2. Original Book Scanning Logic
const files = fs
.readdirSync(BOOKS_DIR)
.filter(f => f.toLowerCase().endsWith(".pdf"))
.sort();
// 3. Generate <a> tags (The template handles the Card UI)
const listItems = files.map(file => {
const encoded = encodeURIComponent(file);
// Preserving your exact naming logic
const displayName = file
.replace(/\.pdf$/i, "")
.replace(/_/g, " ")
.replace(/[_-]+/g, " ")
.replace(/\s+/g, " ")
.trim();
return `<a href="books/${encoded}">${displayName}</a>`;
}).join("\n");
// 4. Injection
let output = TEMPLATE
.replace("{{BOOK_LIST}}", listItems)
.replace("{{REPO_URL}}", repoUrl);
fs.writeFileSync("index.html", output);
console.log(`Successfully built library for: ${repoUrl}`);
} catch (err) {
console.error("Build failed:", err.message);
process.exit(1);
}