react-markdown
Version:
Render Markdown as React components
190 lines (189 loc) • 4.7 kB
TypeScript
export type IntrinsicElements = JSX.IntrinsicElements
export type ReactNode = import('react').ReactNode
export type Position = import('unist').Position
export type Element = import('hast').Element
export type Root = import('hast').Root
export type Text = import('hast').Text
export type Comment = import('hast').Comment
export type Doctype = import('hast').DocType
export type Info = {
space: string | null
attribute: string | null
property: string | null
boolean: boolean
booleanish: boolean
overloadedBoolean: boolean
number: boolean
commaSeparated: boolean
spaceSeparated: boolean
commaOrSpaceSeparated: boolean
mustUseProperty: boolean
defined: boolean
}
export type Schema = {
property: {
[x: string]: Info
}
normal: {
[x: string]: string
}
space: string | null
}
export type Raw = {
type: 'raw'
value: string
}
export type Context = {
options: TransformOptions
schema: Schema
listDepth: number
}
export type TransformLink = (
href: string,
children: Array<Comment | Element | Text>,
title: string | null
) => string
export type TransformImage = (
src: string,
alt: string,
title: string | null
) => string
export type TransformLinkTarget = (
href: string,
children: Array<Comment | Element | Text>,
title: string | null
) => string
export type ReactMarkdownNames = keyof IntrinsicElements
/**
* To do: is `data-sourcepos` typeable?
*/
export type ReactBaseProps = {
[x: string]: unknown
}
export type ReactMarkdownProps = {
node: Element
key: string
children: ReactNode[]
/**
* Passed when `options.rawSourcePos` is given
*/
sourcePosition?: Position | null
/**
* Passed when `options.includeElementIndex` is given
*/
index?: number
/**
* Passed when `options.includeElementIndex` is given
*/
siblingCount?: number
}
export type NormalComponent = (
props: ReactBaseProps & ReactMarkdownProps
) => ReactNode
export type CodeComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
inline?: boolean
}
) => ReactNode
export type HeadingComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
level: number
}
) => ReactNode
export type LiComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
checked: boolean | null
index: number
ordered: boolean
}
) => ReactNode
export type OrderedListComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
depth: number
ordered: true
}
) => ReactNode
export type TableCellComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
style?: {
[x: string]: unknown
}
isHeader: boolean
}
) => ReactNode
export type TableRowComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
isHeader: boolean
}
) => ReactNode
export type UnorderedListComponent = (
props: ReactBaseProps &
ReactMarkdownProps & {
depth: number
ordered: false
}
) => ReactNode
export type SpecialComponents = {
code: CodeComponent | ReactMarkdownNames
h1: HeadingComponent | ReactMarkdownNames
h2: HeadingComponent | ReactMarkdownNames
h3: HeadingComponent | ReactMarkdownNames
h4: HeadingComponent | ReactMarkdownNames
h5: HeadingComponent | ReactMarkdownNames
h6: HeadingComponent | ReactMarkdownNames
li: LiComponent | ReactMarkdownNames
ol: OrderedListComponent | ReactMarkdownNames
td: TableCellComponent | ReactMarkdownNames
th: TableCellComponent | ReactMarkdownNames
tr: TableRowComponent | ReactMarkdownNames
ul: UnorderedListComponent | ReactMarkdownNames
}
export type NormalComponents = Record<
Exclude<ReactMarkdownNames, keyof SpecialComponents>,
NormalComponent | ReactMarkdownNames
>
export type Components = Partial<NormalComponents & SpecialComponents>
export type TransformOptions = {
sourcePos?: boolean
rawSourcePos?: boolean
skipHtml?: boolean
includeElementIndex?: boolean
transformLinkUri?: false | TransformLink
transformImageUri?: TransformImage
linkTarget?: string | TransformLinkTarget
components?: Components
}
/**
* @param {Context} context
* @param {Element} node
* @param {number} index
* @param {Element|Root} parent
*/
declare function toReact(
context: Context,
node: Element,
index: number,
parent: Element | Root
): React.DOMElement<
{
[x: string]: unknown
},
globalThis.Element
>
/**
* @param {Context} context
* @param {Element|Root} node
*/
declare function childrenToReact(
context: Context,
node: Element | Root
): React.ReactNode[]
import style = require('style-to-object')
import React = require('react')
export {toReact as hastToReact, childrenToReact as hastChildrenToReact}