Skip to content

Commit c28ed54

Browse files
committed
Add Using unified in a browser guide
1 parent f65a976 commit c28ed54

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed

.remarkrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {unified} from 'unified'
2424
const naturalLanguage = unified().use([
2525
retextEnglish,
2626
retextPresetWooorm,
27-
[retextEquality, {ignore: ['whitespace']}],
27+
[retextEquality, {ignore: ['hosts', 'whitespace']}],
2828
[retextPassive, {ignore: ['hidden']}],
2929
[retextProfanities, {sureness: 1}],
3030
[retextReadability, {age: 18, minWords: 8}],

asset/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const id = 'search-root'
7979

8080
// For some reason this can be fired multiple times.
8181
if (loc.pathname === home && !document.querySelector('#' + id + ' form')) {
82-
await init()
82+
init()
8383
}
8484

8585
async function init() {

dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
CLI
33
XSS
44
attacher
5+
bundlers
56
bundler
67
esbuild
78
minified
@@ -36,5 +37,6 @@ rehype
3637
retext
3738
unist
3839
vfile
40+
webpack
3941
xast
4042
xo

doc/learn/unified-in-the-browser.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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/

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
"complexity": "off",
190190
"no-await-in-loop": "off",
191191
"unicorn/no-process-exit": "off",
192+
"unicorn/prefer-top-level-await": "off",
192193
"unicorn/prevent-abbreviations": [
193194
"error",
194195
{

0 commit comments

Comments
 (0)