Notes/src/services/in_app_help.ts
2025-02-08 00:41:39 +02:00

87 lines
2.6 KiB
TypeScript

import path from "path";
import fs from "fs";
import type { HiddenSubtreeItem } from "./hidden_subtree.js";
import type NoteMeta from "./meta/note_meta.js";
import type { NoteMetaFile } from "./meta/note_meta.js";
import { fileURLToPath } from "url";
import { isDev } from "./utils.js";
export function getHelpHiddenSubtreeData() {
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const appDir = path.join(srcRoot, "public", isDev ? "app" : "app-dist");
const helpDir = path.join(appDir, "doc_notes", "en", "User Guide");
const metaFilePath = path.join(helpDir, "!!!meta.json");
const metaFileContent = JSON.parse(fs.readFileSync(metaFilePath).toString("utf-8"));
try {
return parseNoteMetaFile(metaFileContent as NoteMetaFile);
} catch (e) {
console.warn(e);
return [];
}
}
function parseNoteMetaFile(noteMetaFile: NoteMetaFile): HiddenSubtreeItem[] {
if (!noteMetaFile.files) {
return [];
}
const metaRoot = noteMetaFile.files[0];
const parsedMetaRoot = parseNoteMeta(metaRoot, "/" + (metaRoot.dirFileName ?? ""));
return parsedMetaRoot.children ?? [];
}
function parseNoteMeta(noteMeta: NoteMeta, docNameRoot: string): HiddenSubtreeItem {
let iconClass: string = "bx bx-file";
const item: HiddenSubtreeItem = {
id: `_help_${noteMeta.noteId}`,
title: noteMeta.title ?? "",
type: "doc", // can change
attributes: []
};
// Handle attributes
for (const attribute of noteMeta.attributes ?? []) {
if (attribute.name === "iconClass") {
iconClass = attribute.value;
}
}
// Handle folder notes
if (!noteMeta.dataFileName) {
iconClass = "bx bx-folder";
item.type = "book";
}
// Handle text notes
if (noteMeta.type === "text" && noteMeta.dataFileName) {
const docPath = `${docNameRoot}/${path.basename(noteMeta.dataFileName, ".html")}`
.substring(1);
item.attributes?.push({
type: "label",
name: "docName",
value: docPath
});
}
// Handle children
if (noteMeta.children) {
const children: HiddenSubtreeItem[] = [];
for (const childMeta of noteMeta.children) {
let newDocNameRoot = (noteMeta.dirFileName ? `${docNameRoot}/${noteMeta.dirFileName}` : docNameRoot);
children.push(parseNoteMeta(childMeta, newDocNameRoot));
}
item.children = children;
}
// Handle note icon
item.attributes?.push({
name: "iconClass",
value: iconClass,
type: "label"
});
return item;
}