From 0abf0b11e611404c005976b4980e3922f8bf0ad6 Mon Sep 17 00:00:00 2001 From: Peaceultime Date: Tue, 17 Dec 2024 20:36:51 +0100 Subject: [PATCH] Fix folder editing and add links updates on file rename --- schemas/project.ts | 2 +- server/api/file.post.ts | 2 +- server/api/file/content/[path].get.ts | 5 ----- server/api/project.post.ts | 11 +++++++++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/schemas/project.ts b/schemas/project.ts index c02a7fd..892771e 100644 --- a/schemas/project.ts +++ b/schemas/project.ts @@ -10,7 +10,7 @@ const baseItem = z.object({ navigable: z.boolean(), private: z.boolean(), order: z.number().finite(), - content: z.string().optional(), + content: z.string().optional().or(z.null()), }); export const item: z.ZodType = baseItem.extend({ children: z.lazy(() => item.array().optional()), diff --git a/server/api/file.post.ts b/server/api/file.post.ts index e76f567..83a2dad 100644 --- a/server/api/file.post.ts +++ b/server/api/file.post.ts @@ -31,7 +31,7 @@ export function convertToStorableLinks(content: string, path: string[]): string return content.replaceAll(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/g, (e: string, a1?: string, a2?: string , a3?: string) => { const parsed = parsePath(a1 ?? '%%%%----%%%%----%%%%'); const replacer = path.find(e => e.endsWith(parsed)); - const value = `[[${replacer ?? ''}${a2 ?? ''}${(!a3 && replacer !== parsed ? '|' + a1 : a3) ?? ''}]]`; + const value = `[[${a1 ? (replacer ?? '') : ''}${a2 ?? ''}${(!a3 && a1 && replacer !== parsed ? '|' + a1 : a3) ?? ''}]]`; return value; }); } \ No newline at end of file diff --git a/server/api/file/content/[path].get.ts b/server/api/file/content/[path].get.ts index ae0c41f..90b2fe3 100644 --- a/server/api/file/content/[path].get.ts +++ b/server/api/file/content/[path].get.ts @@ -50,11 +50,6 @@ export default defineEventHandler(async (e) => { content.content = convertFromStorableLinks(content.content); } - if(session && session.user && session.user.id !== content.owner) - { - content.content = content.content.replace(/%%(.+)%%/g, ""); - } - return { content: content.content }; } diff --git a/server/api/project.post.ts b/server/api/project.post.ts index 4490cfb..8cf43ce 100644 --- a/server/api/project.post.ts +++ b/server/api/project.post.ts @@ -3,7 +3,7 @@ import useDatabase from '~/composables/useDatabase'; import { explorerContentTable } from '~/db/schema'; import { project, type ProjectItem } from '~/schemas/project'; import { parsePath } from "#shared/general.utils"; -import { eq, getTableColumns } from "drizzle-orm"; +import { eq, getTableColumns, sql } from "drizzle-orm"; export default defineEventHandler(async (e) => { const { user } = await getUserSession(e); @@ -42,6 +42,8 @@ export default defineEventHandler(async (e) => { const item = items[i]; const old = full[item.match!]; + const path = [item.parent, parsePath(item.name === '' ? item.title : item.name)].filter(e => !!e).join('/'); + tx.insert(explorerContentTable).values({ path: item.path, owner: user.id, @@ -53,7 +55,7 @@ export default defineEventHandler(async (e) => { content: item.content ?? old.content, }).onConflictDoUpdate({ set: { - path: [item.parent, parsePath(item.name === '' ? item.title : item.name)].filter(e => !!e).join('/'), + path: path, title: item.title, type: item.type, navigable: item.navigable, @@ -64,6 +66,11 @@ export default defineEventHandler(async (e) => { }, target: explorerContentTable.path, }).run(); + + if(item.path !== path) + { + tx.update(explorerContentTable).set({ content: sql`replace(${explorerContentTable.content}, ${sql.placeholder('old')}, ${sql.placeholder('new')})` }).prepare().run({ 'old': item.path, 'new': path }); + } } for(let i = 0; i < full.length; i++) {