Skip to content

Commit a661cb0

Browse files
committed
feat: add new htmlToMarkdown function
1 parent a4c2c93 commit a661cb0

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

app/utils/html-to-markdown.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { parseFragment } from 'parse5'
2+
import { fromParse5 } from 'hast-util-from-parse5'
3+
import { toMdast } from 'hast-util-to-mdast'
4+
import { toMarkdown as mdastToMarkdown } from 'mdast-util-to-markdown'
5+
import { gfmTableToMarkdown } from 'mdast-util-gfm-table'
6+
7+
export interface HtmlToMarkdownOptions {
8+
/** Whether to pad table columns to equal width (default: `true`). */
9+
tablePipeAlign?: boolean
10+
}
11+
12+
/**
13+
* Convert an HTML string to Markdown
14+
*/
15+
export function htmlToMarkdown(html: string, options: HtmlToMarkdownOptions = {}): string {
16+
const { tablePipeAlign = true } = options
17+
const dom = parseFragment(html)
18+
const hast = fromParse5(dom)
19+
const mdast = toMdast(hast)
20+
return mdastToMarkdown(mdast, { extensions: [gfmTableToMarkdown({ tablePipeAlign })] })
21+
}

0 commit comments

Comments
 (0)