Skip to content

Commit ea8e6ba

Browse files
committed
refactor(html)!: clarify renderer API names
Authored by an AI agent on behalf of Atinux; reviewed through the requested changes and automated checks.
1 parent 25fff03 commit ea8e6ba

9 files changed

Lines changed: 124 additions & 118 deletions

File tree

packages/comark-html/src/index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ParserOptions, RendererOptions } from 'comark'
22
import { createMarkdownParser } from 'comark'
3-
import { renderHtml } from './render.ts'
3+
import { renderHtmlFromDocument } from './render.ts'
44

5-
export { renderHtml } from './render.ts'
5+
export { renderHtmlFromDocument } from './render.ts'
66

77
/**
88
* Creates a reusable parse+render function with pre-configured options.
@@ -13,43 +13,43 @@ export { renderHtml } from './render.ts'
1313
*
1414
* @example
1515
* ```typescript
16-
* import { createRender } from '@comark/html'
17-
* import highlight from 'comark/plugins/highlight'
16+
* import { createHtmlRenderer } from '@comark/html'
17+
* import highlight from '@comark/html/plugins/highlight'
1818
*
19-
* const render = createRender({
19+
* const renderHtml = createHtmlRenderer({
2020
* plugins: [highlight()],
2121
* components: {
22-
* alert: ([, attrs, ...children], { render }) =>
23-
* `<div class="alert alert-${attrs.type}">${render(children)}</div>`
22+
* alert: async ([, attrs, ...children], { render }) =>
23+
* `<div class="alert alert-${attrs.type}">${await render(children)}</div>`
2424
* }
2525
* })
2626
*
27-
* const html = await render('# Hello\n\n**Bold** text.')
27+
* const html = await renderHtml('# Hello\n\n**Bold** text.')
2828
* ```
2929
*/
30-
export function createRender(options?: ParserOptions & RendererOptions): (markdown: string) => Promise<string> {
31-
const parse = createMarkdownParser(options)
30+
export function createHtmlRenderer(options?: ParserOptions & RendererOptions): (markdown: string) => Promise<string> {
31+
const parseMarkdown = createMarkdownParser(options)
3232
return async (markdown: string) => {
33-
const tree = await parse(markdown)
34-
return await renderHtml(tree, options as RendererOptions)
33+
const document = await parseMarkdown(markdown)
34+
return await renderHtmlFromDocument(document, options as RendererOptions)
3535
}
3636
}
3737

3838
/**
3939
* Parse markdown and render it to an HTML string.
4040
*
41-
* @param markdown - The markdown/Comark content to render
41+
* @param markdown - The markdown content to parse and render
4242
* @param options - Optional parse and render options
4343
* @returns A Promise resolving to the HTML string
4444
*
4545
* @example
4646
* ```typescript
47-
* import { render } from '@comark/html'
47+
* import { renderHtml } from '@comark/html'
4848
*
49-
* const html = await render('# Hello\n\nThis is **bold** and _italic_.')
49+
* const html = await renderHtml('# Hello\n\nThis is **bold** and _italic_.')
5050
* document.body.innerHTML = html
5151
* ```
5252
*/
53-
export async function render(markdown: string, options?: RendererOptions): Promise<string> {
54-
return createRender(options)(markdown)
53+
export async function renderHtml(markdown: string, options?: ParserOptions & RendererOptions): Promise<string> {
54+
return createHtmlRenderer(options)(markdown)
5555
}

packages/comark-html/src/plugins/binding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const escape = (s: string) =>
1717
* @example
1818
* ```ts
1919
* import binding, { Binding } from '@comark/html/plugins/binding'
20-
* import { createRender } from '@comark/html'
20+
* import { createHtmlRenderer } from '@comark/html'
2121
*
22-
* const render = createRender({
22+
* const renderHtml = createHtmlRenderer({
2323
* plugins: [binding()],
2424
* components: { Binding },
2525
* })

packages/comark-html/src/plugins/math.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export { default } from 'comark/plugins/math'
1212
* @example
1313
* ```typescript
1414
* import math, { Math } from '@comark/html/plugins/math'
15-
* import { createRender } from '@comark/html'
15+
* import { createHtmlRenderer } from '@comark/html'
1616
*
17-
* const render = createRender({
17+
* const renderHtml = createHtmlRenderer({
1818
* plugins: [math()],
1919
* components: { Math },
2020
* })

packages/comark-html/src/plugins/mermaid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export { default } from 'comark/plugins/mermaid'
1212
* @example
1313
* ```typescript
1414
* import mermaid, { Mermaid } from '@comark/html/plugins/mermaid'
15-
* import { createRender } from '@comark/html'
15+
* import { createHtmlRenderer } from '@comark/html'
1616
*
17-
* const render = createRender({
17+
* const renderHtml = createHtmlRenderer({
1818
* plugins: [mermaid()],
1919
* components: { Mermaid },
2020
* })

packages/comark-html/src/render.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ export interface RenderHTMLContext {
1414
export type ComponentRenderFn = (element: ElementNode, ctx: RenderHTMLContext) => string | Promise<string>
1515

1616
/**
17-
* Render Comark tree to HTML
17+
* Render a Markdown document to HTML.
1818
*
19-
* @param tree - The Comark tree to render
19+
* @param document - The parsed Markdown document to render
2020
* @param options - Optional rendering options with custom components and data
2121
* @returns The HTML string
2222
*
2323
* @example
2424
* ```typescript
2525
* import { parseMarkdown } from 'comark'
26-
* import { renderHtml } from '@comark/html'
26+
* import { renderHtmlFromDocument } from '@comark/html'
2727
*
28-
* const tree = await parseMarkdown('::alert{type="info"}\nHello!\n::')
28+
* const document = await parseMarkdown('::alert{type="info"}\nHello!\n::')
2929
*
30-
* const html = renderHtml(tree, {
30+
* const html = await renderHtmlFromDocument(document, {
3131
* components: {
32-
* alert: ([tag, attrs, ...children], { render }) => {
33-
* return `<div class="alert alert-${attrs.type}">${render(children)}</div>`
32+
* alert: async ([tag, attrs, ...children], { render }) => {
33+
* return `<div class="alert alert-${attrs.type}">${await render(children)}</div>`
3434
* }
3535
* }
3636
* })
3737
* ```
3838
*/
39-
export async function renderHtml(
40-
tree: MarkdownDocument | { nodes: MarkdownDocument['nodes'] },
39+
export async function renderHtmlFromDocument(
40+
document: MarkdownDocument | { nodes: MarkdownDocument['nodes'] },
4141
options?: RendererOptions
4242
): Promise<string> {
43-
return (await render(tree, { blockSeparator: '\n', format: 'text/html', ...options })).trim()
43+
return (await render(document, { blockSeparator: '\n', format: 'text/html', ...options })).trim()
4444
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, it, expect } from 'vitest'
22
import { parseMarkdown } from 'comark'
33
import githubAlert from '@comark/html/plugins/alert'
4-
import { renderHtml } from '../src/index'
4+
import { renderHtmlFromDocument } from '../src/index'
55

66
describe('githubAlert', () => {
77
it('should convert !TIP to <svg> icon', async () => {
8-
const tree = await parseMarkdown(
8+
const doc = await parseMarkdown(
99
`
1010
> [!NOTE]
1111
> Useful information that users should know, even when skimming content.
@@ -15,7 +15,7 @@ describe('githubAlert', () => {
1515
plugins: [githubAlert()],
1616
}
1717
)
18-
const html = await renderHtml(tree)
18+
const html = await renderHtmlFromDocument(doc)
1919
expect(html).toContain('<blockquote as="note">')
2020
})
2121
})

0 commit comments

Comments
 (0)