Skip to content

Commit 2927317

Browse files
fix(editor): handle mdast nodes without position data (#4850)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent e180bfa commit 2927317

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/fern-docs/mdx/src/convert.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ export function mdxToHtml(rootContent: string, options?: MdxToHtmlOptions): MdxT
204204
throw new Error(`Unsupported node type: ${nodeType}`);
205205
}
206206

207+
if (positionStart === -1 || positionEnd === -1) {
208+
return getToHastDefaultHandler(nodeType)(state, node, parents);
209+
}
210+
207211
// Special case: if this is a paragraph that contains only images/imageReferences,
208212
// treat it as a custom element to avoid nested rendering
209213
if (nodeType === "paragraph" && node.children && Array.isArray(node.children)) {
@@ -236,7 +240,7 @@ export function mdxToHtml(rootContent: string, options?: MdxToHtmlOptions): MdxT
236240
}
237241

238242
// Default handler for custom elements
239-
function customElementHandler(_state: ToHastState, node: any, __?: MdastParents) {
243+
function customElementHandler(state: ToHastState, node: any, parents?: MdastParents) {
240244
const { type, name, positionStart, positionEnd } = getNodeInfo(node);
241245

242246
const nodeType = type as CustomElementsType;
@@ -259,6 +263,10 @@ export function mdxToHtml(rootContent: string, options?: MdxToHtmlOptions): MdxT
259263
}
260264
}
261265

266+
if (positionStart === -1 || positionEnd === -1) {
267+
return getToHastDefaultHandler(nodeType as ToHastDefaultHandlersType)?.(state, node, parents) ?? node;
268+
}
269+
262270
const { content } = getNodeContent(node, rootContent);
263271

264272
return mdxUnsupportedCustomElementNodev2(generateContentHash(positionStart, positionEnd, content), content);
@@ -616,15 +624,12 @@ function getNodeInfo(node: any) {
616624
}
617625
const positionStart = node.position?.start?.offset;
618626
const positionEnd = node.position?.end?.offset;
619-
if (positionStart == null || positionEnd == null) {
620-
throw new Error("mdast node unexpectedly does not have a position");
621-
}
622627

623628
return {
624629
type: node.type as string,
625630
name: node.name as string | undefined,
626-
positionStart: positionStart as number,
627-
positionEnd: positionEnd as number
631+
positionStart: positionStart ?? -1,
632+
positionEnd: positionEnd ?? -1
628633
};
629634
}
630635

0 commit comments

Comments
 (0)