From 6d167960945d4d99f992b5f213a50ffb51c68a2f Mon Sep 17 00:00:00 2001 From: ANIRUDDHA ADAK Date: Mon, 7 Sep 2026 08:40:19 +0530 Subject: [PATCH] fix(docs): decline Windows reserved names for auto file naming (#242) Co-authored-by: Aniruddha Adak --- apps/docs/src/renderer/file-actions.ts | 6 +++++- apps/docs/tests/auto-file-name.test.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 apps/docs/tests/auto-file-name.test.ts diff --git a/apps/docs/src/renderer/file-actions.ts b/apps/docs/src/renderer/file-actions.ts index 3c3ec79..45574a7 100644 --- a/apps/docs/src/renderer/file-actions.ts +++ b/apps/docs/src/renderer/file-actions.ts @@ -514,7 +514,7 @@ function pmNodeText(node: PmNode): string { } /** Sanitize a heading into a safe filename base: strip illegal path chars, collapse whitespace, cap length; null if invalid. (Mirrors slides' draft naming.) */ -function sanitizeFileBaseName(raw: string): string | null { +export function sanitizeFileBaseName(raw: string): string | null { const cleaned = raw // eslint-disable-next-line no-control-regex -- stripping control chars is the point here .replace(/[\\/:*?"<>|\u0000-\u001f]/g, ' ') @@ -524,6 +524,10 @@ function sanitizeFileBaseName(raw: string): string | null { .replace(/^\.+|\.+$/g, '') .trim() if (!cleaned) return null + // Windows device names stay reserved with an extension (CON.docx is still + // CON): decline them so the first save keeps the Untitled name instead of + // proposing a file Windows cannot create (same family as the pdf/shell guards). + if (/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(cleaned)) return null return cleaned.length > 40 ? cleaned.slice(0, 40).trim() : cleaned } diff --git a/apps/docs/tests/auto-file-name.test.ts b/apps/docs/tests/auto-file-name.test.ts new file mode 100644 index 0000000..5b25c52 --- /dev/null +++ b/apps/docs/tests/auto-file-name.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from 'vitest' + +import { sanitizeFileBaseName } from '../src/renderer/file-actions' + +describe('sanitizeFileBaseName', () => { + it('keeps an ordinary heading as the file base', () => { + expect(sanitizeFileBaseName('Quarterly report')).toBe('Quarterly report') + }) + + it('declines Windows reserved names so the save keeps its Untitled name', () => { + for (const reserved of ['CON', 'prn', 'AUX', 'nul', 'COM1', 'com9', 'LPT1', 'lpt9']) { + expect(sanitizeFileBaseName(reserved)).toBeNull() + } + expect(sanitizeFileBaseName('My CON file')).toBe('My CON file') + expect(sanitizeFileBaseName('com10')).toBe('com10') + }) +})