Update generate-index.mjs
This commit is contained in:
committed by
GitHub
parent
7228a78548
commit
9b61e82745
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
import fs from "fs/promises";
|
import { promises as fs } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
const ROOT = "public";
|
const ROOT = "public";
|
||||||
const OUT = path.join(ROOT, "index.json");
|
const OUT = path.join(ROOT, "index.json");
|
||||||
@@ -8,7 +8,7 @@ const MAX_BYTES = 64 * 1024;
|
|||||||
|
|
||||||
function dateFromName(name) {
|
function dateFromName(name) {
|
||||||
const m = name.match(/^(\d{4}-\d{2}-\d{2})/);
|
const m = name.match(/^(\d{4}-\d{2}-\d{2})/);
|
||||||
return m?new Date(m[1]).getTime():null;
|
return m ? new Date(m[0]).getTime() : null;
|
||||||
}
|
}
|
||||||
async function readHead(abs) {
|
async function readHead(abs) {
|
||||||
const fh = await fs.open(abs, "r");
|
const fh = await fs.open(abs, "r");
|
||||||
@@ -24,44 +24,4 @@ function parseTitle(raw,ext){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function walk(relBase = "") {
|
async function walk(relBase = "") {
|
||||||
const abs=path.join(ROOT,relBase);
|
const abs = path.join
|
||||||
const entries=await fs.readdir(abs,{withFileTypes:true});
|
|
||||||
const dir={type:"dir",name:path.basename(relBase)||"",path:relBase,children:[]};
|
|
||||||
for(const e of entries){
|
|
||||||
if(e.name.startsWith(".")) continue;
|
|
||||||
const rel=path.posix.join(relBase,e.name);
|
|
||||||
const absPath=path.join(ROOT,rel);
|
|
||||||
if(e.isDirectory()){
|
|
||||||
const top=rel.split("/")[0];
|
|
||||||
if(STATIC_TOPLEVEL.has(top)){continue;}
|
|
||||||
const child=await walk(rel);
|
|
||||||
dir.children.push(child);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const ext=path.extname(e.name);
|
|
||||||
if(![".md",".html"].includes(ext)) continue;
|
|
||||||
const st=await fs.stat(absPath);
|
|
||||||
const raw=await readHead(absPath);
|
|
||||||
const title=parseTitle(raw,ext)||e.name;
|
|
||||||
const date=dateFromName(e.name);
|
|
||||||
dir.children.push({
|
|
||||||
type:"file",
|
|
||||||
name:e.name,
|
|
||||||
title,
|
|
||||||
path:rel,
|
|
||||||
ext,
|
|
||||||
pinned:rel.startsWith("pinned/"),
|
|
||||||
mtime:date||st.mtimeMs
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
(async()=>{
|
|
||||||
const tree=await walk();
|
|
||||||
const flat=[];
|
|
||||||
(function flatten(n){for(const c of n.children){if(c.type==="file")flat.push(c);else flatten(c);}})(tree);
|
|
||||||
const sections=[...new Set(flat.map(f=>f.path.split("/")[0]))];
|
|
||||||
await fs.writeFile(OUT,JSON.stringify({tree:tree.children,flat,sections},null,2));
|
|
||||||
console.log("index.json built:",OUT);
|
|
||||||
})();
|
|
||||||
Reference in New Issue
Block a user