diff --git a/mdedit/js/file-system.js b/mdedit/js/file-system.js index 810cb2c..f805e0f 100644 --- a/mdedit/js/file-system.js +++ b/mdedit/js/file-system.js @@ -683,9 +683,38 @@ async function readServerDirectory(dirUrl, parentNode, depth) { async function loadServerDirectory() { if (!(location.protocol === 'http:' || location.protocol === 'https:')) return; + // Compute the directory URL the file tree should be rooted at. + // + // /working/ → root = /working/ + // /working/x/y/ → root = /working/x/y/ + // /working → root = /working/ (no-slash + // canonical-folder URL — the dispatcher + // routes mdedit here directly without + // a redirect, so we infer "directory" + // from the absence of a `.` in the + // last segment rather than stripping + // back to the parent.) + // /x/y/mdedit.html → root = /x/y/ (the leaf + // segment IS a file; strip to parent.) + // + // The rule: if the last path segment contains a "." it's a file, + // strip it; otherwise treat the whole path as the directory. let href = window.location.href.split('?')[0].split('#')[0]; - const lastSlash = href.lastIndexOf('/'); - const baseUrl = (lastSlash >= 0) ? href.substring(0, lastSlash + 1) : href + '/'; + let baseUrl; + if (href.endsWith('/')) { + baseUrl = href; + } else { + const lastSlash = href.lastIndexOf('/'); + const lastSeg = lastSlash >= 0 ? href.substring(lastSlash + 1) : href; + if (lastSeg.indexOf('.') !== -1) { + // Looks like a file (has an extension) — strip to parent. + baseUrl = lastSlash >= 0 ? href.substring(0, lastSlash + 1) : href + '/'; + } else { + // Looks like a directory — append the trailing slash so all + // subsequent listing URLs are computed correctly. + baseUrl = href + '/'; + } + } // Only enter server-source mode if the host actually serves JSON directory // listings (zddc-server / Caddy). On a plain static host the probe fails