fix(markdown): resolve sibling images for filesystem-root files (#366)

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-14 14:26:03 +08:00
committed by GitHub
co-authored by Aniruddha Adak
parent cbcd289e49
commit 616a7acf5a
3 changed files with 26 additions and 7 deletions
+1 -6
View File
@@ -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
@@ -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
+13 -1
View File
@@ -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')