Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(editor): use the small image size for Markdown pictures

tianma-if1ec4bfc06f3b
diff --git a/apps/mobile/src/components/LocalTiptapEditor.tsx b/apps/mobile/src/components/LocalTiptapEditor.tsx
index a07c934d..b7e71936 100644
--- a/apps/mobile/src/components/LocalTiptapEditor.tsx
+++ b/apps/mobile/src/components/LocalTiptapEditor.tsx
@@ -53,7 +53,6 @@ import {
} from "@edgeever/shared";
import { createEdgeEverMathematics } from "@edgeever/shared/mathematics";
import {
- DEFAULT_IMAGE_WIDTH_PERCENT,
NEW_IMAGE_WIDTH_PERCENT,
IMAGE_WIDTH_PRESETS,
PHONE_IMAGE_FILL_CSS,
@@ -2182,7 +2181,7 @@ const applyImageWidth = (
element: HTMLElement,
attributes: Record<string, unknown>
): number => {
- const width = parseImageWidth(attributes.width) ?? DEFAULT_IMAGE_WIDTH_PERCENT;
+ const width = parseImageWidth(attributes.width) ?? NEW_IMAGE_WIDTH_PERCENT;
element.style.width = `${width}%`;
element.dataset.width = String(width);
return width;
diff --git a/apps/web/src/components/editor/ResizableImage.tsx b/apps/web/src/components/editor/ResizableImage.tsx
index aaf36120..fc114a74 100644
--- a/apps/web/src/components/editor/ResizableImage.tsx
+++ b/apps/web/src/components/editor/ResizableImage.tsx
@@ -6,8 +6,8 @@ import { Maximize2 } from "lucide-react";
import { useTranslation } from "react-i18next";
import { getImageReferrerPolicy, IMAGE_GALLERY_NODE_TYPE } from "@edgeever/shared";
import {
- DEFAULT_IMAGE_WIDTH_PERCENT,
IMAGE_WIDTH_PRESETS,
+ NEW_IMAGE_WIDTH_PERCENT,
clampImageWidth,
parseImageWidth,
} from "@edgeever/shared/image-display";
@@ -47,7 +47,7 @@ const ResizableImageNodeView = ({
const wrapperRef = useRef<HTMLDivElement | null>(null);
const imageRef = useRef<HTMLImageElement | null>(null);
const [previewWidth, setPreviewWidth] = useState<number | null>(null);
- const nodeWidth = parseImageWidth(node.attrs.width) ?? DEFAULT_IMAGE_WIDTH_PERCENT;
+ const nodeWidth = parseImageWidth(node.attrs.width) ?? NEW_IMAGE_WIDTH_PERCENT;
const width = previewWidth ?? nodeWidth;
const { editable, inGallery } = useEditorState({
editor,
diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.ts
index a83d28c4..149ecf06 100644
--- a/packages/shared/src/content.ts
+++ b/packages/shared/src/content.ts
@@ -13,6 +13,7 @@ import {
} from "./mathematics-markdown";
import { projectNativeUnknownContentForMarkdown } from "./mobile-content-compatibility";
import { PluginEmbed, PLUGIN_EMBED_NODE_TYPE } from "./plugin-embed";
+import { NEW_IMAGE_WIDTH_PERCENT, parseImageWidth } from "./image-display";
import { ImageGallery, IMAGE_GALLERY_NODE_TYPE, normalizeImageGalleries } from "./image-gallery";
import { EMPTY_EXTERNAL_LINK_NODE_TYPE } from "./empty-external-link";
@@ -126,13 +127,23 @@ const expandExtraBlankLinesForParse = (markdown: string) =>
return segment.replace(/\n{3,}/g, (run) => "\n\n".repeat(run.length - 1));
}).join("");
+const withDefaultImageWidths = (node: TiptapNode): TiptapNode => {
+ const content = node.content?.map((child) => (
+ child.type === "text" ? child : withDefaultImageWidths(child)
+ ));
+ const next = content ? { ...node, content } : node;
+ if (next.type !== "image" || parseImageWidth(next.attrs?.width) != null) return next;
+ return { ...next, attrs: { ...next.attrs, width: NEW_IMAGE_WIDTH_PERCENT } };
+};
+
export const markdownToDoc = (markdown: string): TiptapDoc => {
const normalized = markdown.replace(/\r\n?/g, "\n");
if (!normalized) {
return emptyDoc();
}
- return markdownManager.parse(expandExtraBlankLinesForParse(normalized)) as TiptapDoc;
+ const parsed = markdownManager.parse(expandExtraBlankLinesForParse(normalized)) as TiptapDoc;
+ return { ...parsed, content: parsed.content.map(withDefaultImageWidths) };
};
const docContainsNodeType = (doc: TiptapDoc, nodeType: string): boolean => {
diff --git a/tests/content-markdown.test.ts b/tests/content-markdown.test.ts
index 3f9e0a9e..6b00f3a3 100644
--- a/tests/content-markdown.test.ts
+++ b/tests/content-markdown.test.ts
@@ -83,6 +83,7 @@ const answer = 42;
const doc = markdownToDoc(markdown);
expect(doc.content.map((node) => node.type)).toEqual(["codeBlock", "image"]);
+ expect(doc.content[1]?.attrs?.width).toBe(35);
expect(docToMarkdown(doc)).toBe(markdown);
});