import { describe, expect, it } from 'vitest'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { parseMarkdown } from 'comark'
import type { NodeTransform } from 'comark'
import { MarkdownDocument } from '../src/components/MarkdownDocument'
import { defineMarkdownDocumentComponent } from '../src/index'
function Pill({ label = '', tone = '', children }: { label?: string; tone?: string; children?: React.ReactNode }) {
return (
{label}
{children}
)
}
describe('MarkdownDocument — transformNode', () => {
it('swaps a node for a component with new props, and children see those props', async () => {
const value = await parseMarkdown(
'---\nname: Ada\n---\n::badge{:label="frontmatter.name"}\n:span{:title="props.tone"}\n::'
)
const transformNode: NodeTransform = ({ tag, props }) =>
tag === 'badge' ? { tag: 'pill', props: { ...props, tone: 'info' } } : undefined
const html = renderToString(
)
expect(html).toContain('Ada')
})
it('renders a node turned into a native void element without its children', async () => {
const value = await parseMarkdown('Type here')
const html = renderToString(
tag === 'p' ? { tag: 'input', props: { placeholder: 'x', children: 'y' } } : undefined
}
/>
)
expect(html).toContain('')
})
it('keeps children for a custom component mapped to a void tag', async () => {
const value = await parseMarkdown('Caption')
const Figure = ({ children }: { children?: React.ReactNode }) => {children}
const html = renderToString(
(tag === 'p' ? { tag: 'img' } : undefined)}
/>
)
expect(html).toContain('Caption')
})
it('runs config-level transforms before instance-level ones', async () => {
const value = await parseMarkdown('# Title')
const Renderer = defineMarkdownDocumentComponent({
transformNode: ({ tag }) => (tag === 'h1' ? { tag: 'h2' } : undefined),
})
const html = renderToString(
(tag === 'h2' ? { props: { ...props, className: 'demoted' } } : undefined)}
/>
)
expect(html).toContain('Title
')
})
})