Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add html to hast #901

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion __tests__/lib/hast.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hast } from '../../lib';
import { hast, hastFromHtml } from '../../lib';
import { h } from 'hastscript';

describe('hast transformer', () => {
Expand All @@ -22,3 +22,17 @@ describe('hast transformer', () => {
expect(hast(md, { components })).toStrictEqualExceptPosition(expected);
});
});

describe('hastFromHtml', () => {
it('parses html', () => {
const html = '<div><span>Nice</span></div>';
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');
});
});
5 changes: 2 additions & 3 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
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');
Expand All @@ -22,7 +20,7 @@
};

export const reactProcessor = (opts = {}) => {
return createProcessor({ remarkPlugins, ...opts });

Check failure on line 23 in index.tsx

View workflow job for this annotation

GitHub Actions / Bundle Watch

No value exists in scope for the shorthand property 'remarkPlugins'. Either declare one or provide an initializer.
};

export const html = (text: string, opts = {}) => {
Expand All @@ -33,4 +31,5 @@
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 };
6 changes: 6 additions & 0 deletions lib/hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
Expand All @@ -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;
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading