From fdfcc6ae06561d267d8231e75f62791e244cfd6f Mon Sep 17 00:00:00 2001 From: Kelly Joseph Price Date: Fri, 7 Jun 2024 15:26:46 -0700 Subject: [PATCH] feat: add html to hast --- __tests__/lib/hast.test.ts | 16 +++++++++++++++- index.tsx | 5 ++--- lib/hast.ts | 6 ++++++ lib/index.ts | 4 ++-- package-lock.json | 15 +++++++++++++++ package.json | 1 + 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/__tests__/lib/hast.test.ts b/__tests__/lib/hast.test.ts index 35fb91198..f0013535b 100644 --- a/__tests__/lib/hast.test.ts +++ b/__tests__/lib/hast.test.ts @@ -1,4 +1,4 @@ -import { hast } from '../../lib'; +import { hast, hastFromHtml } from '../../lib'; import { h } from 'hastscript'; describe('hast transformer', () => { @@ -22,3 +22,17 @@ describe('hast transformer', () => { expect(hast(md, { components })).toStrictEqualExceptPosition(expected); }); }); + +describe('hastFromHtml', () => { + it('parses html', () => { + const html = '
Nice
'; + const tree = hastFromHtml(html); + + // @ts-ignore + expect(tree.children[0].tagName).toBe('html'); + // @ts-ignore + expect(tree.children[0].children[1].children[0].tagName).toBe('div'); + // @ts-ignore + expect(tree.children[0].children[1].children[0].children[0].tagName).toBe('span'); + }); +}); diff --git a/index.tsx b/index.tsx index e0b42152a..18ae9313c 100644 --- a/index.tsx +++ b/index.tsx @@ -6,8 +6,6 @@ import * as Components from './components'; import { getHref } from './components/Anchor'; import { options } from './options'; -import { compile, hast, run, mdast, mdx, plain, remarkPlugins } from './lib'; - import './styles/main.scss'; const unimplemented = debug('mdx:unimplemented'); @@ -33,4 +31,5 @@ export const esast = (text: string, opts = {}) => { unimplemented('esast export'); }; -export { compile, hast, run, mdast, mdx, plain, Components, utils }; +export { compile, hast, hastFromHtml, run, mdast, mdx, plain, remarkPlugins } from './lib'; +export { Components, utils }; diff --git a/lib/hast.ts b/lib/hast.ts index bc13fd7bb..d7d4ce817 100644 --- a/lib/hast.ts +++ b/lib/hast.ts @@ -3,6 +3,8 @@ import remarkRehype from 'remark-rehype'; import { injectComponents } from '../processor/transform'; import { MdastComponents } from '../types'; import mdast from './mdast'; +import { unified } from 'unified'; +import rehypeParse from 'rehype-parse'; interface Options { components?: Record; @@ -19,4 +21,8 @@ const hast = (text: string, opts: Options = {}) => { return processor.runSync(processor.parse(text)); }; +export const hastFromHtml = (html: string) => { + return unified().use(rehypeParse).parse(html); +}; + export default hast; diff --git a/lib/index.ts b/lib/index.ts index 4f9876c69..248c41474 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,10 +1,10 @@ import astProcessor, { MdastOpts, remarkPlugins } from './ast-processor'; import compile from './compile'; -import hast from './hast'; +import hast, { hastFromHtml } from './hast'; import mdast from './mdast'; import mdx from './mdx'; import plain from './plain'; import run from './run'; export type { MdastOpts }; -export { astProcessor, compile, hast, mdast, mdx, plain, run, remarkPlugins }; +export { astProcessor, compile, hast, hastFromHtml, mdast, mdx, plain, run, remarkPlugins }; diff --git a/package-lock.json b/package-lock.json index 6cbdfcb00..dea40196a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "path-browserify": "^1.0.1", "process": "^0.11.10", "prop-types": "^15.8.1", + "rehype-parse": "^9.0.0", "rehype-remark": "^10.0.0", "rehype-slug": "^6.0.0", "remark": "^15.0.1", @@ -26974,6 +26975,20 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/rehype-parse": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/rehype-parse/-/rehype-parse-9.0.0.tgz", + "integrity": "sha512-WG7nfvmWWkCR++KEkZevZb/uw41E8TsH4DsY9UxsTbIXCVGbAs4S+r8FrQ+OtH5EEQAs+5UxKC42VinkmpA1Yw==", + "dependencies": { + "@types/hast": "^3.0.0", + "hast-util-from-html": "^2.0.0", + "unified": "^11.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/rehype-raw": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/rehype-raw/-/rehype-raw-5.1.0.tgz", diff --git a/package.json b/package.json index e764682d7..3c1581dcf 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "path-browserify": "^1.0.1", "process": "^0.11.10", "prop-types": "^15.8.1", + "rehype-parse": "^9.0.0", "rehype-remark": "^10.0.0", "rehype-slug": "^6.0.0", "remark": "^15.0.1",