obsidian-visualiser/server/api/project/[projectId]/navigation.get.ts

52 lines
1.2 KiB
TypeScript

import useDatabase from '~/composables/useDatabase';
export interface Navigation
{
title: string;
path: string;
type: string;
order: number
children?: Navigation[];
}
export default defineEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
if(!project)
{
setResponseStatus(e, 404);
return;
}
const db = useDatabase();
const content = db.query(`SELECT "path", "title", "type", "order" FROM explorer_files WHERE project = ?1 and navigable = 1`).all(project!).sort((a: any, b: any) => a.path.length - b.path.length) as any[];
if(content.length > 0)
{
const navigation: Navigation[] = [];
for(const item of content)
addChild(navigation, item);
return navigation;
}
setResponseStatus(e, 404);
});
function addChild(arr: Navigation[], e: Navigation): void
{
const parent = arr.find(f => e.path.startsWith(f.path));
if(parent)
{
if(!parent.children)
parent.children = [];
addChild(parent.children, e);
}
else
{
arr.push({ title: e.title, path: e.path, type: e.type, order: e.order });
}
}