Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): collapse a doubled markdown extension on sidebar rename (#3134)

Andrew Mikofalvy496a4be0198c
diff --git a/.changeset/rename-doubled-md-extension.md b/.changeset/rename-doubled-md-extension.md
new file mode 100644
index 000000000..7de91be4e
--- /dev/null
+++ b/.changeset/rename-doubled-md-extension.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Renaming a file in the Files sidebar no longer doubles its markdown extension. The rename box hands you the whole filename with only the name part selected, so typing or pasting a name that already ends in `.md` left the original `.md` in place and committed `notes.md.md`. That produced two files for one document: `notes.md.md` is filed under the document name `notes.md`, which resolves back to a different file, so the next save wrote the same document out again as a plain `notes.md` beside it. Both names also land on one row in the sidebar, so only one of the two files was visible there. A typed `.md` or `.mdx` now replaces the retained one instead of stacking on it. Names that merely contain dots (`v1.2.md`, `report.2026.md`) and non-document files are untouched. This stops new doubled names from being created; `.md.md` and `.mdx.mdx` files already on disk are left alone and still need renaming by hand.
diff --git a/packages/app/src/components/file-tree-rename-validation.test.ts b/packages/app/src/components/file-tree-rename-validation.test.ts
index b1ab6a74a..92cdcb4ff 100644
--- a/packages/app/src/components/file-tree-rename-validation.test.ts
+++ b/packages/app/src/components/file-tree-rename-validation.test.ts
@@ -269,3 +269,120 @@ describe('validateAndCoerceRenameDestination — asset paths', () => {
});
});
});
+
+describe('validateAndCoerceRenameDestination — doubled document extension', () => {
+ // Pierre pre-selects only the stem and leaves the extension in place, so a
+ // user who types a whole filename ending in `.md` commits `name.md` plus the
+ // retained `.md`. `name.md.md` on disk carries docName `name.md`, which maps
+ // back to the tree path of a DIFFERENT file — two files, one tree path.
+ test('typed name already ending in .md collapses the retained extension', () => {
+ expect(
+ validateAndCoerceRenameDestination(
+ 'brain/research/Untitled.md',
+ 'brain/research/codebase-audit-backlog-v2.md.md',
+ false,
+ ),
+ ).toEqual({
+ kind: 'allow',
+ destinationPath: 'brain/research/codebase-audit-backlog-v2.md',
+ });
+ });
+
+ test('typed name already ending in .mdx collapses the retained extension', () => {
+ expect(validateAndCoerceRenameDestination('foo.mdx', 'bar.mdx.mdx', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'bar.mdx',
+ });
+ });
+
+ test('typed .md over an .mdx source keeps the typed extension', () => {
+ expect(validateAndCoerceRenameDestination('foo.mdx', 'bar.md.mdx', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'bar.md',
+ });
+ });
+
+ test('typed .mdx over a .md source keeps the typed extension', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', 'bar.mdx.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'bar.mdx',
+ });
+ });
+
+ test('collapse preserves the casing the user typed', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', 'bar.MD.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'bar.MD',
+ });
+ });
+
+ test('collapse preserves the directory portion', () => {
+ expect(
+ validateAndCoerceRenameDestination(
+ 'meetings/2026/foo.md',
+ 'meetings/2026/notes.md.md',
+ false,
+ ),
+ ).toEqual({
+ kind: 'allow',
+ destinationPath: 'meetings/2026/notes.md',
+ });
+ });
+
+ test('a multi-dot basename whose stem is not a document extension is left alone', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', 'v1.2.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'v1.2.md',
+ });
+ });
+
+ test('a dotted name that merely looks like a version suffix is left alone', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', 'report.2026.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'report.2026.md',
+ });
+ });
+
+ test('an asset source is left alone even when the typed name carries a document extension', () => {
+ // Collapsing here would retitle an image to `notes.md`.
+ expect(
+ validateAndCoerceRenameDestination('media/image.png', 'media/notes.md.png', false),
+ ).toEqual({
+ kind: 'allow',
+ destinationPath: 'media/notes.md.png',
+ });
+ });
+
+ test('a doubled asset extension is left alone (no docName round-trip to break)', () => {
+ expect(
+ validateAndCoerceRenameDestination('media/photo.png', 'media/photo.png.png', false),
+ ).toEqual({
+ kind: 'allow',
+ destinationPath: 'media/photo.png.png',
+ });
+ });
+
+ test('an uppercase source extension still collapses its retained suffix', () => {
+ // Guard 1 case-folds; guard 2 matches the retained suffix verbatim, which
+ // is exactly the string sliced off the source. Lowercasing the retained
+ // suffix anywhere in that chain would silently stop collapse from firing.
+ expect(validateAndCoerceRenameDestination('foo.MD', 'bar.MD.MD', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'bar.MD',
+ });
+ });
+
+ test('only the one retained layer comes off a deliberately doubled typed name', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', 'v1.md.md.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: 'v1.md.md',
+ });
+ });
+
+ test('a stem-less destination is left alone (docName `.md` still resolves to its own file)', () => {
+ expect(validateAndCoerceRenameDestination('foo.md', '.md.md', false)).toEqual({
+ kind: 'allow',
+ destinationPath: '.md.md',
+ });
+ });
+});
diff --git a/packages/app/src/components/file-tree-rename-validation.ts b/packages/app/src/components/file-tree-rename-validation.ts
index 3a9921b9f..6d94a1d87 100644
--- a/packages/app/src/components/file-tree-rename-validation.ts
+++ b/packages/app/src/components/file-tree-rename-validation.ts
@@ -5,7 +5,10 @@
* Document rows keep their source extension when the user commits a basename
* only. Explicit extensions are preserved verbatim: `.md` ↔ `.mdx` stays on
* the managed-document path, and `.md`/`.mdx` → any other extension is routed
- * as a document-to-file rename by the caller.
+ * as a document-to-file rename by the caller. The one exception is a typed
+ * document extension onto which the retained source extension stacks a second
+ * one (`.md.md`, but equally `.md.mdx`) — see
+ * `collapseDoubledDocumentExtension`.
*
* MUST operate on the RAW event paths from Pierre (before
* `normalizeTreePathForKind`), because that normalizer silently appends `.md`
@@ -50,6 +53,37 @@ export function hasSupportedDocumentExtension(path: string): boolean {
return SUPPORTED_DOCUMENT_EXTENSIONS.has(getFileExtension(path).toLowerCase());
}
+/**
+ * Drop a retained source extension that the user's own typed extension already
+ * supplied. Pierre pre-selects only the stem, so typing a whole filename ending
+ * in `.md` commits what was typed FOLLOWED by the still-present suffix.
+ *
+ * The doubled path is not merely ugly: `name.md.md` carries docName `name.md`,
+ * and `docNameToTreePath` returns any docName already ending in `.md`/`.mdx`
+ * verbatim — so the doubled file and a real `name.md` resolve to one tree path,
+ * and persistence writes the doubled file's document to the other file's name.
+ *
+ * Scoped to document sources whose remainder is itself a document extension —
+ * that pairing is what breaks the bijection. Genuine dotted names (`v1.2.md`,
+ * `report.2026.md`) keep their suffix, and asset renames are left alone
+ * entirely for two separate reasons: `photo.png.png` maps to a tree path
+ * verbatim with no docName round-trip to break, and collapsing `notes.md.png`
+ * would silently retitle an image to the document extension the user happened
+ * to type.
+ *
+ * Exactly one retained layer comes off; this is not a general doubled-extension
+ * sanitizer. A destination the user deliberately typed with its own doubled
+ * suffix keeps what remains (`v1.md.md.md` → `v1.md.md`), and a stem-less
+ * `.md.md` is left intact because its docName is `.md`, which carries no
+ * strippable extension and so still resolves back to that same file.
+ */
+function collapseDoubledDocumentExtension(destinationPath: string, sourceExt: string): string {
+ if (!SUPPORTED_DOCUMENT_EXTENSIONS.has(sourceExt.toLowerCase())) return destinationPath;
+ if (!destinationPath.endsWith(sourceExt)) return destinationPath;
+ const withoutRetainedExt = destinationPath.slice(0, -sourceExt.length);
+ return hasSupportedDocumentExtension(withoutRetainedExt) ? withoutRetainedExt : destinationPath;
+}
+
export function validateAndCoerceRenameDestination(
sourcePath: string,
destinationPath: string,
@@ -62,6 +96,8 @@ export function validateAndCoerceRenameDestination(
const destExt = getFileExtension(destinationPath);
return {
kind: 'allow',
- destinationPath: destExt ? destinationPath : replaceFileExtension(destinationPath, sourceExt),
+ destinationPath: destExt
+ ? collapseDoubledDocumentExtension(destinationPath, sourceExt)
+ : replaceFileExtension(destinationPath, sourceExt),
};
}