safe-mdx 1.3.3 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/parse.ts CHANGED
@@ -5,9 +5,9 @@ import { Root, RootContent } from 'mdast'
5
5
  import { remark } from 'remark'
6
6
  import remarkGfm from 'remark-gfm'
7
7
  import remarkMdx from 'remark-mdx'
8
- import { parseHtmlToMdxAst } from './html/html-to-mdx-ast.js'
8
+ import { parseHtmlToMdxAst, remarkMdxJsxNormalize } from './html/html-to-mdx-ast.js'
9
9
 
10
- export { parseHtmlToMdxAst }
10
+ export { parseHtmlToMdxAst, remarkMdxJsxNormalize }
11
11
 
12
12
  export function mdxParse(code: string) {
13
13
  const file = mdxProcessor.processSync(code)
package/src/safe-mdx.tsx CHANGED
@@ -186,7 +186,7 @@ export class MdastToJsx {
186
186
 
187
187
  mapMdastChildren(node: any) {
188
188
  const res = node.children
189
- ?.flatMap((child) => this.mdastTransformer(child))
189
+ ?.flatMap((child) => this.mdastTransformer(child, node.type))
190
190
  .filter(Boolean)
191
191
  if (Array.isArray(res)) {
192
192
  if (!res.length) {
@@ -287,7 +287,7 @@ export class MdastToJsx {
287
287
  )
288
288
  }
289
289
  default: {
290
- return this.mdastTransformer(node)
290
+ return this.mdastTransformer(node, 'mdxJsxElement')
291
291
  }
292
292
  }
293
293
  }
@@ -564,14 +564,14 @@ export class MdastToJsx {
564
564
  }
565
565
 
566
566
  run() {
567
- const res = this.mdastTransformer(this.mdast) as ReactNode
567
+ const res = this.mdastTransformer(this.mdast, 'root') as ReactNode
568
568
  if (Array.isArray(res) && res.length === 1) {
569
569
  return res[0]
570
570
  }
571
571
  return res
572
572
  }
573
573
 
574
- mdastTransformer(node: MyRootContent): ReactNode {
574
+ mdastTransformer(node: MyRootContent, parentType?: string): ReactNode {
575
575
  if (!node) {
576
576
  return []
577
577
  }
@@ -580,7 +580,7 @@ export class MdastToJsx {
580
580
  if (this.renderNode) {
581
581
  const customResult = this.renderNode(
582
582
  node,
583
- this.mdastTransformer.bind(this),
583
+ (n: MyRootContent) => this.mdastTransformer(n, node.type),
584
584
  )
585
585
  if (customResult !== undefined) {
586
586
  return customResult
@@ -928,9 +928,10 @@ export class MdastToJsx {
928
928
  return []
929
929
  }
930
930
 
931
- // Parse HTML to MDX AST using the new approach
931
+ // Parse HTML to MDX AST using the new approach - always returns an array
932
932
  const mdxAst = htmlToMdxAst({
933
933
  html: text,
934
+ parentType: parentType || 'root',
934
935
  convertTagName: ({ tagName }) => {
935
936
  const lowerTag = tagName.toLowerCase()
936
937
  // Only keep valid HTML elements
@@ -943,11 +944,7 @@ export class MdastToJsx {
943
944
  })
944
945
 
945
946
  // Process the MDX AST nodes
946
- if (Array.isArray(mdxAst)) {
947
- return mdxAst.map(child => this.mdastTransformer(child))
948
- } else {
949
- return this.mdastTransformer(mdxAst)
950
- }
947
+ return mdxAst.map(child => this.mdastTransformer(child, parentType))
951
948
  }
952
949
  case 'imageReference': {
953
950
  return []