Skip to content

Commit b0a2d32

Browse files
committed
refactor: extract our two custom remark into their own files
1 parent 588f446 commit b0a2d32

File tree

5 files changed

+91
-90
lines changed

5 files changed

+91
-90
lines changed

packages/gatsby-plugin-mdx/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"mdast-util-mdx": "^2.0.0",
5959
"mdast-util-to-markdown": "^1.3.0",
6060
"unified": "^10.1.2",
61-
"unist-util-map": "^3.1.1",
6261
"unist-util-visit": "^4.1.0",
6362
"vfile": "^5.3.2"
6463
},

packages/gatsby-plugin-mdx/src/get-source-plugins-as-remark-plugins.ts

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { GatsbyCache, Reporter, NodePluginArgs } from "gatsby"
33
import type { Pluggable } from "unified"
44

55
import { IMdxPluginOptions } from "./plugin-options"
6+
import { remarkMdxHtmlPlugin } from "./remark-mdx-html-plugin"
7+
import { pathPlugin } from "./remark-path-prefix-plugin"
68

79
interface IGetSourcePluginsAsRemarkPlugins {
810
gatsbyRemarkPlugins: IMdxPluginOptions["gatsbyRemarkPlugins"]
@@ -13,10 +15,6 @@ interface IGetSourcePluginsAsRemarkPlugins {
1315
cache: GatsbyCache
1416
}
1517

16-
// ensure only one `/` in new url
17-
const withPathPrefix = (url: string, pathPrefix: string): string =>
18-
(pathPrefix + url).replace(/\/\//, `/`)
19-
2018
export async function getSourcePluginsAsRemarkPlugins({
2119
gatsbyRemarkPlugins,
2220
getNode,
@@ -27,35 +25,12 @@ export async function getSourcePluginsAsRemarkPlugins({
2725
}: IGetSourcePluginsAsRemarkPlugins): Promise<
2826
ProcessorOptions["remarkPlugins"]
2927
> {
30-
const { visit } = await import(`unist-util-visit`)
31-
const { map } = await import(`unist-util-map`)
32-
33-
// Ensure relative links include `pathPrefix`
34-
const pathPlugin = () =>
35-
async function transformer(markdownAST): Promise<any> {
36-
if (!pathPrefix) {
37-
return markdownAST
38-
}
39-
visit(markdownAST, [`link`, `definition`], node => {
40-
if (
41-
node.url &&
42-
typeof node.url === `string` &&
43-
node.url.startsWith(`/`) &&
44-
!node.url.startsWith(`//`)
45-
) {
46-
// TODO: where does withPathPrefix
47-
node.url = withPathPrefix(node.url, pathPrefix)
48-
}
49-
})
50-
return markdownAST
51-
}
52-
5328
const userPluginsFiltered = gatsbyRemarkPlugins.filter(
5429
plugin => typeof plugin.module === `function`
5530
)
5631

5732
if (!userPluginsFiltered.length) {
58-
return pathPrefix ? [pathPlugin] : []
33+
return pathPrefix ? [[pathPlugin, { pathPrefix }]] : []
5934
}
6035

6136
const userPlugins = userPluginsFiltered.map(plugin => {
@@ -81,67 +56,15 @@ export async function getSourcePluginsAsRemarkPlugins({
8156
},
8257
plugin.options || {}
8358
)
84-
85-
const divAST = map(markdownAST, node => {
86-
if (node.type !== `html`) {
87-
return node
88-
}
89-
return {
90-
type: `mdxJsxFlowElement`,
91-
name: `div`,
92-
attributes: [
93-
{
94-
type: `mdxJsxAttribute`,
95-
name: `dangerouslySetInnerHTML`,
96-
value: {
97-
type: `mdxJsxAttributeValueExpression`,
98-
data: {
99-
estree: {
100-
type: `Program`,
101-
body: [
102-
{
103-
type: `ExpressionStatement`,
104-
expression: {
105-
type: `ObjectExpression`,
106-
properties: [
107-
{
108-
type: `Property`,
109-
method: false,
110-
shorthand: false,
111-
computed: false,
112-
key: {
113-
type: `Identifier`,
114-
name: `__html`,
115-
},
116-
value: {
117-
type: `Literal`,
118-
value: node.value,
119-
},
120-
kind: `init`,
121-
},
122-
],
123-
},
124-
},
125-
],
126-
sourceType: `module`,
127-
},
128-
},
129-
},
130-
},
131-
],
132-
}
133-
})
134-
135-
return divAST
13659
}
13760
}
13861

13962
return wrappedGatsbyPlugin
14063
})
14164

14265
if (pathPrefix) {
143-
return [pathPlugin, ...userPlugins]
66+
return [[pathPlugin, { pathPrefix }], ...userPlugins, remarkMdxHtmlPlugin]
14467
} else {
145-
return userPlugins
68+
return [...userPlugins, remarkMdxHtmlPlugin]
14669
}
14770
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { Node } from "unist-util-visit"
2+
3+
// This plugin replaces html nodes with JSX divs that render given HTML via dangerouslySetInnerHTML
4+
// We have to find out if this is really a good idea, but its processing footprint is very low
5+
// compared to other solutions that would traverse the given HTML.
6+
export const remarkMdxHtmlPlugin = () =>
7+
async function transformer(markdownAST: Node): Promise<Node> {
8+
const { visit } = await import(`unist-util-visit`)
9+
10+
visit(markdownAST, node => {
11+
if (node.type !== `html`) {
12+
return
13+
}
14+
node.type = `mdxJsxFlowElement`
15+
node.name = `div`
16+
node.attributes = [
17+
{
18+
type: `mdxJsxAttribute`,
19+
name: `dangerouslySetInnerHTML`,
20+
value: {
21+
type: `mdxJsxAttributeValueExpression`,
22+
data: {
23+
estree: {
24+
type: `Program`,
25+
body: [
26+
{
27+
type: `ExpressionStatement`,
28+
expression: {
29+
type: `ObjectExpression`,
30+
properties: [
31+
{
32+
type: `Property`,
33+
method: false,
34+
shorthand: false,
35+
computed: false,
36+
key: {
37+
type: `Identifier`,
38+
name: `__html`,
39+
},
40+
value: {
41+
type: `Literal`,
42+
value: node.value,
43+
},
44+
kind: `init`,
45+
},
46+
],
47+
},
48+
},
49+
],
50+
sourceType: `module`,
51+
},
52+
},
53+
},
54+
},
55+
]
56+
})
57+
58+
return markdownAST
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Node } from "unist-util-visit"
2+
3+
// ensure only one `/` in new url
4+
const withPathPrefix = (url: string, pathPrefix: string): string =>
5+
(pathPrefix + url).replace(/\/\//, `/`)
6+
7+
// Ensure relative links include `pathPrefix`
8+
export const pathPlugin = ({ pathPrefix }: { pathPrefix: string }) =>
9+
async function transformer(markdownAST: Node): Promise<Node> {
10+
if (!pathPrefix) {
11+
return markdownAST
12+
}
13+
const { visit } = await import(`unist-util-visit`)
14+
15+
visit(markdownAST, [`link`, `definition`], node => {
16+
if (
17+
node.url &&
18+
typeof node.url === `string` &&
19+
node.url.startsWith(`/`) &&
20+
!node.url.startsWith(`//`)
21+
) {
22+
// TODO: where does withPathPrefix
23+
node.url = withPathPrefix(node.url, pathPrefix)
24+
}
25+
})
26+
return markdownAST
27+
}

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25456,13 +25456,6 @@ unist-util-map@^2.0.1:
2545625456
"@types/mdast" "^3.0.0"
2545725457
object-assign "^4.0.0"
2545825458

25459-
unist-util-map@^3.1.1:
25460-
version "3.1.1"
25461-
resolved "https://registry.npmjs.org/unist-util-map/-/unist-util-map-3.1.1.tgz#e69e23fc36a9dd6dfebc90ce8c3cc8e426ed2a71"
25462-
integrity sha512-n36sjBn4ibPtAzrFweyT4FOcCI/UdzboaEcsZvwoAyD/gVw5B3OLlMBySePMO6r+uzjxQEyRll2akfVaT4SHhw==
25463-
dependencies:
25464-
"@types/unist" "^2.0.0"
25465-
2546625459
unist-util-modify-children@^1.0.0, unist-util-modify-children@^1.1.1:
2546725460
version "1.1.6"
2546825461
resolved "https://registry.yarnpkg.com/unist-util-modify-children/-/unist-util-modify-children-1.1.6.tgz#1587130ca0ab5c56155fa60837ff524c3fbfbfaa"

0 commit comments

Comments
 (0)