Skip to content

Add onComplete callback for completing processing in MarkdownHooks #913

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
* Configuration specifically for {@linkcode MarkdownHooks}.
* @property {ReactNode | null | undefined} [fallback]
* Content to render while the processor processing the markdown (optional).
* @property {(file: VFile, hast: Root | undefined, error: Error | undefined) => unknown} [onComplete]
* Callback when the processor is done processing.
*/

/**
Expand Down Expand Up @@ -232,6 +234,7 @@ export function MarkdownHooks(options) {
if (!cancelled) {
setError(error)
setTree(tree)
options.onComplete?.(file, tree, error)
}
})

Expand All @@ -243,7 +246,7 @@ export function MarkdownHooks(options) {
cancelled = true
}
},
[options.children, processor]
[options.children, processor, options.onComplete]
)

if (error) throw error
Expand Down
24 changes: 24 additions & 0 deletions test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,30 @@ test('MarkdownHooks', async function (t) {
assert.equal(result.container.innerHTML, '<p>a</p>')
})

await t.test('should support `onComplete`', async function () {
const plugin = deferPlugin()
let succes = false
render(
<MarkdownHooks
children={'a'}
onComplete={function () {
succes = true
}}
rehypePlugins={[plugin.plugin]}
/>
)

assert.equal(succes, false)

plugin.resolve()

await waitFor(function () {
assert.notEqual(succes, false)
})

assert.equal(succes, true)
})

await t.test('should support plugins that error', async function () {
const plugin = deferPlugin()
const result = render(
Expand Down