Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Keep a component's body bytes when only its props change (#7318)
Dimitri POSTOLOV591bdc9b2753
diff --git a/.changeset/component-prop-edit-keeps-body.md b/.changeset/component-prop-edit-keeps-body.mdnew file mode 100644index 000000000..e3d815595--- /dev/null+++ b/.changeset/component-prop-edit-keeps-body.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Changing the properties of a component that is not inside another component now rewrites only its opening tag, unless you also edited its content. An unindented body no longer gains an indent, and changing a property back restores the original text.diff --git a/knip.config.ts b/knip.config.tsindex 36a395261..03ef8d4a4 100644--- a/knip.config.ts+++ b/knip.config.ts@@ -155,11 +155,15 @@ export default {'src/shadow-fixture-launch.test-helper.mjs',],project: 'src/**',- ignoreDependencies: ['@types/shell-quote'],+ ignoreDependencies: [+ '@types/shell-quote',+ ],},'packages/cli': {entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],- ignoreDependencies: ['yjs'],+ ignoreDependencies: [+ 'yjs',+ ],},'packages/desktop': {entry: [diff --git a/packages/app/src/editor/extensions/source-dirty-serialize.test.ts b/packages/app/src/editor/extensions/source-dirty-serialize.test.tsindex 96dc00a5e..569064615 100644--- a/packages/app/src/editor/extensions/source-dirty-serialize.test.ts+++ b/packages/app/src/editor/extensions/source-dirty-serialize.test.ts@@ -26,6 +26,12 @@ function findAllNodes(node: JSONContent, type: string): JSONContent[] {return results;}+function markForRebuild(node: JSONContent | undefined): void {+ if (!node?.attrs) return;+ node.attrs.sourceDirty = true;+ node.attrs.sourceRaw = '';+}+function normalize(s: string): string {return s.split('\n')@@ -51,7 +57,7 @@ describe('DT — Dirty-tracking serialization behavior', () => {const json = mdManager.parse(input);const component = findNode(json, 'jsxComponent');- if (component?.attrs) component.attrs.sourceDirty = true;+ markForRebuild(component);const output = mdManager.serialize(json);expect(output).toContain('Callout');@@ -101,7 +107,7 @@ describe('DT — Dirty-tracking serialization behavior', () => {const json = mdManager.parse(input);const stepsComponents = findAllNodes(json, 'jsxComponent');- if (stepsComponents[0]?.attrs) stepsComponents[0].attrs.sourceDirty = true;+ markForRebuild(stepsComponents[0]);const output = mdManager.serialize(json);expect(output).toContain('Steps');@@ -114,9 +120,7 @@ describe('DT — Dirty-tracking serialization behavior', () => {const json = mdManager.parse(input);const stepsComponents = findAllNodes(json, 'jsxComponent');- for (const c of stepsComponents) {- if (c.attrs) c.attrs.sourceDirty = true;- }+ for (const c of stepsComponents) markForRebuild(c);const output = mdManager.serialize(json);expect(output).toContain('Steps');@@ -182,7 +186,7 @@ describe('DT — Unknown attr preservation via reconstructAttrs merge (M10/FR-21const json = mdManager.parse(input);const component = findNode(json, 'jsxComponent');- if (component?.attrs) component.attrs.sourceDirty = true;+ markForRebuild(component);const output = mdManager.serialize(json);expect(output).toContain('color="#F05032"');diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.tsindex a50b00123..4549c74b3 100644--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts@@ -1,3 +1,4 @@+import { readFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.tsindex 6fcdc23f2..4fc2c11f9 100644--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { normalizeBridge } from '@inkeep/open-knowledge-core';import { updateYFragment } from '@tiptap/y-tiptap';diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.tsindex 40b5cdfd6..9f2f6feb6 100644--- a/packages/app/tests/integration/interior-edit-freshness.test.ts+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.tsindex 93666af70..2a085742b 100644--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.tsindex 867598d34..878d5196b 100644--- a/packages/app/tests/integration/producer-guard-wiring.test.ts+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.tsindex 0970ac12f..4ada3cf26 100644--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts@@ -1,3 +1,4 @@+import { randomUUID } from 'node:crypto';import { afterEach, describe, expect, test } from 'vitest';import { agentWriteMd, createTestServer, type TestServer } from './test-harness';diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.tsindex b632185e1..cbe2b705f 100644--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts@@ -1,3 +1,4 @@+import { execFileSync } from 'node:child_process';import { randomUUID } from 'node:crypto';import { join } from 'node:path';diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.tsindex c530ce4ba..89bc9b4d9 100644--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts@@ -1,3 +1,4 @@+import { randomUUID } from 'node:crypto';import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';import type * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.tsindex d3767adc9..a78aeb0e4 100644--- a/packages/app/tests/integration/undo-after-rollback.test.ts+++ b/packages/app/tests/integration/undo-after-rollback.test.ts@@ -1,3 +1,4 @@+import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';import { afterAll, beforeAll, describe, expect, test } from 'vitest';import * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.tsindex b076f955c..e7cc69c3f 100644--- a/packages/app/tests/integration/undo-recycle-reset.test.ts+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts@@ -1,3 +1,4 @@+import { writeFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.tsindex 7935e35f8..9ddcd0730 100644--- a/packages/app/tests/integration/undo-rederive-survival.test.ts+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts@@ -1,3 +1,4 @@+import { prependFrontmatter } from '@inkeep/open-knowledge-core';import { Plugin, PluginKey } from '@tiptap/pm/state';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';diff --git a/packages/app/tests/stress/jsx-prop-edit-body-bytes.e2e.ts b/packages/app/tests/stress/jsx-prop-edit-body-bytes.e2e.tsnew file mode 100644index 000000000..1f7e0ae0d--- /dev/null+++ b/packages/app/tests/stress/jsx-prop-edit-body-bytes.e2e.ts@@ -0,0 +1,100 @@+import { randomUUID } from 'node:crypto';+import type { Page } from '@playwright/test';+import { expect, test, waitForActiveProviderSynced } from './_helpers';++const CALLOUT = '.jsx-component-wrapper[data-component-type="callout"]';++const CASES = [+ {+ name: 'a top-level Callout',+ seed: '# Notes\n\n<Callout type="note">\nFirst line.\n\nSecond line.\n</Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout with a doc-relative image',+ seed: '# Notes\n\n<Callout type="note">\nSee this:\n\n\n</Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout with an inline doc-relative image',+ seed: '<Callout type="note">\nSee  here.\n</Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout in a list item',+ seed: '- Item\n\n <Callout type="note">\n First line.\n </Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout in a blockquote',+ seed: '> <Callout type="note">\n> First line.\n> </Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout with a quoted ">" in an attribute',+ seed: '<Callout type="note" title="a > b">\nFirst line.\n</Callout>\n\nAfter.\n',+ },+ {+ name: 'a Callout that holds another Callout',+ seed: '<Callout type="note">\nOuter.\n\n<Callout type="tip">\nInner.\n</Callout>\n</Callout>\n\nAfter.\n',+ },+];++function readSource(page: Page): Promise<string> {+ return page.evaluate(() => window.__activeProvider?.document.getText('source').toString() ?? '');+}++async function setCalloutType(page: Page, value: string): Promise<void> {+ const wrapper = page.locator(CALLOUT).first();+ await wrapper.locator('p').first().hover();+ await wrapper.getByRole('button', { name: 'Callout properties' }).first().click();+ const popover = page.locator('[data-slot="popover-content"]');+ await expect(popover.locator('[data-prop-panel]')).toBeVisible();+ await popover.locator('#prop-type').click();+ await page.getByRole('option', { name: value, exact: true }).click();+ await popover.getByRole('button', { name: 'Done' }).click();+ await expect(popover).toHaveCount(0);+}++for (const { name, seed } of CASES) {+ test(`a Properties edit on ${name} changes only the opening tag`, async ({ page, api }) => {+ const docName = `jsx-prop-edit-bytes-${randomUUID().slice(0, 8)}`;+ await api.createPage(`${docName}.md`);+ await api.testReset(docName);+ await api.replaceDoc(docName, seed);+ await page.goto(`/#/${docName}`);+ await waitForActiveProviderSynced(page);+ await expect(page.locator(CALLOUT).first()).toBeVisible();+ expect(await readSource(page), 'opening the document changed its bytes').toBe(seed);++ await setCalloutType(page, 'warning');+ await expect.poll(() => readSource(page)).toContain('type="warning"');+ expect(+ await readSource(page),+ 'the prop edit rewrote the Callout body, not only its opening tag',+ ).toBe(seed.replace('type="note"', 'type="warning"'));++ await setCalloutType(page, 'note');+ await expect.poll(() => readSource(page)).not.toContain('type="warning"');+ expect(+ await readSource(page),+ 'changing the prop back did not restore the original bytes',+ ).toBe(seed);+ });+}++test('a body edit before a Properties edit survives the prop edit', async ({ page, api }) => {+ const seed = '<Callout type="note">\nFirst line.\n</Callout>\n\nAfter.\n';+ const docName = `jsx-prop-edit-bytes-${randomUUID().slice(0, 8)}`;+ await api.createPage(`${docName}.md`);+ await api.testReset(docName);+ await api.replaceDoc(docName, seed);+ await page.goto(`/#/${docName}`);+ await waitForActiveProviderSynced(page);++ await page.locator(CALLOUT).locator('p', { hasText: 'First line.' }).click();+ await page.keyboard.press('End');+ await page.keyboard.type(' Added.');+ await expect.poll(() => readSource(page)).toContain('First line. Added.');++ await setCalloutType(page, 'warning');+ await expect.poll(() => readSource(page)).toContain('type="warning"');+ expect(await readSource(page), 'the prop edit dropped the body edit').toContain(+ 'First line. Added.',+ );+});diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.tsindex f4cc8a286..2a597ec3d 100644--- a/packages/core/src/bridge/compose.ts+++ b/packages/core/src/bridge/compose.ts@@ -1,3 +1,4 @@+import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.tsindex e7e63f43d..ffd8fce06 100644--- a/packages/core/src/bridge/doc-boundary-space.ts+++ b/packages/core/src/bridge/doc-boundary-space.ts@@ -1,3 +1,4 @@+import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';diff --git a/packages/core/src/bridge/pm-structural-equivalence.ts b/packages/core/src/bridge/pm-structural-equivalence.tsindex 8b5e32ed2..a4bd660ff 100644--- a/packages/core/src/bridge/pm-structural-equivalence.ts+++ b/packages/core/src/bridge/pm-structural-equivalence.ts@@ -172,7 +172,7 @@ function deepDropKeys(value: unknown, ignoreAttrs: (key: string) => boolean): unreturn value;}-function stableStringify(value: unknown): string {+export function stableStringify(value: unknown): string {return JSON.stringify(value, (_k, v) => {if (v && typeof v === 'object' && !Array.isArray(v)) {const sorted: Record<string, unknown> = {};diff --git a/packages/core/src/bridge/structural-freshness.ts b/packages/core/src/bridge/structural-freshness.tsindex 2f57dcd1f..2af67f3f4 100644--- a/packages/core/src/bridge/structural-freshness.ts+++ b/packages/core/src/bridge/structural-freshness.ts@@ -1,3 +1,4 @@+import { authoredImageUrl } from '../markdown/resolve-image-url.ts';import { type PmStructuralNode, structuralDivergence } from './pm-structural-equivalence.ts';const RESOLVER_DERIVED_ATTRS = new Set(['src', 'href', 'size']);@@ -23,19 +24,31 @@ function deepDropResolverKeys<T>(value: T): T {return value;}+function withAuthoredImageUrl(attrs: Record<string, unknown>): Record<string, unknown> {+ const { sourceUrl, ...rest } = attrs;+ return { ...rest, src: authoredImageUrl(attrs.src, sourceUrl) };+}+function stripResolverDerivedAttrs(node: PmStructuralNode): PmStructuralNode {- if (- node.type === 'jsxComponent' &&- typeof node.attrs?.componentName === 'string' &&- WIKI_EMBED_COMPONENTS.has(node.attrs.componentName)- ) {+ const componentName = node.type === 'jsxComponent' ? node.attrs?.componentName : undefined;+ if (typeof componentName === 'string' && WIKI_EMBED_COMPONENTS.has(componentName)) {return deepDropResolverKeys(node);}+ if (node.type === 'image' && node.attrs) {+ return { ...node, attrs: withAuthoredImageUrl(node.attrs) };+ }+ const props = node.attrs?.props;+ if (componentName === 'CommonMarkImage' && props && typeof props === 'object') {+ return {+ ...node,+ attrs: { ...node.attrs, props: withAuthoredImageUrl(props as Record<string, unknown>) },+ };+ }if (!node.content) return node;return { ...node, content: node.content.map(stripResolverDerivedAttrs) };}-function stripMdastPositions<T>(value: T): T {+export function stripMdastPositions<T>(value: T): T {if (Array.isArray(value)) {return value.map((entry) => stripMdastPositions(entry)) as unknown as T;}diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.tsindex fc267cbea..fb0ba5a7c 100644--- a/packages/core/src/markdown/comment-promoter.ts+++ b/packages/core/src/markdown/comment-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/index.ts b/packages/core/src/markdown/index.tsindex 9f6477013..57a780eda 100644--- a/packages/core/src/markdown/index.ts+++ b/packages/core/src/markdown/index.ts@@ -56,7 +56,12 @@ import {import type { LinkStyle } from '../extensions/link-fidelity.ts';import { isValidSourceLiteralRaw } from '../extensions/source-literal-mark.ts';import { createRegistry } from '../registry/index.ts';-import type { PropDef } from '../registry/types.ts';+import type { PropDef, SerializeContext } from '../registry/types.ts';+import {+ type AuthoredJsxSourceReader,+ createAuthoredJsxSourceReader,+ plainJsxFlowElement,+} from './jsx-authored-body.ts';import type {CommentBlockMdast,CommentMdast,@@ -73,7 +78,7 @@ import {parseMdToMdast,serializeMd,} from './pipeline.ts';-import { normalizeDocRelativeAssetUrl } from './resolve-image-url.ts';+import { authoredImageUrl, normalizeDocRelativeAssetUrl } from './resolve-image-url.ts';import { emitMdxJsxTextFromNode } from './serialize-helpers.ts';import { flattenCellBlocks } from './table-cell-flatten.ts';import { toMarkdownHandlers } from './to-markdown-handlers.ts';@@ -119,6 +124,9 @@ export class MarkdownManager {private serializeProcessor: Processor;private parseCtx: ParseContextHolder = { current: {} };private freshnessHolder: FreshnessCheckerHolder = { checker: undefined };+ private authoredJsxSource: AuthoredJsxSourceReader = createAuthoredJsxSourceReader((sourceRaw) =>+ this.schema.nodeFromJSON(this.parseWithFallback(sourceRaw)),+ );constructor(options: MarkdownManagerOptions) {this.schema = getSchema(options.extensions);@@ -131,6 +139,7 @@ export class MarkdownManager {const { nodeHandlers, markHandlers } = buildPmToMdastHandlers(this.schema,this.freshnessHolder,+ this.authoredJsxSource,);this.pmNodeHandlers = nodeHandlers;this.pmMarkHandlers = markHandlers;@@ -1143,6 +1152,7 @@ function buildMdastToPmHandlers(function buildPmToMdastHandlers(schema: Schema,freshness: FreshnessCheckerHolder,+ authoredJsxSource: AuthoredJsxSourceReader,): {nodeHandlers: FromProseMirrorOptions<string, string>['nodeHandlers'];markHandlers: FromProseMirrorOptions<string, string>['markHandlers'];@@ -1330,7 +1340,7 @@ function buildPmToMdastHandlers(nodeHandlers.image = (pmNode: PmNode) => {const result: Image = {type: 'image' as const,- url: (pmNode.attrs.sourceUrl as string | null) ?? pmNode.attrs.src,+ url: authoredImageUrl(pmNode.attrs.src, pmNode.attrs.sourceUrl),alt: pmNode.attrs.alt,title: pmNode.attrs.title,};@@ -1404,18 +1414,18 @@ function buildPmToMdastHandlers(? (pmNode.attrs.attributes as Array<MdxJsxAttribute | MdxJsxExpressionAttribute>): [];- if (!effectiveDirty(pmNode, freshness.checker) && pmNode.attrs.sourceRaw) {- return {- type: 'mdxJsxFlowElement' as const,- name: componentName,- attributes: preservedAttrs,- children: [],- data: { sourceRaw: String(pmNode.attrs.sourceRaw) },- } as MdxJsxFlowElement;- }+ const verbatim = (): MdxJsxFlowElement => ({+ type: 'mdxJsxFlowElement',+ name: componentName,+ attributes: preservedAttrs,+ children: [],+ data: { sourceRaw: String(pmNode.attrs.sourceRaw) },+ });++ if (!effectiveDirty(pmNode, freshness.checker) && pmNode.attrs.sourceRaw) return verbatim();const meta = registry.getOrWildcard(componentName ?? '*');- return meta.serialize(pmNode, {+ const serializeContext: SerializeContext = {all: (node) => state.all(node) as MdastNodes[],registry,serializeChildren: () => {@@ -1425,7 +1435,16 @@ function buildPmToMdastHandlers('mdast node and let the to-markdown handler render the body via state.containerFlow.',);},- });+ };+ const emitted = meta.serialize(pmNode, serializeContext);+ const element = plainJsxFlowElement(emitted, componentName);+ if (!element || hasDirtyDescendant(pmNode)) return emitted;+ const authored = authoredJsxSource.read(pmNode, element, (node) =>+ meta.serialize(node, serializeContext),+ );+ if (!authored) return element;+ if (authored.kind === 'verbatim') return verbatim();+ return { ...element, data: { ...element.data, sourceBody: authored.body } };};}diff --git a/packages/core/src/markdown/jsx-authored-body.ts b/packages/core/src/markdown/jsx-authored-body.tsnew file mode 100644index 000000000..80aebe99a--- /dev/null+++ b/packages/core/src/markdown/jsx-authored-body.ts@@ -0,0 +1,121 @@+import type { Node as PmNode } from '@tiptap/pm/model';+import type { Nodes as MdastNodes } from 'mdast';+import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';+import { stableStringify } from '../bridge/pm-structural-equivalence.ts';+import { stripMdastPositions } from '../bridge/structural-freshness.ts';+import type { AuthoredJsxBody } from './mdast-augmentation.ts';++type AuthoredJsxSource = { kind: 'verbatim' } | { kind: 'body'; body: AuthoredJsxBody };++export interface AuthoredJsxSourceReader {+ read(+ node: PmNode,+ emitted: MdxJsxFlowElement,+ serialize: (authored: PmNode) => MdastNodes,+ ): AuthoredJsxSource | null;+}++interface AuthoredElement {+ attributes: string;+ children: string;+}++const TAG_NAME_END = /[\s/>{]/;++const AUTHORED_ELEMENT_CACHE_LIMIT = 256;++function canonicalJson(value: unknown): string {+ return stableStringify(stripMdastPositions(value));+}++export function plainJsxFlowElement(+ node: MdastNodes,+ name: string | null,+): MdxJsxFlowElement | null {+ if (node.type !== 'mdxJsxFlowElement') return null;+ return node.name === name && !node.data?.htmlBoundary ? node : null;+}++function splitAuthoredOpenTag(sourceRaw: string, name: string): AuthoredJsxBody | null {+ const start = sourceRaw.indexOf('<');+ if (start < 0 || sourceRaw.slice(0, start).trim() !== '') return null;+ const nameEnd = start + 1 + name.length;+ if (!sourceRaw.startsWith(`<${name}`, start) || !TAG_NAME_END.test(sourceRaw[nameEnd] ?? '')) {+ return null;+ }+ let quote: string | null = null;+ let braceDepth = 0;+ for (let i = nameEnd; i < sourceRaw.length; i++) {+ const ch = sourceRaw[i];+ if (quote !== null) {+ if (ch === quote) quote = null;+ } else if (braceDepth > 0) {+ if (ch === '{') braceDepth++;+ else if (ch === '}') braceDepth--;+ } else if (ch === '{') {+ braceDepth = 1;+ } else if (ch === '"' || ch === "'") {+ quote = ch;+ } else if (ch === '>') {+ if (sourceRaw[i - 1] === '/') return null;+ const afterOpenTag = sourceRaw.slice(i + 1);+ if (!afterOpenTag.trimEnd().endsWith(`</${name}>`)) return null;+ return { leading: sourceRaw.slice(0, start), afterOpenTag };+ }+ }+ return null;+}++export function createAuthoredJsxSourceReader(+ parse: (sourceRaw: string) => PmNode,+): AuthoredJsxSourceReader {+ const cache = new Map<string, AuthoredElement | null>();++ const describeAuthoredElement = (+ sourceRaw: string,+ name: string,+ serialize: (authored: PmNode) => MdastNodes,+ ): AuthoredElement | null => {+ const doc = parse(sourceRaw);+ const only = doc.childCount === 1 ? doc.firstChild : null;+ if (only?.type.name !== 'jsxComponent' || only.attrs.kind !== 'element') return null;+ const emitted = plainJsxFlowElement(serialize(only), name);+ if (!emitted) return null;+ return {+ attributes: canonicalJson(emitted.attributes),+ children: canonicalJson(emitted.children),+ };+ };++ const authoredElement = (+ sourceRaw: string,+ name: string,+ serialize: (authored: PmNode) => MdastNodes,+ ): AuthoredElement | null => {+ const cached = cache.get(sourceRaw);+ if (cached !== undefined) return cached;+ const value = describeAuthoredElement(sourceRaw, name, serialize);+ if (cache.size >= AUTHORED_ELEMENT_CACHE_LIMIT) {+ const oldest = cache.keys().next().value;+ if (oldest !== undefined) cache.delete(oldest);+ }+ cache.set(sourceRaw, value);+ return value;+ };++ return {+ read(node, emitted, serialize) {+ const { componentName, sourceRaw, kind } = node.attrs;+ if (kind !== 'element' || node.childCount === 0) return null;+ if (typeof componentName !== 'string' || componentName === '') return null;+ if (typeof sourceRaw !== 'string' || sourceRaw === '') return null;+ const body = splitAuthoredOpenTag(sourceRaw, componentName);+ if (!body) return null;+ const authored = authoredElement(sourceRaw, componentName, serialize);+ if (!authored || canonicalJson(emitted.children) !== authored.children) return null;+ return canonicalJson(emitted.attributes) === authored.attributes+ ? { kind: 'verbatim' }+ : { kind: 'body', body };+ },+ };+}diff --git a/packages/core/src/markdown/mdast-augmentation.ts b/packages/core/src/markdown/mdast-augmentation.tsindex f03550b99..9d2996d91 100644--- a/packages/core/src/markdown/mdast-augmentation.ts+++ b/packages/core/src/markdown/mdast-augmentation.ts@@ -1,5 +1,10 @@import type { Position } from 'unist';+export interface AuthoredJsxBody {+ leading: string;+ afterOpenTag: string;+}+export interface EscapeProvenanceEntry {offset: number;char: string;@@ -266,6 +271,7 @@ declare module 'mdast-util-mdx-jsx' {interface MdxJsxFlowElementData {sourceRaw?: string;htmlBoundary?: { opener: string; closer: string };+ sourceBody?: AuthoredJsxBody;}interface MdxJsxTextElementData {sourceRaw?: string;diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.tsindex 861003fb4..ae8cb390b 100644--- a/packages/core/src/markdown/merged-walker.ts+++ b/packages/core/src/markdown/merged-walker.ts@@ -1,3 +1,4 @@+import type { Nodes, Parent, Root } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/resolve-image-url.ts b/packages/core/src/markdown/resolve-image-url.tsindex cf2c9da8e..3e7d4710a 100644--- a/packages/core/src/markdown/resolve-image-url.ts+++ b/packages/core/src/markdown/resolve-image-url.ts@@ -30,6 +30,10 @@ function posixNormalizeJoin(dir: string, rel: string): string {return isAbsolute ? `/${joined}` : joined;}+export function authoredImageUrl<Url>(src: Url, sourceUrl: Url | null | undefined): Url {+ return sourceUrl ?? src;+}+export function normalizeDocRelativeAssetUrl(rawUrl: string, sourcePath?: string): string {if (typeof rawUrl !== 'string' || rawUrl === '') return rawUrl;if (!sourcePath) return rawUrl;diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.tsindex 4d3250d3a..8be632ba4 100644--- a/packages/core/src/markdown/safe-url.ts+++ b/packages/core/src/markdown/safe-url.ts@@ -1,3 +1,4 @@+export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.tsindex 1075d5ce5..887647988 100644--- a/packages/core/src/markdown/to-markdown-handlers.ts+++ b/packages/core/src/markdown/to-markdown-handlers.ts@@ -638,6 +638,11 @@ export const toMarkdownHandlers = {const name = mdxNode.name ?? '';const attrs = serializeMdxJsxAttrs(mdxNode.attributes ?? []);+ const authoredBody = mdxNode.data?.sourceBody;+ if (authoredBody && name && !attrs.includes('\n')) {+ return `${authoredBody.leading}<${name}${attrs ? ` ${attrs}` : ''}>${authoredBody.afterOpenTag}`;+ }+if (!mdxNode.children || mdxNode.children.length === 0) {if (!name) return '';return attrs ? `<${name} ${attrs} />` : `<${name} />`;diff --git a/packages/core/src/registry/built-ins.ts b/packages/core/src/registry/built-ins.tsindex 9eb3fd381..b0ec2dc5b 100644--- a/packages/core/src/registry/built-ins.ts+++ b/packages/core/src/registry/built-ins.ts@@ -5,6 +5,7 @@ import {ALLOWED_PDF_MIME_TYPES,ALLOWED_VIDEO_MIME_TYPES,} from '../constants/upload.ts';+import { authoredImageUrl } from '../markdown/resolve-image-url.ts';import { emitMdxJsx } from '../markdown/serialize-helpers.ts';import { isLoomUrl } from '../utils/loom-embed.ts';import { isVimeoUrl } from '../utils/vimeo-embed.ts';@@ -1067,7 +1068,7 @@ export const builtInComponents: JsxComponentMeta[] = [| undefined;const image = {type: 'image' as const,- url: p?.sourceUrl ?? p?.src ?? '',+ url: authoredImageUrl(p?.src ?? '', p?.sourceUrl),alt: p?.alt ?? '',title: p?.title ?? null,};