Skip to content

Commit

Permalink
Fix types to allow definitions in flow elements
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 31, 2023
1 parent 5f8b988 commit f9794c4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 14 deletions.
121 changes: 111 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Parent as MdastParent,
Literal as MdastLiteral,
BlockContent,
DefinitionContent,
PhrasingContent
} from 'mdast'
import type {Program} from 'estree-jsx'
Expand All @@ -16,52 +17,140 @@ export {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './lib/index.js'
export type {ToMarkdownOptions} from './lib/index.js'

// Expose node types.
/**
* MDX JSX attribute value set to an expression.
*
* ```markdown
* > | <a b={c} />
* ^^^
* ```
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttributeValueExpression extends MdastLiteral {
/**
* Node type.
*/
type: 'mdxJsxAttributeValueExpression'
data?: {estree?: Program} & MdastLiteral['data']
data?: {
/**
* Program node from estree.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
estree?: Program | null | undefined
} & MdastLiteral['data']
}

/**
* MDX JSX attribute as an expression.
*
* ```markdown
* > | <a {...b} />
* ^^^^^^
* ```
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxExpressionAttribute extends MdastLiteral {
/**
* Node type.
*/
type: 'mdxJsxExpressionAttribute'
data?: {estree?: Program} & MdastLiteral['data']
data?: {
/**
* Program node from estree.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
estree?: Program | null | undefined
} & MdastLiteral['data']
}

/**
* MDX JSX attribute with a key.
*
* ```markdown
* > | <a b="c" />
* ^^^^^
* ```
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxAttribute extends MdastNode {
/**
* Node type.
*/
type: 'mdxJsxAttribute'
/**
* Attribute name.
*/
name: string
/**
* Attribute value.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
value?: MdxJsxAttributeValueExpression | string | null | undefined
}

/**
* MDX JSX element node, occurring in flow (block).
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface MdxJsxElementFields {
export interface MdxJsxFlowElement extends MdastParent {
/**
* Node type.
*/
type: 'mdxJsxFlowElement'
/**
* MDX JSX element name (`null` for fragments).
*/
// eslint-disable-next-line @typescript-eslint/ban-types
name: string | null
/**
* MDX JSX element attributes.
*/
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
/**
* Content.
*/
children: Array<BlockContent | DefinitionContent>
}

/**
* MDX JSX element node, occurring in text (phrasing).
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxFlowElement extends MdxJsxElementFields, MdastParent {
type: 'mdxJsxFlowElement'
children: BlockContent[]
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxJsxTextElement extends MdxJsxElementFields, MdastParent {
export interface MdxJsxTextElement extends MdastParent {
/**
* Node type.
*/
type: 'mdxJsxTextElement'
/**
* MDX JSX element name (`null` for fragments).
*/
// eslint-disable-next-line @typescript-eslint/ban-types
name: string | null
/**
* MDX JSX element attributes.
*/
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
/**
* Content.
*/
children: PhrasingContent[]
}

// Add nodes to mdast content.
declare module 'mdast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface StaticPhrasingContentMap {
/**
* MDX JSX element node, occurring in text (phrasing).
*/
mdxJsxTextElement: MdxJsxTextElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface BlockContentMap {
/**
* MDX JSX element node, occurring in flow (block).
*/
mdxJsxFlowElement: MdxJsxFlowElement
}
}
Expand All @@ -70,13 +159,25 @@ declare module 'mdast' {
declare module 'hast' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface RootContentMap {
/**
* MDX JSX element node, occurring in text (phrasing).
*/
mdxJsxTextElement: MdxJsxTextElement
/**
* MDX JSX element node, occurring in flow (block).
*/
mdxJsxFlowElement: MdxJsxFlowElement
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface ElementContentMap {
/**
* MDX JSX element node, occurring in text (phrasing).
*/
mdxJsxTextElement: MdxJsxTextElement
/**
* MDX JSX element node, occurring in flow (block).
*/
mdxJsxFlowElement: MdxJsxFlowElement
}
}
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"rules": {
"@typescript-eslint/ban-types": "off"
}
"prettier": true
},
"remarkConfig": {
"plugins": [
Expand Down

0 comments on commit f9794c4

Please sign in to comment.