|
| 1 | +--- |
| 2 | +authorGithub: wooorm |
| 3 | +authorTwitter: wooorm |
| 4 | +author: Titus Wormer |
| 5 | +description: How to use unified in the browser |
| 6 | +group: guide |
| 7 | +modified: 2024-08-09 |
| 8 | +published: 2024-08-09 |
| 9 | +tags: |
| 10 | + - browser |
| 11 | + - dom |
| 12 | + - hast |
| 13 | + - mdast |
| 14 | + - nlcst |
| 15 | + - rehype |
| 16 | + - remark |
| 17 | + - retext |
| 18 | + - unified |
| 19 | + - web |
| 20 | +title: unified in the browser |
| 21 | +--- |
| 22 | + |
| 23 | +## unified in the browser |
| 24 | + |
| 25 | +unified is many different projects that are maintained on GitHub |
| 26 | +and distributed mainly through npm. |
| 27 | +Almost all the projects can be used anywhere: in Bun, Deno, Node.js, |
| 28 | +on the edge, or in browsers. |
| 29 | +To use our projects in a browser, |
| 30 | +you need to do one or two things. |
| 31 | +And there’s different ways to go about it. |
| 32 | + |
| 33 | +### Contents |
| 34 | + |
| 35 | +* [Bundle](#bundle) |
| 36 | +* [CDN](#cdn) |
| 37 | + |
| 38 | +### Bundle |
| 39 | + |
| 40 | +A common way to use unified in the browser is to bundle it with a bundler. |
| 41 | +You perhaps know bundlers already: webpack, Rollup, or esbuild. |
| 42 | +You might be using one. |
| 43 | +Or otherwise have a favorite. |
| 44 | +If not, |
| 45 | +[esbuild][] is a good choice. |
| 46 | + |
| 47 | +Bundling is almost always a good idea. |
| 48 | +It gives end users a single file to download. |
| 49 | +Often minified. |
| 50 | + |
| 51 | +Say we have some code using unified in `example.js`: |
| 52 | + |
| 53 | +```js twoslash |
| 54 | +/// <reference lib="dom" /> |
| 55 | +// ---cut--- |
| 56 | +import rehypeStringify from 'rehype-stringify' |
| 57 | +import remarkParse from 'remark-parse' |
| 58 | +import remarkRehype from 'remark-rehype' |
| 59 | +import {unified} from 'unified' |
| 60 | + |
| 61 | +const file = await unified() |
| 62 | + .use(remarkParse) |
| 63 | + .use(remarkRehype) |
| 64 | + .use(rehypeStringify) |
| 65 | + .process('Hello, *world*!') |
| 66 | + |
| 67 | +console.log(String(file)) |
| 68 | +``` |
| 69 | + |
| 70 | +And you want to use that in some HTML called `index.html`: |
| 71 | + |
| 72 | +```html |
| 73 | +<!doctype html> |
| 74 | +<meta charset=utf8> |
| 75 | +<title>Example</title> |
| 76 | +<script src=example.min.js type=module></script> |
| 77 | +``` |
| 78 | + |
| 79 | +To make `example.js` work in the browser, |
| 80 | +you can bundle it with esbuild. |
| 81 | +First, set up a package: |
| 82 | + |
| 83 | +```sh |
| 84 | +npm init --yes |
| 85 | +npm install esbuild --save-dev rehype-stringify remark-parse remark-rehype unified |
| 86 | +``` |
| 87 | + |
| 88 | +Then, bundle `example.js`: |
| 89 | + |
| 90 | +```sh |
| 91 | +npx esbuild --bundle --format=esm --minify --outfile=example.min.js example.js |
| 92 | +``` |
| 93 | + |
| 94 | +Now, open `index.html` in a browser. |
| 95 | +When you open the console of your developer tools, |
| 96 | +you should see `Hello, <em>world</em>!` |
| 97 | + |
| 98 | +That’s it! |
| 99 | + |
| 100 | +### CDN |
| 101 | + |
| 102 | +If you don’t want to bundle unified yourself, |
| 103 | +you can use a CDN. |
| 104 | + |
| 105 | +A CDN hosts files for you. |
| 106 | +And they can process them for you as well. |
| 107 | +The nice thing is that you do not have to install and bundle things yourself. |
| 108 | +The downside is that you’re dependent on a particular server that you do not |
| 109 | +control. |
| 110 | + |
| 111 | +One such CDN is [esm.sh][esmsh]. |
| 112 | +Like the code above, |
| 113 | +you can use it in a browser like this: |
| 114 | + |
| 115 | +```html |
| 116 | +<!doctype html> |
| 117 | +<meta charset=utf8> |
| 118 | +<title>Example</title> |
| 119 | +<script type=module> |
| 120 | + import rehypeStringify from 'https://esm.sh/rehype-stringify@10?bundle' |
| 121 | + import remarkParse from 'https://esm.sh/remark-parse@11?bundle' |
| 122 | + import remarkRehype from 'https://esm.sh/remark-rehype@11?bundle' |
| 123 | + import {unified} from 'https://esm.sh/unified@11?bundle' |
| 124 | +
|
| 125 | + const file = await unified() |
| 126 | + .use(remarkParse) |
| 127 | + .use(remarkRehype) |
| 128 | + .use(rehypeStringify) |
| 129 | + .process('Hello, *world*!') |
| 130 | +
|
| 131 | + console.log(String(file)) |
| 132 | +</script> |
| 133 | +``` |
| 134 | + |
| 135 | +[esbuild]: https://esbuild.github.io/ |
| 136 | + |
| 137 | +[esmsh]: https://esm.sh/ |
0 commit comments