diff --git a/apps/markdown/src/renderer/App.tsx b/apps/markdown/src/renderer/App.tsx index e65ec1b..3e99837 100644 --- a/apps/markdown/src/renderer/App.tsx +++ b/apps/markdown/src/renderer/App.tsx @@ -22,7 +22,7 @@ import { tiptapFindTarget } from './editor/findTarget' import { collectOutline, type OutlineItem } from './editor/outline' import { buildSlashItems } from './editor/slashCommand' import type { SlashController, SlashMenuState } from './editor/slashCommand' -import { setImageBaseDir } from './editor/localImage' +import { dirOf, setImageBaseDir } from './editor/localImage' import { Ribbon } from './components/Ribbon' import { OutlinePane } from './components/OutlinePane' import { SlashMenu, type SlashMenuHandle } from './components/SlashMenu' @@ -55,11 +55,6 @@ const EMPTY_ENVELOPE: DocEnvelope = { bom: false, } -function dirOf(path: string): string { - const i = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) - return i > 0 ? path.slice(0, i) : path -} - function bytesToBase64(bytes: Uint8Array): string { let binary = '' const CHUNK = 0x8000 diff --git a/apps/markdown/src/renderer/editor/localImage.ts b/apps/markdown/src/renderer/editor/localImage.ts index 4062aba..a63606b 100644 --- a/apps/markdown/src/renderer/editor/localImage.ts +++ b/apps/markdown/src/renderer/editor/localImage.ts @@ -10,6 +10,18 @@ export function setImageBaseDir(dir: string | null): void { imageBaseDir = dir } +/** + * Directory of a file path for resolving relative image sources. A file at + * the filesystem root ("/note.md") resolves to "/" so its sibling images + * keep working; paths without a separator have no directory to take. + */ +export function dirOf(path: string): string { + const i = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) + if (i > 0) return path.slice(0, i) + if (i === 0) return path.slice(0, 1) + return path +} + /** * Map an authored image src to a displayable URL. Markdown keeps the authored * value (usually a path relative to the .md file); the editor DOM loads it via diff --git a/apps/markdown/tests/slash-and-image.test.ts b/apps/markdown/tests/slash-and-image.test.ts index ecc81b2..2f34b18 100644 --- a/apps/markdown/tests/slash-and-image.test.ts +++ b/apps/markdown/tests/slash-and-image.test.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { buildSlashItems, filterSlashItems } from '../src/renderer/editor/slashCommand' -import { resolveImageSrc, unresolveImageSrc } from '../src/renderer/editor/localImage' +import { dirOf, resolveImageSrc, unresolveImageSrc } from '../src/renderer/editor/localImage' import { moveSelectedBlocks, uiOp } from '../src/renderer/editor/ops' // Undestroyed views leave DOMObserver flush timers that fire after jsdom teardown @@ -218,6 +218,18 @@ describe('resolveImageSrc on Windows paths', () => { }) }) +describe('dirOf', () => { + it('returns the filesystem root for root-level files', () => { + expect(dirOf('/note.md')).toBe('/') + expect(resolveImageSrc('a.png', dirOf('/note.md'))).toBe('md-asset:///a.png') + }) + + it('returns containing dirs for nested and Windows paths', () => { + expect(dirOf('/a/b.md')).toBe('/a') + expect(dirOf('C:\\a\\b.md')).toBe('C:\\a') + }) +}) + describe('buildPrintHtml', () => { it('strips editor-only code block chrome from the print output', async () => { const { buildPrintHtml } = await import('../src/renderer/export/printHtml')