Skip to content

Commit

Permalink
perf(rehype): stop using hast-util-select and simplify the implementa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
craftzdog committed Jul 25, 2023
1 parent 30e2de4 commit e344eb3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 179 deletions.
86 changes: 0 additions & 86 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"license": "MIT",
"homepage": "https://github.com/inkdropapp/html2markdown",
"dependencies": {
"hast-util-select": "^5.0.5",
"mdast-util-gfm": "^3.0.0",
"rehype-parse": "^8.0.4",
"rehype-remark": "^9.1.2",
Expand Down
30 changes: 0 additions & 30 deletions src/hast-util-insert.ts

This file was deleted.

65 changes: 27 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { comment as toMdastComment } from './to-mdast-comment'
import { unified } from 'unified'
import rehypeParse from 'rehype-parse'
import rehype2remark from 'rehype-remark'
import { rehypeInsert } from './rehype-insert'
import { rehypeInsertBaseURI } from './rehype-insert-base-uri'
import stringify, { Options as RemarkStringifyOptions } from 'remark-stringify'
import { squeezeLinks } from 'remark-squeeze-links'
import { gfmToMarkdown } from 'mdast-util-gfm'
Expand All @@ -24,45 +24,34 @@ function getConverter(opts?: Options) {
baseURI = null
} = opts || {}

const remark = unified()
let remark = unified()
.data('toMarkdownExtensions', [gfmToMarkdown()])
.use(rehypeParse)
.use(rehypeInsert, {
insertions: baseURI
? [
{
selector: 'head',
insert: {
type: 'element',
tagName: 'base',
properties: {
href: baseURI
},
children: []
}
}
]
: []
})
.use(rehype2remark, {
handlers: {
pre: toMdastCodeBlock,
comment: toMdastComment,
...(toMdastOptions.handlers || {})
},
...toMdastOptions
})
// @ts-ignore
return remark.use(squeezeLinks).use(stringify, {
listItemIndent: 'one',
bullet: '*',
fences: true,
handlers: {
break: hardBreak,
...(stringifyOptions.handlers || {})
},
...stringifyOptions
})
if (baseURI) remark = remark.use(rehypeInsertBaseURI, baseURI || '/')

return (
remark
.use(rehype2remark, {
handlers: {
pre: toMdastCodeBlock,
comment: toMdastComment,
...(toMdastOptions.handlers || {})
},
...toMdastOptions
})
.use(squeezeLinks)
// @ts-ignore
.use(stringify, {
listItemIndent: 'one',
bullet: '*',
fences: true,
handlers: {
break: hardBreak,
...(stringifyOptions.handlers || {})
},
...stringifyOptions
})
)
}

export function html2Markdown(html: string, opts?: Options): string {
Expand Down
21 changes: 21 additions & 0 deletions src/rehype-insert-base-uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Plugin } from 'unified'
import type { Root } from 'hast'
import { visit } from 'unist-util-visit'

export const rehypeInsertBaseURI: Plugin<[string], Root, Root> = baseURI => {
return function rehypeInsertTransformer(tree) {
visit(tree, { type: 'element', tagName: 'head' }, headNode => {
headNode.children = [
...headNode.children,
{
type: 'element',
tagName: 'base',
properties: {
href: baseURI
},
children: []
}
]
})
}
}
24 changes: 0 additions & 24 deletions src/rehype-insert.ts

This file was deleted.

0 comments on commit e344eb3

Please sign in to comment.