Skip to content

Commit 8474183

Browse files
authored
feat: support remarkPlugins & rehypePlugins (#160)
1 parent 54da410 commit 8474183

File tree

6 files changed

+659
-31
lines changed

6 files changed

+659
-31
lines changed

packages/playground/use-theme-doc/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"@types/react": "^18.2.13",
2525
"@vitejs/plugin-react": "^4.0.1",
2626
"@vitejs/plugin-react-swc": "^3.3.2",
27+
"rehype-mathjax": "^4.0.3",
28+
"remark-emoji": "^4.0.0",
29+
"remark-math": "^5.1.1",
2730
"rimraf": "^4.4.1",
2831
"serve": "^14.2.0",
2932
"vite": "^4.4.8",

packages/playground/use-theme-doc/pages/md-test1$.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import Counter from './Counter';
88

99
<Counter />
1010

11+
# test mdx plugin
12+
13+
remark-emoji: :dog: :+1:
14+
15+
remark-math:
16+
17+
Lift($$L$$) can be determined by Lift Coefficient ($$C_L$$) like the following
18+
equation.
19+
20+
$$
21+
L = \frac{1}{2} \rho v^2 S C_L
22+
$$
23+
1124
# Heading one
1225

1326
Sint sit cillum pariatur eiusmod nulla pariatur ipsum. Sit laborum anim qui mollit tempor pariatur nisi minim dolor. Aliquip et adipisicing sit sit fugiat commodo id sunt. Nostrud enim ad commodo incididunt cupidatat in ullamco ullamco Lorem cupidatat velit enim et Lorem. Ut laborum cillum laboris fugiat culpa sint irure do reprehenderit culpa occaecat. Exercitation esse mollit tempor magna aliqua in occaecat aliquip veniam reprehenderit nisi dolor in laboris dolore velit.

packages/playground/use-theme-doc/vite.config.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from 'vite'
22
import * as path from 'path'
33
// import react from '@vitejs/plugin-react'
4-
import react from "@vitejs/plugin-react-swc";
4+
import react from '@vitejs/plugin-react-swc'
55

66
import pages from 'vite-plugin-react-pages'
77

@@ -16,6 +16,29 @@ export default defineConfig({
1616
pages({
1717
pagesDir: path.join(__dirname, 'pages'),
1818
// useHashRouter: true
19+
modifyRemarkPlugins(original) {
20+
return [
21+
...original,
22+
{
23+
name: 'remark-emoji',
24+
createPlugin: () => import('remark-emoji').then((m) => m.default),
25+
},
26+
{
27+
name: 'remark-math',
28+
createPlugin: () => import('remark-math').then((m) => m.default),
29+
},
30+
]
31+
},
32+
modifyRehypePlugins(original) {
33+
return [
34+
...original,
35+
{
36+
name: 'rehype-mathjax',
37+
createPlugin: () =>
38+
import('rehype-mathjax').then((m) => m.default as any),
39+
},
40+
]
41+
},
1942
}),
2043
],
2144
// theme local dev

packages/react-pages/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-react-pages",
3-
"version": "4.1.4",
3+
"version": "4.1.5-alpha.0",
44
"type": "module",
55
"types": "./dist/node-types/index.d.ts",
66
"exports": {

packages/react-pages/src/node/index.ts

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path'
2-
import type { PluggableList } from 'unified'
2+
import type { PluggableList, Pluggable } from 'unified'
33
import type {
44
Plugin,
55
IndexHtmlTransformContext,
@@ -58,8 +58,22 @@ export interface PluginConfig {
5858
pageStrategy?: PageStrategy
5959
useHashRouter?: boolean
6060
staticSiteGeneration?: staticSiteGenerationConfig
61+
/** user can add/remove remark plugins passed to mdx */
62+
modifyRemarkPlugins?: ModifyNamedUnifiedPlugins
63+
/** user can add/remove rehype plugins passed to mdx */
64+
modifyRehypePlugins?: ModifyNamedUnifiedPlugins
6165
}
6266

67+
export type NamedUnifiedPlugin = {
68+
/** use name so that modifier can recognize a plugin */
69+
name: string
70+
createPlugin: () => Pluggable | Promise<Pluggable>
71+
}
72+
73+
export type ModifyNamedUnifiedPlugins = (
74+
original: NamedUnifiedPlugin[]
75+
) => NamedUnifiedPlugin[]
76+
6377
function pluginFactory(opts: PluginConfig = {}): Plugin {
6478
const { useHashRouter = false, staticSiteGeneration } = opts
6579

@@ -277,8 +291,8 @@ export default async function setupPlugins(
277291
// use dynamic import so that it supports node commonjs
278292
const mdx = await import('@mdx-js/rollup')
279293
const mdxPlugin = mdx.default({
280-
remarkPlugins: await getRemarkPlugins(),
281-
rehypePlugins: await getRehypePlugins(),
294+
remarkPlugins: await getRemarkPlugins(vpConfig.modifyRemarkPlugins),
295+
rehypePlugins: await getRehypePlugins(vpConfig.modifyRehypePlugins),
282296
// treat .md as mdx
283297
mdExtensions: [],
284298
mdxExtensions: ['.md', '.mdx'],
@@ -294,25 +308,68 @@ export default async function setupPlugins(
294308
]
295309
}
296310

297-
function getRemarkPlugins(): Promise<PluggableList> {
298-
return Promise.all([
299-
// use dynamic import so that it works in node commonjs
300-
import('remark-frontmatter').then((m) => m.default),
301-
import('remark-gfm').then((m) => m.default),
302-
import('remark-mdx-images').then((m) => m.default),
311+
function getRemarkPlugins(
312+
modifyPlugins?: ModifyNamedUnifiedPlugins
313+
): Promise<PluggableList> {
314+
const originalPlugins: NamedUnifiedPlugin[] = [
315+
{
316+
name: 'remark-frontmatter',
317+
// use dynamic import so that it works in node commonjs
318+
// use lazy-eval function so that we don't import/create a plugin until we actually need it
319+
// (it may be removed by modifyPlugins so we can avoid calling it)
320+
createPlugin: () => import('remark-frontmatter').then((m) => m.default),
321+
},
322+
{
323+
name: 'remark-gfm',
324+
createPlugin: () => import('remark-gfm').then((m) => m.default),
325+
},
326+
{
327+
name: 'remark-mdx-images',
328+
createPlugin: () => import('remark-mdx-images').then((m) => m.default),
329+
},
330+
{
331+
name: 'DemoMdxPlugin',
332+
createPlugin: () => DemoMdxPlugin,
333+
},
334+
{
335+
name: 'TsInfoMdxPlugin',
336+
createPlugin: () => TsInfoMdxPlugin,
337+
},
338+
{
339+
name: 'FileTextMdxPlugin',
340+
createPlugin: () => FileTextMdxPlugin,
341+
},
342+
]
343+
return createFinalPlugins(originalPlugins, modifyPlugins)
344+
}
303345

304-
// plugins created for vite-pages:
305-
DemoMdxPlugin,
306-
TsInfoMdxPlugin,
307-
FileTextMdxPlugin,
308-
])
346+
function getRehypePlugins(
347+
modifyPlugins?: ModifyNamedUnifiedPlugins
348+
): Promise<PluggableList> {
349+
const originalPlugins: NamedUnifiedPlugin[] = [
350+
{
351+
name: 'rehype-slug',
352+
// use dynamic import so that it works in node commonjs
353+
// use lazy-eval function so that we don't import/create a plugin until we actually need it
354+
// (it may be removed by modifyPlugins so we can avoid calling it)
355+
createPlugin: () => import('rehype-slug').then((m) => m.default),
356+
},
357+
]
358+
return createFinalPlugins(originalPlugins, modifyPlugins)
309359
}
310360

311-
function getRehypePlugins(): Promise<PluggableList> {
312-
return Promise.all([
313-
// use dynamic import so that it works in node commonjs
314-
import('rehype-slug').then((m) => m.default),
315-
])
361+
function createFinalPlugins(
362+
originalPlugins: NamedUnifiedPlugin[],
363+
modifyPlugins: ModifyNamedUnifiedPlugins | undefined
364+
) {
365+
const finalPlugins = (() => {
366+
if (typeof modifyPlugins === 'function') {
367+
const res = modifyPlugins(originalPlugins)
368+
if (Array.isArray(res)) return res
369+
}
370+
return originalPlugins
371+
})()
372+
return Promise.all(finalPlugins.map(({ createPlugin }) => createPlugin()))
316373
}
317374

318375
/**

0 commit comments

Comments
 (0)