import { createStreamingHtmlRenderer } from '../../src/html/streaming.js'
import { renderHtml } from '../../src/html.js'
import { createElement, useEffect, type ReactNode } from 'react'
import { flushSync } from 'react-dom'
import { createRoot, hydrateRoot } from 'react-dom/client'
import { renderToString } from 'react-dom/server'
import { Markdown } from '../../src/react.js'
import type { MarkdownExtension } from '../../src/types.js'
import type { MarkdownProps } from '../../src/react.js'
import { parseMarkdown } from '../../src/parser.js'
import { streamingMarkdownExtension } from '../../src/extensions/streaming.js'
const extensions = [streamingMarkdownExtension()]
function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message)
}
function HydrationReady({ children, onReady }: { children: ReactNode; onReady: () => void }) {
useEffect(onReady, [onReady])
return children
}
async function verifyStreamingReact() {
const container = document.body.appendChild(document.createElement('div'))
const root = createRoot(container)
let checks = verifyStreamingHtml()
const render = (source: string, options: Omit = {}) => {
flushSync(() => root.render({source}))
const expected = parseMarkdown(source).children.find(node => node.type === 'code')
assert(expected?.type === 'code', 'Test source must contain code')
assert(container.querySelector('pre code')?.textContent === expected.value, 'Code changed during a client update')
checks++
}
const code = Array.from({ length: 100 }, (_, index) => `${index}: & "quoted" š©š½āš» eĢ\n`).join('')
const source = '```ts\n' + code
render(source)
const first = container.querySelector('code')!.firstChild!
const text = first.textContent
for (const suffix of ['tail', 'tail\n', 'tail\nnext', 'tail\nnext\n```', 'tail\nnext\n```\n\nAfter']) {
render(source + suffix)
assert(container.querySelector('code')!.firstChild === first, 'Appending code replaced settled DOM text')
assert(first.textContent === text, 'Appending code rewrote settled DOM text')
checks++
}
for (const replacement of ['```\n', '```ts\nchanged\n', '```ts\n\n\n\n\n', source.replace('0:', 'edited:'), source.slice(0, 70), source]) render(replacement)
let customChildren: unknown
render(source, { components: { code: ({ children }: { children: string }) => {
customChildren = children
return {children}
} } })
assert(customChildren === code, 'Custom code components must still receive string children')
render(source, { highlighter: value => value.replaceAll('&', '&').replaceAll('<', '<') })
render(source)
flushSync(() => root.unmount())
const errors: unknown[] = []
container.innerHTML = renderToString(createElement(Markdown, { children: source, extensions }))
const serverText = container.querySelector('code')!.firstChild
let hydratedReady!: () => void
const hydratedCommitted = new Promise(resolve => { hydratedReady = resolve })
const hydrationView = (value: string) => {value}
const hydrated = hydrateRoot(container, hydrationView(source), { onRecoverableError: error => errors.push(error) })
await hydratedCommitted
assert(errors.length === 0, 'Code text segmentation caused a hydration mismatch')
assert(container.querySelector('code')!.firstChild === serverText, 'Hydration replaced the server text')
flushSync(() => hydrated.render(hydrationView(source + 'hydrated update')))
assert(container.querySelector('code')!.textContent === code + 'hydrated update', 'Hydrated code did not update correctly')
flushSync(() => hydrated.unmount())
const inlineExtensions: MarkdownExtension[] = [{ name: 'mention', inlineParser: {
markers: '@',
parse({ source, index, inLink }) {
if (inLink) return
const match = /^@([a-z]+)/.exec(source.slice(index))
if (!match) return
return { length: match[0].length, node: { type: 'inlineCode', value: match[1]! } }
},
} }]
const inlineSource = '# @hello **@world** [@label](/safe)'
container.innerHTML = renderToString({inlineSource})
const inlineServerNode = container.querySelector('code')
let inlineReady!: () => void
const inlineCommitted = new Promise(resolve => { inlineReady = resolve })
const inlineView = (value: string) => {value}
const inlineRoot = hydrateRoot(container, inlineView(inlineSource), { onRecoverableError: error => errors.push(error) })
await inlineCommitted
assert(errors.length === 0, 'Source inline parser hydration errors: ' + errors.map(String).join('; '))
assert(container.querySelector('code') === inlineServerNode, 'Source inline parser replaced server markup')
assert(container.querySelector('strong code')?.textContent === 'world', 'Inline parser did not run inside emphasis')
assert(container.querySelector('a')?.textContent === '@label', 'Inline parser lost link-label context')
flushSync(() => inlineRoot.render(inlineView('@hel')))
assert(container.querySelector('code')?.textContent === 'hel', 'Inline parser did not process a partial update')
flushSync(() => inlineRoot.render(inlineView('@hello')))
assert(container.querySelector('code')?.textContent === 'hello', 'Inline parser did not process the completed update')
flushSync(() => inlineRoot.unmount())
checks += 6
container.remove()
return { checks: checks + 4 }
}
function verifyStreamingHtml() {
const actual = document.createElement('div'), expected = document.createElement('div')
let checks = 0
for (const source of ['```ts title="α<&"\nconst x = "<&"\nš\n```\n\nAfter', '# Answer\n\n- Item\n\n[unsafe](javascript:alert(1))\n']) {
const renderer = createStreamingHtmlRenderer()
for (let end = 0; end <= source.length; end++) {
const prefix = source.slice(0, end), html = renderer.render(prefix)
const reference = renderHtml(prefix, { extensions })
assert(html === reference, 'Streaming HTML changed a complete prefix')
actual.innerHTML = html;expected.innerHTML = reference
assert(actual.innerHTML === expected.innerHTML, 'Streaming HTML DOM differs')
assert(!actual.querySelector('script'), 'Streaming HTML relaxed raw HTML safety')
checks++
}
renderer.reset()
}
return checks
}
declare global {
interface Window { verifyStreamingReact: typeof verifyStreamingReact }
}
window.verifyStreamingReact = verifyStreamingReact