import * as fs from "node:fs";
import * as path from "node:path";
import { HandlebarsTemplateEngine } from "../template/HandlebarsTemplateEngine";
import type { CireConfig } from "../types";
interface {
: string;
: string;
?: string;
: "file" | "directory";
?: TreeNode[];
?: string;
}
Generates index pages using Handlebars template system for better customization.
export class {
private : HandlebarsTemplateEngine;
(: CireConfig) {
const defaultTemplateDir = path.join(__dirname, "../../templates");
const templateDir = config.template?.templateDir || defaultTemplateDir;
this.templateEngine = new HandlebarsTemplateEngine(templateDir);
}
Build tree structure from file paths
private (: string[]): TreeNode {
const root: TreeNode = {
name: "root",
path: "",
type: "directory",
children: [],
};
for (const file of files) {
const parts = file.split("/").filter((part) => part.length > 0);
let current = root;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const fullPath = parts.slice(0, i + 1).join("/");
const isLast = i === parts.length - 1;
let existingChild = current.children?.find(
(child) => child.name === part,
);
if (!existingChild) {
existingChild = {
: part,
: fullPath,
: isLast
? ``
: undefined,
: isLast ? "file" : "directory",
: isLast ? undefined : [],
};
if (!current.children) {
current.children = [];
}
current.children.push(existingChild);
}
current = existingChild;
}
}
return root;
}
Generate navigation page using Handlebars template system
(: string[], : CireConfig): string {
const tree = this.buildTree(files);
const totalFiles = files.length;
const directories = new Set(files.map((f) => path.dirname(f))).size;
// Prepare template data
const templateData = {
: ` - Documentation`,
: "", // 导航页面不需要额外内容,信息都在 header 中显示
: config.name,
:
config.description ||
"Static website generator providing IDE-like experiences for documentation, generated with <a href='https://github.com/Eric-Song-Nop/cire'>Cire</a>",
,
,
: config.input.language || "typescript",
: ["./default.css"],
: {
: false,
: false,
: false,
: false,
: true,
},
,
: "index",
};
// Render using the index layout
return this.templateEngine.render("index", templateData);
}
Save navigation page to output directory
async (
: string,
: string,
: string = "cireIndex.html",
): Promise<void> {
const indexPath = path.join(outputDir, fileName);
fs.writeFileSync(indexPath, html, "utf-8");
}
}