Skip to content

Commit 94a6ec8

Browse files
committed
Fix typescript
1 parent e81ed3c commit 94a6ec8

22 files changed

+9551
-124
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ yarn-error.log
99
.vscode
1010
storybook-static
1111
packages/playground/out
12-
packages/starter
12+
packages/starter
13+
14+
# Contentlayer
15+
.contentlayer

packages/mdx/package.json

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
{
22
"name": "@code-hike/mdx",
3-
"version": "0.3.0-next.0",
3+
"version": "0.3.0",
44
"files": [
55
"dist"
66
],
77
"main": "./dist/ch-core.umd.js",
8-
"module": "./dist/ch-core.es.js",
8+
"module": "./dist/plugin.es.js",
99
"exports": {
1010
".": {
1111
"import": "./dist/plugin.es.js",
12-
"require": "./dist/plugin.umd.js"
12+
"require": "./dist/plugin.umd.js",
13+
"style": "./dist/style.css"
1314
},
14-
"./components": "./dist/components.es.js"
15+
"./components": "./dist/components.es.js",
16+
"./dist/index.css": "./dist/style.css"
1517
},
1618
"types": "./dist/types/index.d.ts",
1719
"scripts": {
1820
"dev": "next",
19-
"build": "vite build",
21+
"build": "rollup -c rollup.config.js",
2022
"test": "vitest",
2123
"coverage": "vitest run --coverage"
2224
},
@@ -25,8 +27,8 @@
2527
"diff": "^4.0.2",
2628
"hast-util-to-estree": "^1.4.0",
2729
"is-plain-obj": "^3.0.0",
28-
"mdx-bundler": "^8.0.1",
2930
"node-fetch": "^2.0.0",
31+
"mdx-bundler": "^8.0.1",
3032
"remark-rehype": "^8.1.0",
3133
"shiki": "^0.10.1",
3234
"unified": "^9.2.2",
@@ -35,20 +37,26 @@
3537
"use-spring": "^0.2.3"
3638
},
3739
"devDependencies": {
40+
"@rollup/plugin-json": "^4.1.0",
3841
"@mdx-js/mdx": "^2.0.0",
3942
"@types/node": "^17.0.21",
4043
"@types/node-fetch": "^2.6.1",
4144
"@types/react": "^17.0.39",
45+
"esbuild": "^0.13.2",
46+
"mdx-bundler": "^6.0.1",
4247
"next": "^12.1.0",
4348
"react": "^17.0.2",
4449
"react-dom": "^17.0.2",
50+
"react-json-view": "^1.21.3",
4551
"sass": "^1.49.9",
46-
"typescript": "^4.6.2",
52+
"typescript": "^3.9.5",
4753
"vite": "^2.8.6",
48-
"vitest": "^0.5.9",
49-
"esbuild": "^0.13.2",
50-
"mdx-bundler": "^6.0.1",
51-
"react-json-view": "^1.21.3"
54+
"rollup": "^2.41.2",
55+
"autoprefixer": "^9.8.2",
56+
"cssnano": "^4.1.10",
57+
"rollup-plugin-postcss": "^4.0.0",
58+
"rollup-plugin-typescript2": "^0.27.1",
59+
"vitest": "^0.5.9"
5260
},
5361
"peerDependencies": {
5462
"react": "^16.8.3 || ^17 || ^18"

packages/mdx/rollup.config.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import autoprefixer from "autoprefixer"
2+
import postcss from "rollup-plugin-postcss"
3+
4+
import cssnano from "cssnano"
5+
import typescript from "rollup-plugin-typescript2"
6+
// import copy from "rollup-plugin-copy"
7+
import path from "path"
8+
import json from "@rollup/plugin-json"
9+
import fs from "fs"
10+
11+
const createConfig = filename => ({
12+
input: `src/${filename}.tsx`,
13+
output: [
14+
// {
15+
// file: `./dist/${filename}.js`,
16+
// format: "umd",
17+
// name: "ch", //todo get by parameter
18+
// },
19+
{
20+
file: `./dist/${filename}.cjs.js`,
21+
format: "cjs",
22+
},
23+
{
24+
file: `./dist/${filename}.esm.js`,
25+
format: "es",
26+
},
27+
],
28+
// external: ["react"],
29+
plugins: [
30+
json({ compact: true }),
31+
typescript({
32+
tsconfigOverride: {
33+
compilerOptions: { jsx: "react" },
34+
},
35+
}),
36+
postcss({
37+
extract: path.resolve("dist/index.css"),
38+
plugins: [autoprefixer(), cssnano()],
39+
}),
40+
// copy({
41+
// targets: [{ src: "src/index.scss", dest: "dist" }],
42+
// }),
43+
],
44+
})
45+
46+
export default function makeConfig(commandOptions) {
47+
const { inputs = ["index"] } = JSON.parse(
48+
fs.readFileSync("package.json", "utf8")
49+
)
50+
return inputs.map(createConfig)
51+
}

packages/mdx/src/index.tsx

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

packages/mdx/src/mdx-plugin/annotations.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Code, relativeToAbsolute } from "../utils"
22
import { CodeAnnotation } from "../smooth-code"
3-
import { Node, Parent } from "unist"
43
import { wrapChildren } from "./to-estree"
54
import { annotationsMap } from "../mdx-client/annotations"
5+
import { JsxNode as JsxNode, SuperNode } from "./nodes"
66

77
export function getAnnotationsFromMetastring(
88
options: Record<string, string>
@@ -76,9 +76,9 @@ function getCommentData(line: Code["lines"][0]) {
7676
}
7777

7878
export function extractJSXAnnotations(
79-
node: Node,
79+
node: SuperNode,
8080
index: number,
81-
parent: Parent
81+
parent: SuperNode
8282
) {
8383
const annotations = [] as CodeAnnotation[]
8484

@@ -87,7 +87,8 @@ export function extractJSXAnnotations(
8787
parent.children[nextIndex] &&
8888
parent.children[nextIndex].type ===
8989
"mdxJsxFlowElement" &&
90-
parent.children[nextIndex].name === "CH.Annotation"
90+
(parent.children[nextIndex] as JsxNode).name ===
91+
"CH.Annotation"
9192
) {
9293
const jsxAnnotation = parent.children[nextIndex] as any
9394

packages/mdx/src/mdx-plugin/code.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Node, Parent } from "unist"
21
import { highlight } from "../highlighter"
32
import { extractLinks } from "./links"
43
import {
@@ -16,15 +15,16 @@ import {
1615
extractJSXAnnotations,
1716
} from "./annotations"
1817
import { mergeFocus } from "../utils"
18+
import { CodeNode, SuperNode } from "./nodes"
1919

2020
export async function transformCodeNodes(
21-
tree: Node,
21+
tree: SuperNode,
2222
{ theme }: { theme: any }
2323
) {
2424
await visitAsync(
2525
tree,
2626
"code",
27-
async (node, index, parent) => {
27+
async (node: CodeNode, index, parent) => {
2828
await transformCode(
2929
{ node, index, parent: parent! },
3030
{ theme }
@@ -33,7 +33,7 @@ export async function transformCodeNodes(
3333
)
3434
}
3535

36-
export function isEditorNode(node: Node) {
36+
export function isEditorNode(node: SuperNode) {
3737
return (
3838
node.type === "code" ||
3939
(node.type === "mdxJsxFlowElement" &&
@@ -42,7 +42,7 @@ export function isEditorNode(node: Node) {
4242
}
4343

4444
async function transformCode(
45-
nodeInfo: NodeInfo,
45+
nodeInfo: NodeInfo<CodeNode>,
4646
config: { theme: any }
4747
) {
4848
toJSX(nodeInfo.node, {
@@ -66,14 +66,14 @@ export async function mapAnyCodeNode(
6666
) {
6767
const { node } = nodeInfo
6868
if (node.type === "code") {
69-
return mapCode(nodeInfo, config)
69+
return mapCode(nodeInfo as NodeInfo<CodeNode>, config)
7070
} else {
7171
return mapEditor(nodeInfo, config)
7272
}
7373
}
7474

7575
async function mapCode(
76-
nodeInfo: NodeInfo,
76+
nodeInfo: NodeInfo<CodeNode>,
7777
config: { theme: any }
7878
): Promise<EditorProps> {
7979
const file = await mapFile(nodeInfo, config)
@@ -94,19 +94,23 @@ export async function mapEditor(
9494
config: { theme: any }
9595
): Promise<EditorProps> {
9696
const [northNodes, southNodes = []] = splitChildren(
97-
node as Parent,
97+
node,
9898
"thematicBreak"
9999
)
100100

101101
const northFiles = await Promise.all(
102102
northNodes
103103
.filter(({ node }) => node.type === "code")
104-
.map(nodeInfo => mapFile(nodeInfo, config))
104+
.map((nodeInfo: NodeInfo<CodeNode>) =>
105+
mapFile(nodeInfo, config)
106+
)
105107
)
106108
const southFiles = await Promise.all(
107109
southNodes
108110
.filter(({ node }) => node.type === "code")
109-
.map(nodeInfo => mapFile(nodeInfo, config))
111+
.map((nodeInfo: NodeInfo<CodeNode>) =>
112+
mapFile(nodeInfo, config)
113+
)
110114
)
111115
const allFiles = [...northFiles, ...southFiles]
112116
const northActive =
@@ -141,7 +145,7 @@ export async function mapEditor(
141145
}
142146

143147
async function mapFile(
144-
{ node, index, parent }: NodeInfo,
148+
{ node, index, parent }: NodeInfo<CodeNode>,
145149
config: { theme: any }
146150
): Promise<CodeStep & FileOptions & { name: string }> {
147151
const { theme } = config

packages/mdx/src/mdx-plugin/editor.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Node, Parent } from "unist"
21
import { visitAsync } from "./unist-utils"
32
import { transformEditor } from "./code"
3+
import { JsxNode, SuperNode } from "./nodes"
44

55
export async function transformEditorNodes(
6-
tree: Node,
6+
tree: SuperNode,
77
{ theme }: { theme: any }
88
) {
99
await visitAsync(
1010
tree,
1111
"mdxJsxFlowElement",
12-
async (node, index, parent) => {
12+
async (node: JsxNode, index, parent) => {
1313
if (node.name === "CH.Code") {
1414
await transformEditor(
1515
{ node, index, parent: parent! },

packages/mdx/src/mdx-plugin/inline-code.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import {
33
toJSX,
44
CH_CODE_CONFIG_PLACEHOLDER,
55
} from "./unist-utils"
6-
import { Node, Parent } from "unist"
7-
import visit from "unist-util-visit"
8-
import visitParents from "unist-util-visit-parents"
96
import { highlight } from "../highlighter"
107
import { EditorStep } from "../mini-editor"
118
import { Code } from "../utils"
9+
import { SuperNode, visit } from "./nodes"
10+
import visitParents from "unist-util-visit-parents"
1211

1312
export async function transformInlineCodes(
14-
tree: Node,
13+
tree: SuperNode,
1514
{ theme }: { theme: any }
1615
) {
1716
// transform *`foo`* to <CH.InlineCode>foo</CH.InlineCode>
@@ -56,8 +55,8 @@ export async function transformInlineCodes(
5655
}
5756

5857
async function getCode(
59-
tree: Node,
60-
node: Parent,
58+
tree: SuperNode,
59+
node: SuperNode,
6160
inlinedCode: string,
6261
lang: string | undefined,
6362
theme: any
@@ -104,10 +103,13 @@ async function getCode(
104103
})
105104
}
106105

107-
function getAncestors(tree: Node, node: Node): Node[] {
108-
let ancestors: Node[] = []
109-
visitParents(tree, node, (node, nodeAncestors) => {
110-
ancestors = nodeAncestors
106+
function getAncestors(
107+
tree: SuperNode,
108+
node: SuperNode
109+
): SuperNode[] {
110+
let ancestors: SuperNode[] = []
111+
visitParents(tree, node as any, (node, nodeAncestors) => {
112+
ancestors = nodeAncestors as SuperNode[]
111113
})
112114
return ancestors
113115
}

packages/mdx/src/mdx-plugin/links.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Node, Parent } from "unist"
21
import { CodeStep } from "../smooth-code"
32
import { annotationsMap } from "../mdx-client/annotations"
3+
import { SuperNode } from "./nodes"
44

55
export function extractLinks(
6-
node: Node,
6+
node: SuperNode,
77
index: number,
8-
parent: Parent,
8+
parent: SuperNode,
99
code: string
1010
) {
1111
const annotations = [] as CodeStep["annotations"]

0 commit comments

Comments
 (0)