From cda536e4da03de192545aafc223ebe3d4ac125a7 Mon Sep 17 00:00:00 2001 From: tecc Date: Mon, 1 May 2023 20:09:55 +0200 Subject: [PATCH] fix(170-escape-note-names): Escape title names when exporting --- pages/api/export.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pages/api/export.ts b/pages/api/export.ts index 11cbdaf..1b7f9a3 100644 --- a/pages/api/export.ts +++ b/pages/api/export.ts @@ -11,6 +11,11 @@ import { NOTE_DELETED } from 'libs/shared/meta'; import { metaToJson } from 'libs/server/meta'; import { toBuffer } from 'libs/shared/str'; +export function escapeFileName(name: string): string { + // list of characters taken from https://www.mtu.edu/umc/services/websites/writing/characters-avoid/ + return name.replace(/[#%&{}\\<>*?/$!'":@+`|=]/g, "_"); +} + export default api() .use(useAuth) .use(useStore) @@ -33,16 +38,16 @@ export default api() if (metaJson.deleted === NOTE_DELETED.DELETED) { return; } - const title = metaJson.title ?? 'Untitled'; + const title = escapeFileName(metaJson.title ?? 'Untitled'); const resolvedPrefix = prefix.length === 0 ? '' : prefix + '/'; const basePath = resolvedPrefix + title; const uniquePath = duplicate[basePath] ? `${basePath} (${duplicate[basePath]})` : basePath; + duplicate[basePath] = (duplicate[basePath] ?? 0) + 1; zip.addFile(`${uniquePath}.md`, toBuffer(note.content)); - duplicate[basePath] = (duplicate[basePath] || 0) + 1; await Promise.all(item.children.map((v) => addItem(v, uniquePath))); }