You've already forked obsidian-visualiser
Add sync, Tree, Markdown, content editor.
This commit is contained in:
@@ -2,20 +2,20 @@ import useDatabase from "~/composables/useDatabase";
|
||||
import { extname, basename } from 'node:path';
|
||||
import type { File, FileType, Tag } from '~/types/api';
|
||||
import type { CanvasColor, CanvasContent } from "~/types/canvas";
|
||||
import { explorerContentTable } from "~/db/schema";
|
||||
|
||||
const typeMapping: Record<string, FileType> = {
|
||||
".md": "Markdown",
|
||||
".canvas": "Canvas"
|
||||
".md": "markdown",
|
||||
".canvas": "canvas"
|
||||
};
|
||||
|
||||
export default defineTask({
|
||||
meta: {
|
||||
name: 'sync',
|
||||
description: 'Synchronise the project 1 with Obsidian',
|
||||
description: 'Synchronise the project with Obsidian',
|
||||
},
|
||||
async run(event) {
|
||||
console.log('Task "sync" executed');
|
||||
/* try {
|
||||
try {
|
||||
const tree = await $fetch('https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/git/trees/master', {
|
||||
method: 'get',
|
||||
headers: {
|
||||
@@ -27,7 +27,8 @@ export default defineTask({
|
||||
}
|
||||
}) as any;
|
||||
|
||||
const files: File[] = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => {
|
||||
|
||||
const files: typeof explorerContentTable.$inferInsert = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => {
|
||||
if(e.type === 'tree')
|
||||
{
|
||||
const title = basename(e.path);
|
||||
@@ -35,10 +36,13 @@ export default defineTask({
|
||||
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,
|
||||
//order: order && order[1] ? order[1] : 50,
|
||||
title: order && order[2] ? order[2] : title,
|
||||
type: 'Folder',
|
||||
content: null
|
||||
type: 'folder',
|
||||
content: null,
|
||||
owner: '1',
|
||||
navigable: true,
|
||||
private: e.path.startsWith('98.Privé')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,14 +54,17 @@ export default defineTask({
|
||||
|
||||
return {
|
||||
path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
||||
order: order && order[1] ? order[1] : 50,
|
||||
//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')
|
||||
type: (typeMapping[extension] ?? 'file'),
|
||||
content: reshapeContent(content as string, typeMapping[extension] ?? 'File'),
|
||||
owner: '1',
|
||||
navigable: true,
|
||||
private: e.path.startsWith('98.Privé')
|
||||
}
|
||||
}));
|
||||
|
||||
let tags: Tag[] = [];
|
||||
/*let tags: Tag[] = [];
|
||||
const tagFile = files.find(e => e.path === "tags");
|
||||
|
||||
if(tagFile)
|
||||
@@ -70,11 +77,13 @@ export default defineTask({
|
||||
const end = titles.length === i + 1 ? tagFile.content.length : titles[i + 1].position.start.offset - 1;
|
||||
tags.push({ tag: titles[i].properties.id, description: tagFile.content.substring(titles[i].position?.end.offset + 1, end) });
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
const db = useDatabase();
|
||||
db.delete(explorerContentTable).run();
|
||||
db.insert(explorerContentTable).values(files).run();
|
||||
|
||||
const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[];
|
||||
/*const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[];
|
||||
const removeFiles = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`);
|
||||
db.transaction((data: File[]) => data.forEach(e => removeFiles.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path)));
|
||||
removeFiles.finalize();
|
||||
@@ -118,7 +127,7 @@ export default defineTask({
|
||||
})(tags);
|
||||
|
||||
insertTags.finalize();
|
||||
updateTags.finalize();
|
||||
updateTags.finalize();*/
|
||||
|
||||
useStorage('cache').clear();
|
||||
|
||||
@@ -126,8 +135,8 @@ export default defineTask({
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
return { result: false };
|
||||
} */
|
||||
return { result: false, error: e };
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -135,16 +144,16 @@ function reshapeContent(content: string, type: FileType): string | null
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case "Markdown":
|
||||
case "File":
|
||||
case "markdown":
|
||||
case "file":
|
||||
return content;
|
||||
case "Canvas":
|
||||
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':
|
||||
case 'folder':
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user