Merge pull request #171 from notea-org/fix/170-escape-note-names

#170: Escape note titles when exporting to zip files
This commit is contained in:
tecc 2023-05-01 20:49:40 +02:00 committed by GitHub
commit 31e466eff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,11 @@ import { NOTE_DELETED } from 'libs/shared/meta';
import { metaToJson } from 'libs/server/meta'; import { metaToJson } from 'libs/server/meta';
import { toBuffer } from 'libs/shared/str'; 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() export default api()
.use(useAuth) .use(useAuth)
.use(useStore) .use(useStore)
@ -33,16 +38,16 @@ export default api()
if (metaJson.deleted === NOTE_DELETED.DELETED) { if (metaJson.deleted === NOTE_DELETED.DELETED) {
return; return;
} }
const title = metaJson.title ?? 'Untitled'; const title = escapeFileName(metaJson.title ?? 'Untitled');
const resolvedPrefix = prefix.length === 0 ? '' : prefix + '/'; const resolvedPrefix = prefix.length === 0 ? '' : prefix + '/';
const basePath = resolvedPrefix + title; const basePath = resolvedPrefix + title;
const uniquePath = duplicate[basePath] const uniquePath = duplicate[basePath]
? `${basePath} (${duplicate[basePath]})` ? `${basePath} (${duplicate[basePath]})`
: basePath; : basePath;
duplicate[basePath] = (duplicate[basePath] ?? 0) + 1;
zip.addFile(`${uniquePath}.md`, toBuffer(note.content)); zip.addFile(`${uniquePath}.md`, toBuffer(note.content));
duplicate[basePath] = (duplicate[basePath] || 0) + 1;
await Promise.all(item.children.map((v) => addItem(v, uniquePath))); await Promise.all(item.children.map((v) => addItem(v, uniquePath)));
} }