Skip to content
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

Add MarkdownHooks fallback prop #897

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @typedef {import('./lib/index.js').AllowElement} AllowElement
* @typedef {import('./lib/index.js').Components} Components
* @typedef {import('./lib/index.js').ExtraProps} ExtraProps
* @typedef {import('./lib/index.js').HooksOptions} HooksOptions
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').UrlTransform} UrlTransform
*/
Expand Down
24 changes: 18 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @import {Element, Nodes, Parents, Root} from 'hast'
* @import {Root as MdastRoot} from 'mdast'
* @import {ComponentType, JSX, ReactElement} from 'react'
* @import {ComponentType, JSX, ReactElement, ReactNode} from 'react'
* @import {Options as RemarkRehypeOptions} from 'remark-rehype'
* @import {BuildVisitor} from 'unist-util-visit'
* @import {PluggableList, Processor} from 'unified'
Expand Down Expand Up @@ -77,6 +77,18 @@
* Change URLs (default: `defaultUrlTransform`)
*/

/**
* @typedef HooksOptionsOnly
* Configuration specifically for {@linkcode MarkdownHooks}.
* @property {ReactNode} [fallback]
* Fallback content to render while the processor processing the markdown.
*/

/**
* @typedef {Options & HooksOptionsOnly} HooksOptions
* Configuration for {@linkcode MarkdownHooks}.
*/

/**
* @callback UrlTransform
* Transform all URLs.
Expand All @@ -94,7 +106,7 @@ import {unreachable} from 'devlop'
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
import {urlAttributes} from 'html-url-attributes'
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
import {createElement, useEffect, useState} from 'react'
import {useEffect, useState} from 'react'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
Expand Down Expand Up @@ -193,10 +205,10 @@ export async function MarkdownAsync(options) {
* For async support on the server,
* see {@linkcode MarkdownAsync}.
*
* @param {Readonly<Options>} options
* @param {Readonly<HooksOptions>} options
* Props.
* @returns {ReactElement}
* React element.
* @returns {ReactNode}
* React node.
*/
export function MarkdownHooks(options) {
const processor = createProcessor(options)
Expand All @@ -223,7 +235,7 @@ export function MarkdownHooks(options) {

if (error) throw error

return tree ? post(tree, options) : createElement(Fragment)
return tree ? post(tree, options) : options.fallback
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,28 @@ test('MarkdownHooks', async function (t) {
}
)

await t.test(
'should support `MarkdownHooks` loading fallback',
async function () {
const plugin = deferPlugin()

const {container} = render(
<MarkdownHooks
children={'a'}
fallback="Loading"
rehypePlugins={[plugin.plugin]}
/>
)

assert.equal(container.innerHTML, 'Loading')
plugin.resolve()
await waitFor(() => {
assert.notEqual(container.innerHTML, 'Loading')
})
assert.equal(container.innerHTML, '<p>a</p>')
}
)

await t.test('should support `MarkdownHooks` that error', async function () {
const plugin = deferPlugin()

Expand Down