Skip to content

Commit 544bff6

Browse files
committed
Refactor code-style
1 parent 939c667 commit 544bff6

File tree

2 files changed

+108
-75
lines changed

2 files changed

+108
-75
lines changed

lib/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@
8080
/**
8181
* @typedef HooksOptionsOnly
8282
* Configuration specifically for {@linkcode MarkdownHooks}.
83-
* @property {ReactNode} [fallback]
84-
* Fallback content to render while the processor processing the markdown.
83+
* @property {ReactNode | null | undefined} [fallback]
84+
* Content to render while the processor processing the markdown (optional).
8585
*/
8686

8787
/**
8888
* @typedef {Options & HooksOptionsOnly} HooksOptions
89-
* Configuration for {@linkcode MarkdownHooks}.
89+
* Configuration for {@linkcode MarkdownHooks},
90+
* extending the regular {@linkcode Options} with a `fallback` prop.
9091
*/
9192

9293
/**
@@ -221,14 +222,19 @@ export function MarkdownHooks(options) {
221222
function () {
222223
let cancelled = false
223224
const file = createFile(options)
225+
224226
processor.run(processor.parse(file), file, function (error, tree) {
225227
if (!cancelled) {
226228
setError(error)
227229
setTree(tree)
228230
}
229231
})
230232

231-
return () => {
233+
/**
234+
* @returns {undefined}
235+
* Nothing.
236+
*/
237+
return function () {
232238
cancelled = true
233239
}
234240
},

test.jsx

+98-71
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
* @import {Plugin} from 'unified'
77
*/
88

9+
/**
10+
* @typedef DeferredPlugin
11+
* Deferred plugin.
12+
* @property {Plugin<[]>} plugin
13+
* Plugin.
14+
* @property {(error: Error) => undefined} reject
15+
* Reject the plugin.
16+
* @property {() => undefined} resolve
17+
* Resolve the plugin.
18+
*/
19+
920
import assert from 'node:assert/strict'
1021
import test from 'node:test'
1122
import 'global-jsdom/register'
@@ -1112,160 +1123,176 @@ test('MarkdownAsync', async function (t) {
11121123
test('MarkdownHooks', async function (t) {
11131124
await t.test('should support `MarkdownHooks`', async function () {
11141125
const plugin = deferPlugin()
1115-
1116-
const {container} = render(
1126+
const result = render(
11171127
<MarkdownHooks children={'a'} rehypePlugins={[plugin.plugin]} />
11181128
)
11191129

1120-
assert.equal(container.innerHTML, '')
1130+
assert.equal(result.container.innerHTML, '')
1131+
11211132
plugin.resolve()
1122-
await waitFor(() => {
1123-
assert.notEqual(container.innerHTML, '')
1133+
1134+
await waitFor(function () {
1135+
assert.notEqual(result.container.innerHTML, '')
11241136
})
1125-
assert.equal(container.innerHTML, '<p>a</p>')
1137+
1138+
assert.equal(result.container.innerHTML, '<p>a</p>')
11261139
})
11271140

11281141
await t.test(
11291142
'should support async plugins w/ `MarkdownHooks` (`rehype-starry-night`)',
11301143
async function () {
11311144
const plugin = deferPlugin()
1132-
1133-
const {container} = render(
1145+
const result = render(
11341146
<MarkdownHooks
11351147
children={'```js\nconsole.log(3.14)'}
11361148
rehypePlugins={[plugin.plugin, rehypeStarryNight]}
11371149
/>
11381150
)
11391151

1140-
assert.equal(container.innerHTML, '')
1152+
assert.equal(result.container.innerHTML, '')
1153+
11411154
plugin.resolve()
1142-
await waitFor(() => {
1143-
assert.notEqual(container.innerHTML, '')
1155+
1156+
await waitFor(function () {
1157+
assert.notEqual(result.container.innerHTML, '')
11441158
})
1159+
11451160
assert.equal(
1146-
container.innerHTML,
1161+
result.container.innerHTML,
11471162
'<pre><code class="language-js"><span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-c1">3.14</span>)\n</code></pre>'
11481163
)
11491164
}
11501165
)
11511166

1152-
await t.test(
1153-
'should support `MarkdownHooks` loading fallback',
1154-
async function () {
1155-
const plugin = deferPlugin()
1167+
await t.test('should support `fallback`', async function () {
1168+
const plugin = deferPlugin()
1169+
const result = render(
1170+
<MarkdownHooks
1171+
children={'a'}
1172+
fallback="Loading"
1173+
rehypePlugins={[plugin.plugin]}
1174+
/>
1175+
)
11561176

1157-
const {container} = render(
1158-
<MarkdownHooks
1159-
children={'a'}
1160-
fallback="Loading"
1161-
rehypePlugins={[plugin.plugin]}
1162-
/>
1163-
)
1177+
assert.equal(result.container.innerHTML, 'Loading')
11641178

1165-
assert.equal(container.innerHTML, 'Loading')
1166-
plugin.resolve()
1167-
await waitFor(() => {
1168-
assert.notEqual(container.innerHTML, 'Loading')
1169-
})
1170-
assert.equal(container.innerHTML, '<p>a</p>')
1171-
}
1172-
)
1179+
plugin.resolve()
11731180

1174-
await t.test('should support `MarkdownHooks` that error', async function () {
1175-
const plugin = deferPlugin()
1181+
await waitFor(function () {
1182+
assert.notEqual(result.container.innerHTML, 'Loading')
1183+
})
11761184

1177-
const {container} = render(
1185+
assert.equal(result.container.innerHTML, '<p>a</p>')
1186+
})
1187+
1188+
await t.test('should support plugins that error', async function () {
1189+
const plugin = deferPlugin()
1190+
const result = render(
11781191
<ErrorBoundary>
11791192
<MarkdownHooks children={'a'} rehypePlugins={[plugin.plugin]} />
11801193
</ErrorBoundary>
11811194
)
11821195

1183-
assert.equal(container.innerHTML, '')
1196+
assert.equal(result.container.innerHTML, '')
1197+
1198+
console.info('\nNote: the below error (`Error: rejected`) is expected.\n')
1199+
11841200
plugin.reject(new Error('rejected'))
1185-
await waitFor(() => {
1186-
assert.notEqual(container.innerHTML, '')
1201+
1202+
await waitFor(function () {
1203+
assert.notEqual(result.container.innerHTML, '')
11871204
})
1188-
assert.equal(container.innerHTML, 'Error: rejected')
1205+
1206+
console.info('Note: the above error (`Error: rejected`) was expected.')
1207+
1208+
assert.equal(result.container.innerHTML, 'Error: rejected')
11891209
})
11901210

1191-
await t.test('should support `MarkdownHooks` rerenders', async function () {
1211+
await t.test('should support rerenders', async function () {
11921212
const pluginA = deferPlugin()
11931213
const pluginB = deferPlugin()
11941214

11951215
const result = render(
11961216
<MarkdownHooks children={'a'} rehypePlugins={[pluginA.plugin]} />
11971217
)
11981218

1219+
assert.equal(result.container.innerHTML, '')
1220+
11991221
result.rerender(
12001222
<MarkdownHooks children={'b'} rehypePlugins={[pluginB.plugin]} />
12011223
)
12021224

12031225
assert.equal(result.container.innerHTML, '')
1204-
pluginB.resolve()
1226+
12051227
pluginA.resolve()
1206-
await waitFor(() => {
1228+
pluginB.resolve()
1229+
1230+
await waitFor(function () {
12071231
assert.notEqual(result.container.innerHTML, '')
12081232
})
1233+
12091234
assert.equal(result.container.innerHTML, '<p>b</p>')
12101235
})
12111236
})
12121237

12131238
/**
1214-
* @typedef DeferredPlugin
1215-
* @property {Plugin<[]>} plugin
1216-
* A unified plugin
1217-
* @property {() => void} resolve
1218-
* Resolve the plugin.
1219-
* @property {(error: Error) => void} reject
1220-
* Reject the plugin.
1221-
*/
1222-
1223-
/**
1224-
* Create an async unified plugin which waits until a promise is resolved.
1239+
* Create an async unified plugin that waits until a promise is resolved or
1240+
* rejected from the outside.
12251241
*
12261242
* @returns {DeferredPlugin}
1227-
* The plugin and resolver.
1243+
* Deferred plugin object.
12281244
*/
12291245
function deferPlugin() {
1230-
/** @type {() => void} */
1231-
let res
12321246
/** @type {(error: Error) => void} */
1233-
let rej
1247+
let hoistedReject
1248+
/** @type {() => void} */
1249+
let hoistedResolve
12341250
/** @type {Promise<void>} */
1235-
const promise = new Promise((resolve, reject) => {
1236-
res = resolve
1237-
rej = reject
1251+
const promise = new Promise(function (resolve, reject) {
1252+
hoistedResolve = resolve
1253+
hoistedReject = reject
12381254
})
12391255

12401256
return {
1241-
resolve() {
1242-
res()
1257+
plugin() {
1258+
return function () {
1259+
return promise
1260+
}
12431261
},
12441262
reject(error) {
1245-
rej(error)
1263+
hoistedReject(error)
12461264
},
1247-
plugin() {
1248-
return () => promise
1265+
resolve() {
1266+
hoistedResolve()
12491267
}
12501268
}
12511269
}
12521270

1271+
/**
1272+
* Basic error boundary.
1273+
*/
12531274
class ErrorBoundary extends Component {
1254-
state = {
1255-
error: null
1256-
}
1257-
12581275
/**
12591276
* @param {Error} error
1277+
* Error.
1278+
* @returns {undefined}
1279+
* Nothing.
12601280
*/
12611281
componentDidCatch(error) {
12621282
this.setState({error})
12631283
}
12641284

12651285
render() {
1266-
const {children} = /** @type {{children: ReactNode}} */ (this.props)
1267-
const {error} = this.state
1286+
const props = /** @type {{children: ReactNode}} */ (this.props)
12681287

1269-
return error ? String(error) : children
1288+
return this.state.error ? String(this.state.error) : props.children
1289+
}
1290+
1291+
state = {
1292+
/**
1293+
* @type {Error | undefined}
1294+
* Error.
1295+
*/
1296+
error: undefined
12701297
}
12711298
}

0 commit comments

Comments
 (0)