Skip to content

Commit 3506796

Browse files
committed
Change plugin folder
1 parent e46411d commit 3506796

22 files changed

+79
-91
lines changed

contributing.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Files and Folders
2+
3+
- `packages/mdx`: the npm package
4+
- `packages/mdx/vite.config.js`: we only use vite for testing with vitest
5+
- `packages/mdx/rollup.config.js`: rollup builds the thing we release to npm
6+
- `packages/mdx/next.config.js`: we have a nextjs testing site, our poor man's storybook
7+
- `packages/mdx/pages`: the pages for the nextjs test site
8+
- `packages/mdx/dev`: code and content used by the nextjs test site
9+
- `packages/mdx/src/remark`: the code that runs at build time when you compile an mdx file
10+
- `examples`: a list of examples, most of them use the Code Hike version from `packages/mdx/dist`
11+
- `examples/bundle-test`: this one is used by `.github/workflows/bundle-analysis.yml`

packages/mdx/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { remarkCodeHike } from "./mdx-plugin/plugin"
1+
export { transform as remarkCodeHike } from "./remark/transform"
22

33
export { highlight } from "./highlighter"

packages/mdx/src/mdx-plugin/ch-usage.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
File renamed without changes.

packages/mdx/src/mdx-plugin/plugin.ts renamed to packages/mdx/src/remark/transform.ts

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { transformSections } from "./section"
44
import { transformSpotlights } from "./spotlight"
55
import { transformScrollycodings } from "./scrollycoding"
66
import { transformSlideshows } from "./slideshow"
7+
import { transformInlineCodes } from "./inline-code"
8+
import { transformPreviews } from "./preview"
9+
710
import { valueToEstree } from "./to-estree"
811
import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
9-
import { transformPreviews } from "./preview"
10-
import { transformInlineCodes } from "./inline-code"
11-
import { EsmNode, SuperNode, visit } from "./nodes"
12-
import { chUsage } from "./ch-usage"
12+
import { JsxNode, SuperNode, visit } from "./nodes"
1313

1414
type CodeHikeConfig = {
1515
theme: any
@@ -18,66 +18,93 @@ type CodeHikeConfig = {
1818
showCopyButton?: boolean
1919
}
2020

21-
export function remarkCodeHike(
22-
unsafeConfig: CodeHikeConfig
23-
) {
21+
const transforms = [
22+
transformPreviews,
23+
transformScrollycodings,
24+
transformSpotlights,
25+
transformSlideshows,
26+
transformSections,
27+
transformInlineCodes,
28+
transformEditorNodes,
29+
transformCodeNodes,
30+
]
31+
32+
export function transform(unsafeConfig: CodeHikeConfig) {
2433
return async (tree: SuperNode) => {
2534
const config = addConfigDefaults(unsafeConfig)
26-
// TODO add opt-in config
27-
let hasCodeHikeImport = false
28-
visit(tree, "mdxjsEsm", (node: EsmNode) => {
29-
if (
30-
node.value.startsWith(
31-
`import { CH } from "@code-hike/mdx`
32-
)
33-
) {
34-
hasCodeHikeImport = true
35-
}
36-
})
3735

3836
try {
39-
await transformPreviews(tree)
40-
await transformScrollycodings(tree, config)
41-
await transformSpotlights(tree, config)
42-
await transformSlideshows(tree, config)
43-
await transformSections(tree, config)
44-
await transformInlineCodes(tree, config)
45-
await transformEditorNodes(tree, config)
46-
await transformCodeNodes(tree, config)
37+
for (const transform of transforms) {
38+
await transform(tree, config)
39+
}
4740
} catch (e) {
4841
console.error("error running remarkCodeHike", e)
4942
throw e
5043
}
5144

52-
const usage = chUsage(tree)
45+
const usedCodeHikeComponents =
46+
getUsedCodeHikeComponentNames(tree)
5347

54-
if (usage.length > 0) {
48+
if (usedCodeHikeComponents.length > 0) {
5549
addConfig(tree, config)
5650

57-
if (config.autoImport && !hasCodeHikeImport) {
58-
addSmartImport(tree, usage)
51+
if (config.autoImport) {
52+
addSmartImport(tree, usedCodeHikeComponents)
5953
}
6054
}
6155
}
6256
}
6357

58+
/**
59+
* Add defaults and normalize config
60+
*/
6461
function addConfigDefaults(
6562
config: Partial<CodeHikeConfig> | undefined
6663
): CodeHikeConfig {
64+
// TODO warn when config looks weird
6765
return {
6866
...config,
6967
theme: config?.theme || {},
7068
autoImport: config?.autoImport === false ? false : true,
7169
}
7270
}
7371

72+
/**
73+
* Returns a the list of component names
74+
* used inside the tree
75+
* that looks like `<CH.* />`
76+
*/
77+
function getUsedCodeHikeComponentNames(
78+
tree: SuperNode
79+
): string[] {
80+
const usage = []
81+
visit(
82+
tree,
83+
["mdxJsxFlowElement", "mdxJsxTextElement"],
84+
(node: JsxNode) => {
85+
if (
86+
node.name &&
87+
node.name.startsWith("CH.") &&
88+
!usage.includes(node.name)
89+
) {
90+
usage.push(node.name)
91+
}
92+
}
93+
)
94+
return usage
95+
}
96+
97+
/**
98+
* Creates a `chCodeConfig` variable node in the tree
99+
* so that the components can access the config
100+
*/
74101
function addConfig(
75102
tree: SuperNode,
76103
config: CodeHikeConfig
77104
) {
78105
tree.children.unshift({
79106
type: "mdxjsEsm",
80-
value: "export const chCodeConfig = {}",
107+
value: `export const ${CH_CODE_CONFIG_VAR_NAME} = {}`,
81108
data: {
82109
estree: {
83110
type: "Program",
@@ -108,10 +135,17 @@ function addConfig(
108135
})
109136
}
110137

111-
function addSmartImport(tree: SuperNode, usage: string[]) {
138+
/**
139+
* Add an import node at the start of the tree
140+
* importing all the components used
141+
*/
142+
function addSmartImport(
143+
tree: SuperNode,
144+
componentNames: string[]
145+
) {
112146
const specifiers = [
113147
"annotations",
114-
...usage.map(name => name.slice("CH.".length)),
148+
...componentNames.map(name => name.slice("CH.".length)),
115149
]
116150

117151
tree.children.unshift({
@@ -202,41 +236,3 @@ function addSmartImport(tree: SuperNode, usage: string[]) {
202236
},
203237
})
204238
}
205-
206-
function addImportNode(tree: SuperNode) {
207-
tree.children.unshift({
208-
type: "mdxjsEsm",
209-
value:
210-
'import { CH } from "@code-hike/mdx/dist/components.cjs.js"',
211-
data: {
212-
estree: {
213-
type: "Program",
214-
body: [
215-
{
216-
type: "ImportDeclaration",
217-
specifiers: [
218-
{
219-
type: "ImportSpecifier",
220-
imported: {
221-
type: "Identifier",
222-
name: "CH",
223-
},
224-
local: {
225-
type: "Identifier",
226-
name: "CH",
227-
},
228-
},
229-
],
230-
source: {
231-
type: "Literal",
232-
value:
233-
"@code-hike/mdx/dist/components.cjs.js",
234-
raw: '"@code-hike/mdx/dist/components.cjs.js"',
235-
},
236-
},
237-
],
238-
sourceType: "module",
239-
},
240-
},
241-
})
242-
}

0 commit comments

Comments
 (0)