Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(desktop): 支持 Obsidian wiki-link 图片嵌入语法预览 (#3014)

Chris Zhang7135aa3150bc
diff --git a/apps/desktop/src/renderer/components/markdown/__tests__/markdownImageLivePreview.test.ts b/apps/desktop/src/renderer/components/markdown/__tests__/markdownImageLivePreview.test.ts
index 7da2070463..b28de102cb 100644
--- a/apps/desktop/src/renderer/components/markdown/__tests__/markdownImageLivePreview.test.ts
+++ b/apps/desktop/src/renderer/components/markdown/__tests__/markdownImageLivePreview.test.ts
@@ -1,8 +1,9 @@
/**
* Unit tests for the pure helpers of `markdownImageLivePreview`:
* - `findImageTargets(doc)` — scans for image-only blocks (markdown
- * `![](...)` lines, HTML `<img>` lines, `<p>`-wrapped blocks), skipping
- * fenced code so documentation samples never render as live images.
+ * `![](...)` lines, HTML `<img>` lines, `<p>`-wrapped blocks, Obsidian
+ * `![[...]]` wiki-link embeds), skipping fenced code so documentation
+ * samples never render as live images.
* - `resolveImageSrcToUrl(src, baseDir)` — maps a raw markdown src to the
* URL the <img> actually loads (xdt-file:// for local paths).
*
@@ -93,6 +94,95 @@ describe('findImageTargets — markdown form', () => {
});
});
+describe('findImageTargets — Obsidian wiki-link form', () => {
+ it('extracts a bare wiki-link embed', () => {
+ const doc = docOf('before', '![[image.png]]', 'after');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0]).toMatchObject({
+ from: doc.line(2).from,
+ to: doc.line(2).to,
+ align: null,
+ images: [{ src: 'image.png', alt: '', title: '', width: null, height: null }],
+ });
+ });
+
+ it('resolves a subfolder-relative wiki-link path', () => {
+ const doc = docOf('![[subfolder/image.png]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0].src).toBe('subfolder/image.png');
+ });
+
+ it('parses a width-only size suffix', () => {
+ const doc = docOf('![[image.png|300]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0]).toMatchObject({
+ src: 'image.png',
+ width: 300,
+ height: null,
+ });
+ });
+
+ it('parses a width x height size suffix', () => {
+ const doc = docOf('![[image.png|300x200]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0]).toMatchObject({
+ src: 'image.png',
+ width: 300,
+ height: 200,
+ });
+ });
+
+ it('allows spaces in the path without angle brackets', () => {
+ const doc = docOf('![[Some Image.png]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0].src).toBe('Some Image.png');
+ });
+
+ it('does not render a plain [[note]] link (no leading !) as an image', () => {
+ const doc = docOf('see [[note]] for details');
+ expect(findImageTargets(doc)).toEqual([]);
+ });
+
+ it('does not render a wiki-link with a non-numeric size suffix', () => {
+ const doc = docOf('![[image.png|alt text]]');
+ expect(findImageTargets(doc)).toEqual([]);
+ });
+
+ it('does not render a wiki-link note embed (no image extension)', () => {
+ const doc = docOf('![[Project overview]]');
+ expect(findImageTargets(doc)).toEqual([]);
+ });
+
+ it('does not render a wiki-link PDF/non-image file embed', () => {
+ const doc = docOf('![[manual.pdf]]');
+ expect(findImageTargets(doc)).toEqual([]);
+ });
+
+ it('accepts a recognized image extension case-insensitively', () => {
+ const doc = docOf('![[photo.PNG]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0].src).toBe('photo.PNG');
+ });
+
+ it('skips wiki-link image examples inside fenced code blocks', () => {
+ const doc = docOf('```markdown', '![[sample.png]]', '```', '![[real.png]]');
+ const targets = findImageTargets(doc);
+ expect(targets).toHaveLength(1);
+ expect(targets[0].images[0].src).toBe('real.png');
+ });
+
+ it('leaves an inline wiki-link mid-paragraph as raw source', () => {
+ const doc = docOf('see ![[icon.png]] inline');
+ expect(findImageTargets(doc)).toEqual([]);
+ });
+});
+
describe('findImageTargets — HTML forms', () => {
it('extracts a bare <img> line with alt / width / height attrs', () => {
const doc = docOf(
@@ -246,6 +336,14 @@ describe('resolveImageSrcToUrl', () => {
);
});
+ it('joins a wiki-link-style relative path with a literal space and subfolder', () => {
+ // Wiki-link parsing hands resolveImageSrcToUrl the raw path as typed
+ // (spaces intact, no percent-encoding) — unlike markdown srcs.
+ expect(resolveImageSrcToUrl('subfolder/Some Image.png', '/repo/notes')).toBe(
+ `xdt-file://local/?path=${encodeURIComponent('/repo/notes/subfolder/Some Image.png')}`,
+ );
+ });
+
it('returns null for empty src or relative src without a base dir', () => {
expect(resolveImageSrcToUrl('', '/base')).toBeNull();
expect(resolveImageSrcToUrl('img/a.png', '')).toBeNull();
diff --git a/apps/desktop/src/renderer/components/markdown/markdownImageLivePreview.ts b/apps/desktop/src/renderer/components/markdown/markdownImageLivePreview.ts
index 4b0322e277..1f690d97c3 100644
--- a/apps/desktop/src/renderer/components/markdown/markdownImageLivePreview.ts
+++ b/apps/desktop/src/renderer/components/markdown/markdownImageLivePreview.ts
@@ -25,6 +25,20 @@
* `</p>`
* `align="center"` on the <p> centers the rendered images; `width` /
* `height` attributes are applied (still capped at container width).
+ * 5. Obsidian wiki-link image embed, alone on its line:
+ * `![[image.png]]`, `![[subfolder/image.png]]`, `![[image.png|300]]`,
+ * `![[image.png|300x200]]`
+ * The path may contain spaces and doesn't need angle brackets; `|300`
+ * sets width (px), `|300x200` sets width x height. Plain `[[note]]`
+ * (no leading `!`) is an Obsidian *note link*, not an image embed, and
+ * is intentionally left as raw source — only `![[...]]` renders. A
+ * non-numeric `|` suffix (Obsidian's alt-text alias form) is also left
+ * as raw source; out of scope for this module. `![[...]]` is also
+ * Obsidian's generic embed syntax for notes/PDFs/etc — the target is
+ * only treated as an image (and thus rendered) when its extension is
+ * one this app already recognizes as an image (`isLightboxImagePath`,
+ * shared with the workdir file tree); anything else stays raw source
+ * instead of being replaced by a broken-image card.
*
* Inline images mid-paragraph, images inside blockquotes / list items, and
* mixed image+text lines stay as raw source — replacing those needs inline
@@ -62,6 +76,7 @@ import {
} from '@codemirror/view';
import i18n from 'i18next';
+import { isLightboxImagePath } from '@/features/cc-agent/workdir-browse/lib/imageExt';
import { resolveLocalPath, toLocalFileUrl } from '@/lib/localPathResolver';
// ─── Facets ─────────────────────────────────────────────────────────────────
@@ -91,6 +106,17 @@ export const imageLocaleFacet = Facet.define<string, string>({
const MD_IMAGE_LINE_RE =
/^ {0,3}!\[([^\]]*)\]\(\s*(<[^<>]*>|[^)\s]+)(?:\s+("[^"]*"|'[^']*'))?\s*\)\s*$/;
+// Obsidian wiki-link *embed* syntax (`![[target]]`) covers notes, PDFs and
+// images alike — this regex only isolates the target/size text; the caller
+// (matchSingleLineImage) still has to check the extension via
+// isLightboxImagePath before treating it as an image. Path excludes `]` and
+// `|` (so it stops at the size suffix or the closing `]]`); size suffix is
+// `|width` or `|widthxheight`, digits only. A non-numeric `|` suffix
+// (Obsidian's alt-text alias) fails to match here and the line stays raw
+// source.
+const WIKI_IMAGE_LINE_RE =
+ /^ {0,3}!\[\[([^\]|]+)(?:\|(\d+)(?:x(\d+))?)?\]\]\s*$/;
+
// HTML forms. `[^>]*?` keeps attribute scanning inside the tag; an attr
// value containing a literal `>` is out of scope (stays raw source).
const HTML_BARE_IMG_LINE_RE = /^ {0,3}<img\b([^>]*?)\/?\s*>\s*$/i;
@@ -258,6 +284,24 @@ function matchSingleLineImage(
align: null,
};
}
+ const wiki = WIKI_IMAGE_LINE_RE.exec(text);
+ if (wiki) {
+ const rawSrc = wiki[1].trim();
+ // `![[...]]` is Obsidian's generic embed syntax (notes, PDFs, images);
+ // only render it here when the target is actually an image extension —
+ // otherwise a note/PDF embed would get replaced by a broken-image card.
+ if (!rawSrc || !isLightboxImagePath(rawSrc)) return null;
+ return {
+ spec: {
+ src: rawSrc,
+ alt: '',
+ title: '',
+ width: wiki[2] ? Number(wiki[2]) : null,
+ height: wiki[3] ? Number(wiki[3]) : null,
+ },
+ align: null,
+ };
+ }
const wrapped = HTML_P_WRAPPED_LINE_RE.exec(text);
if (wrapped) {
const spec = parseImgAttrs(wrapped[2]);