forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-to-markdown.ts
More file actions
21 lines (19 loc) · 777 Bytes
/
html-to-markdown.ts
File metadata and controls
21 lines (19 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { parseFragment } from 'parse5'
import { fromParse5 } from 'hast-util-from-parse5'
import { toMdast } from 'hast-util-to-mdast'
import { toMarkdown as mdastToMarkdown } from 'mdast-util-to-markdown'
import { gfmTableToMarkdown } from 'mdast-util-gfm-table'
export interface HtmlToMarkdownOptions {
/** Whether to pad table columns to equal width (default: `true`). */
tablePipeAlign?: boolean
}
/**
* Convert an HTML string to Markdown
*/
export function htmlToMarkdown(html: string, options: HtmlToMarkdownOptions = {}): string {
const { tablePipeAlign = true } = options
const dom = parseFragment(html)
const hast = fromParse5(dom)
const mdast = toMdast(hast)
return mdastToMarkdown(mdast, { extensions: [gfmTableToMarkdown({ tablePipeAlign })] })
}