Skip to content

Commit

Permalink
fix: parse HTML flow tags correctly out of the box
Browse files Browse the repository at this point in the history
Fixes #277
  • Loading branch information
petyosi committed Jan 5, 2024
1 parent 7739e7d commit 8840399
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"micromark-extension-gfm-table": "^2.0.0",
"micromark-extension-gfm-task-list-item": "^2.0.1",
"micromark-extension-mdx-jsx": "^3.0.0",
"micromark-extension-mdx-md": "^2.0.0",
"micromark-extension-mdxjs": "^3.0.0",
"react-hook-form": "^7.44.2",
"unidiff": "^1.0.2"
Expand Down
18 changes: 18 additions & 0 deletions src/examples/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ const markdownWithSpan = `
A paragraph with <span style="color: red" class="some">some red text <span style="color: blue">with some blue nesting.</span> in here.</span> in it.
`
export function HTag() {
return (
<>
<MDXEditor
markdown={`
Hello world
<h1>hello world</h1>
`}
plugins={[headingsPlugin(), diffSourcePlugin()]}
onChange={(md) => {
console.log('change', md)
}}
/>
</>
)
}

export function SpanWithColor() {
return (
Expand Down
12 changes: 6 additions & 6 deletions src/importMarkdownToLexical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export type MdastExtensions = Options['mdastExtensions']
* @typeParam UN - The type of the mdast node that is being visited.
* @group Markdown Processing
*/
export interface MdastImportVisitor<UN extends Mdast.Content> {
export interface MdastImportVisitor<UN extends Mdast.Nodes> {
/**
* The test function that determines if this visitor should be used for the given node.
* As a convenience, you can also pass a string here, which will be compared to the node's type.
*/
testNode: ((mdastNode: Mdast.Content | Mdast.Root) => boolean) | string
testNode: ((mdastNode: Mdast.Nodes) => boolean) | string
visitNode(params: {
/**
* The node that is currently being visited.
Expand Down Expand Up @@ -51,13 +51,13 @@ export interface MdastImportVisitor<UN extends Mdast.Content> {
* Adds formatting as a context for the current node and its children.
* This is necessary due to mdast treating formatting as a node, while lexical considering it an attribute of a node.
*/
addFormatting(format: typeof IS_BOLD | typeof IS_ITALIC | typeof IS_UNDERLINE | typeof IS_CODE, node?: Mdast.Content): void
addFormatting(format: typeof IS_BOLD | typeof IS_ITALIC | typeof IS_UNDERLINE | typeof IS_CODE, node?: Mdast.RootContent): void

/**
* Removes formatting as a context for the current node and its children.
* This is necessary due to mdast treating formatting as a node, while lexical considering it an attribute of a node.
*/
removeFormatting(format: typeof IS_BOLD | typeof IS_ITALIC | typeof IS_UNDERLINE | typeof IS_CODE, node?: Mdast.Content): void
removeFormatting(format: typeof IS_BOLD | typeof IS_ITALIC | typeof IS_UNDERLINE | typeof IS_CODE, node?: Mdast.RootContent): void
/**
* Access the current formatting context.
*/
Expand All @@ -80,7 +80,7 @@ function isParent(node: unknown): node is Mdast.Parent {
*/
export interface MdastTreeImportOptions {
root: LexicalRootNode
visitors: MdastImportVisitor<Mdast.Content>[]
visitors: MdastImportVisitor<Mdast.RootContent>[]
mdastRoot: Mdast.Root
}

Expand Down Expand Up @@ -167,7 +167,7 @@ export function importMdastTreeToLexical({ root, mdastRoot, visitors }: MdastTre
mdastNode.children.forEach((child) => visit(child, lexicalParent, mdastNode))
}

function visit(mdastNode: Mdast.Content | Mdast.Root, lexicalParent: LexicalNode, mdastParent: Mdast.Parent | null) {
function visit(mdastNode: Mdast.RootContent | Mdast.Root, lexicalParent: LexicalNode, mdastParent: Mdast.Parent | null) {
const visitor = visitors.find((visitor) => {
if (typeof visitor.testNode === 'string') {
return visitor.testNode === mdastNode.type
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/MdastHTMLNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type MdxNodeType = MdastHTMLNode['type']
* Determines if the given node is a HTML MDAST node.
* @group HTML
*/
export function isMdastHTMLNode(node: Mdast.Parent | Mdast.Content | Mdast.Root): node is MdastHTMLNode {
export function isMdastHTMLNode(node: Mdast.Nodes): node is MdastHTMLNode {
return (
MDX_NODE_TYPES.includes(node.type as unknown as MdxNodeType) &&
(htmlTags as readonly string[]).includes((node as MdastHTMLNode).name?.toLowerCase() ?? '')
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import {
createCommand
} from 'lexical'
import * as Mdast from 'mdast'

import { mdxJsxFromMarkdown, mdxJsxToMarkdown } from 'mdast-util-mdx-jsx'
import { mdxJsx } from 'micromark-extension-mdx-jsx'
import { mdxMd } from 'micromark-extension-mdx-md'
import React from 'react'
import { LexicalConvertOptions, exportMarkdownFromLexical } from '../../exportMarkdownFromLexical'
import {
Expand Down Expand Up @@ -210,7 +212,7 @@ const markdownSignal$ = Signal<string>((r) => {

// import configuration
/** @internal */
export const importVisitors$ = Cell<MdastImportVisitor<Mdast.Content>[]>([])
export const importVisitors$ = Cell<MdastImportVisitor<Mdast.Nodes>[]>([])
/** @internal */
export const syntaxExtensions$ = Cell<MarkdownParseOptions['syntaxExtensions']>([])
/** @internal */
Expand Down Expand Up @@ -791,7 +793,7 @@ export const corePlugin = realmPlugin<{
if (!params?.suppressHtmlProcessing) {
r.pubIn({
[addMdastExtension$]: mdxJsxFromMarkdown(),
[addSyntaxExtension$]: mdxJsx(),
[addSyntaxExtension$]: [mdxJsx(), mdxMd()],
[addToMarkdownExtension$]: mdxJsxToMarkdown(),
[addImportVisitor$]: MdastHTMLVisitor
})
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface JsxEditorProps {
* Determines wether the given node is a JSX node.
* @group JSX
*/
export function isMdastJsxNode(node: Mdast.Content | Mdast.Parent | Mdast.Root): node is MdastJsx {
export function isMdastJsxNode(node: Mdast.Nodes): node is MdastJsx {
return node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement'
}

Expand Down

0 comments on commit 8840399

Please sign in to comment.