-
Notifications
You must be signed in to change notification settings - Fork 16
fix(mdxish): fix rendering callout from magic blocks #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,16 @@ | |
| */ | ||
| import type { BlockHit } from '../../../lib/utils/extractMagicBlocks'; | ||
| import type { Code, Parent, Root as MdastRoot, RootContent } from 'mdast'; | ||
| import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx'; | ||
| import type { Plugin } from 'unified'; | ||
|
|
||
| import remarkGfm from 'remark-gfm'; | ||
| import remarkParse from 'remark-parse'; | ||
| import { unified } from 'unified'; | ||
| import { visit } from 'unist-util-visit'; | ||
|
|
||
| import { toAttributes } from '../../utils'; | ||
|
|
||
| /** | ||
| * Matches legacy magic block syntax: [block:TYPE]...JSON...[/block] | ||
| * Group 1: block type (e.g., "image", "code", "callout") | ||
|
|
@@ -260,19 +263,36 @@ function parseMagicBlock(raw: string, options: ParseMagicBlockOptions = {}): Mda | |
|
|
||
| if (!(calloutJson.title || calloutJson.body)) return []; | ||
|
|
||
| return [ | ||
| wrapPinnedBlocks( | ||
| { | ||
| children: [...textToBlock(calloutJson.title || ''), ...textToBlock(calloutJson.body || '')], | ||
| data: { | ||
| hName: 'rdme-callout', | ||
| hProperties: { icon, theme: theme || 'default', title: calloutJson.title, value: calloutJson.body }, | ||
| }, | ||
| type: 'rdme-callout', | ||
| }, | ||
| json, | ||
| ), | ||
| ]; | ||
| const titleBlocks = textToBlock(calloutJson.title || ''); | ||
| const bodyBlocks = textToBlock(calloutJson.body || ''); | ||
|
|
||
| const children: MdastNode[] = []; | ||
| if (titleBlocks.length > 0 && titleBlocks[0].type === 'paragraph') { | ||
| const firstTitle = titleBlocks[0] as { children?: MdastNode[] }; | ||
| const heading = { | ||
| type: 'heading', | ||
| depth: 3, | ||
| children: (firstTitle.children || []) as unknown[], | ||
| }; | ||
| children.push(heading as unknown as MdastNode); | ||
| children.push(...titleBlocks.slice(1), ...bodyBlocks); | ||
| } else { | ||
| children.push(...titleBlocks, ...bodyBlocks); | ||
| } | ||
|
|
||
| // Create mdxJsxFlowElement directly for mdxish | ||
| const calloutElement: MdxJsxFlowElement = { | ||
| type: 'mdxJsxFlowElement', | ||
| name: 'Callout', | ||
| attributes: toAttributes({ icon, theme: theme || 'default', type: theme || 'default' }, [ | ||
| 'icon', | ||
| 'theme', | ||
| 'type', | ||
| ]), | ||
| children: children as MdxJsxFlowElement['children'], | ||
| }; | ||
|
|
||
| return [wrapPinnedBlocks(calloutElement as unknown as MdastNode, json)]; | ||
| } | ||
|
|
||
| // Parameters: renders as a table (used for API parameters, etc.) | ||
|
|
@@ -388,15 +408,45 @@ const magicBlockRestorer: Plugin<[{ blocks: BlockHit[] }], MdastRoot> = | |
| const magicBlockKeys = new Map(blocks.map(({ key, raw }) => [key, raw] as const)); | ||
|
|
||
| // Find inlineCode nodes that match our placeholder tokens | ||
| const modifications: { children: RootContent[]; index: number; parent: Parent }[] = []; | ||
|
|
||
| visit(tree, 'inlineCode', (node: Code, index: number, parent: Parent) => { | ||
| if (!parent || index == null) return; | ||
| const raw = magicBlockKeys.get(node.value); | ||
| if (!raw) return; | ||
|
|
||
| // Parse the original magic block and replace the placeholder with the result | ||
| const children = parseMagicBlock(raw) as unknown as RootContent[]; | ||
| if (!children.length) return; | ||
|
|
||
| // Check if first child is a flow element that needs unwrapping (mdxJsxFlowElement, etc.) | ||
| const needsUnwrapping = (child: RootContent): boolean => { | ||
| return child.type === 'mdxJsxFlowElement'; | ||
| }; | ||
|
Comment on lines
+421
to
+424
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could move out of the top visitor function so it is only created once. |
||
|
|
||
| if (children[0] && needsUnwrapping(children[0]) && parent.type === 'paragraph') { | ||
| // Find paragraph's parent and unwrap | ||
| let paragraphParent: Parent | undefined; | ||
| visit(tree, 'paragraph', (p, pIndex, pParent) => { | ||
| if (p === parent && pParent && 'children' in pParent) { | ||
| paragraphParent = pParent as Parent; | ||
| return false; | ||
| } | ||
| return undefined; | ||
| }); | ||
|
|
||
| if (paragraphParent) { | ||
| const paragraphIndex = paragraphParent.children.indexOf(parent as RootContent); | ||
| if (paragraphIndex !== -1) { | ||
| modifications.push({ children, index: paragraphIndex, parent: paragraphParent }); | ||
| } | ||
| } | ||
| } else { | ||
| parent.children.splice(index, 1, ...children); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we are mutating |
||
| } | ||
| }); | ||
|
|
||
| // Apply modifications in reverse order to avoid index shifting | ||
| modifications.reverse().forEach(({ children, index, parent }) => { | ||
| parent.children.splice(index, 1, ...children); | ||
| }); | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.