Skip to content

Commit

Permalink
Replace rehype with rehype-dom in the browser (#242)
Browse files Browse the repository at this point in the history
Use `rehype-dom` 5.0 (36k) as the Rehype parser in browsers, where Rehype can use DOM APIs to parse HTML.

In Node, require in the full `rehype` processor, which is ~240k.

This should give smaller, faster bundles for Markdown parsing in browsers.
  • Loading branch information
eatyourgreens authored Jan 17, 2024
1 parent 9dbe5ac commit cb53b31
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
101 changes: 101 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 @@ -75,6 +75,7 @@
"markdown-it-video": "~0.6.3",
"mime": "~3.0.0",
"rehype": "~11.0.0",
"rehype-dom": "~5.0.0",
"rehype-react": "~6.2.1"
}
}
9 changes: 7 additions & 2 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import twemoji from '@twemoji/api';
import { sanitize } from 'isomorphic-dompurify';

import { Fragment, createElement } from 'react';
import rehype from 'rehype';
import rehypeDom from 'rehype-dom';
import rehype2react from 'rehype-react';

import html5Embed from './html5-embed';
Expand All @@ -21,6 +21,11 @@ import relNofollow from './links-rel-nofollow';
import replaceSymbols from './default-transformer';

let counter = 0;
let rehypeParser = rehypeDom;

if (typeof window === 'undefined') {
rehypeParser = require('rehype');
}

export function emojify(input) {
return twemoji.parse(input);
Expand Down Expand Up @@ -107,7 +112,7 @@ export function getComponentTree({ html, settings, components }) {

let parsedHTML = null;
try {
parsedHTML = rehype()
parsedHTML = rehypeParser()
.data('settings', rehypeSettings)
.use(rehype2react, {
Fragment,
Expand Down

0 comments on commit cb53b31

Please sign in to comment.