obsidian-visualiser/server/tasks/sync.ts

148 lines
5.8 KiB
TypeScript

import useDatabase from "~/composables/useDatabase";
import { extname, basename } from 'node:path';
import type { File, FileType } from '~/types/api';
import { CanvasColor, CanvasContent } from "~/types/canvas";
import useMarkdown from "~/composables/useMarkdown";
const typeMapping: Record<string, FileType> = {
".md": "Markdown",
".canvas": "Canvas"
}
export default defineTask({
meta: {
name: 'sync',
description: 'Synchronise the project 1 with Obsidian',
},
async run(event) {
try {
const tree = await $fetch('https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/git/trees/master', {
method: 'get',
headers: {
accept: 'application/json',
},
params: {
recursive: true,
per_page: 1000,
}
}) as any;
const files: File[] = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => {
if(e.type === 'tree')
{
const title = basename(e.path);
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
return {
path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
order: order && order[1] ? order[1] : 50,
title: order && order[2] ? order[2] : title,
type: 'Folder',
content: null
}
}
const extension = extname(e.path);
const title = basename(e.path, extension);
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`));
return {
path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
order: order && order[1] ? order[1] : 50,
title: order && order[2] ? order[2] : title,
type: (typeMapping[extension] ?? 'File'),
content: reshapeContent(content as string, typeMapping[extension] ?? 'File')
}
}));
/*let tags = [];
const tagFile = files.find(e => e.path === "tags");
if(tagFile)
{
useMarkdown()(tagFile.content);
tags.push()
}*/
const db = useDatabase();
const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[];
const remove = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`);
db.transaction(data => data.forEach(e => remove.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path)));
remove.finalize();
const insert = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`);
const update = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`);
const transaction = db.transaction((content) => {
for (const item of content) {
let order = item.order;
if (typeof order === 'string')
order = parseInt(item.order, 10);
if (isNaN(order))
order = 999;
if(oldFiles.find(e => item.path === e.path))
{
console.log(`Updating ${item.title} (${item.path})`);
update.run({ $path: item.path, $content: item.content });
}
else
{
/* console.log(`Inserting ${item.title}`) */
insert.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order });
}
}
});
transaction(files);
insert.finalize();
update.finalize();
useStorage('cache').clear();
return { result: true };
}
catch(e)
{
console.error(e);
return { result: false };
}
},
})
function reshapeContent(content: string, type: FileType): string | null
{
switch(type)
{
case "Markdown":
case "File":
return content;
case "Canvas":
const data = JSON.parse(content) as CanvasContent;
data.edges.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
data.nodes.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
return JSON.stringify(data);
default:
case 'Folder':
return null;
}
}
function getColor(color: string): CanvasColor
{
const colors: Record<string, string> = {
'1': 'red',
'2': 'orange',
'3': 'yellow',
'4': 'green',
'5': 'cyan',
'6': 'purple',
};
if(colors.hasOwnProperty(color))
return { class: colors[color] };
else
return { hex: color };
}