mirror of
https://github.com/QingWei-Li/notea.git
synced 2024-11-26 00:09:59 +03:00
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:
commit
31e466eff5
@ -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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user