Skip to content

Commit 8bee843

Browse files
committed
Better typescript support
1 parent d9f66ba commit 8bee843

8 files changed

Lines changed: 78 additions & 14 deletions

File tree

.changeset/big-mangos-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bright": minor
3+
---
4+
5+
Better typescript support

lib/src/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import {
88
BrightComponents,
99
BrightProps,
1010
Extension,
11+
CodeText,
1112
} from "./types"
1213
import { linesToContent } from "./lines"
1314
import { tokensToContent, tokensToTokenList } from "./tokens"
14-
import React from "react"
15+
import React, { FunctionComponent } from "react"
1516
import "server-only"
1617

1718
type CodeComponent = ((props: InputCodeProps) => Promise<JSX.Element>) &
18-
Partial<InputCodeProps>
19+
Partial<InputCodeProps> &
20+
FunctionComponent<InputCodeProps> // this is a lie, until we have typescript 5.1
1921

20-
const Code: CodeComponent = async (componentProps) => {
22+
const Code = (async (componentProps) => {
2123
// merge props and global props
2224
const { children, lang, ...rest } = {
2325
...Code,
@@ -26,7 +28,7 @@ const Code: CodeComponent = async (componentProps) => {
2628
}
2729

2830
// parse code, lang, subProps maybe from markdown
29-
const propsFromChildren = parseChildren(children, lang, rest.code)
31+
const propsFromChildren = parseChildren(children as CodeText, lang, rest.code)
3032

3133
// split code and annotations
3234
let props = { ...rest, ...propsFromChildren }
@@ -62,7 +64,7 @@ const Code: CodeComponent = async (componentProps) => {
6264
/>
6365
</>
6466
)
65-
}
67+
}) as CodeComponent
6668

6769
async function AnnotatedCode(props: CodeProps) {
6870
let newProps = await extractAnnotationsFromCode(props)
@@ -176,8 +178,8 @@ function runExtensionsBeforeHighlight(props: CodeProps): CodeProps {
176178
}
177179

178180
function parseChildren(
179-
children: InputCodeProps["children"],
180-
lang: LanguageAlias,
181+
children: CodeText,
182+
lang?: LanguageAlias,
181183
code?: string
182184
): Partial<BrightProps> {
183185
if (typeof children === "object" && children?.type === "code") {

lib/src/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ThemeColors,
88
} from "@code-hike/lighter"
99
import brightComponents from "./components"
10+
import type { ReactNode } from "react"
1011

1112
type Prettify<T> = {
1213
[K in keyof T]: T[K]
@@ -36,10 +37,13 @@ export type Extension = Prettify<
3637
{
3738
name: string
3839
beforeRoot?: (props: CodeProps, annotations: Annotation[]) => CodeProps
39-
beforeHighlight?: (props: CodeProps, annotations: Annotation[]) => CodeProps
40+
beforeHighlight?: (
41+
props: CodeProps,
42+
annotations: Annotation[]
43+
) => CodeProps | undefined
4044
InlineAnnotation?: InlineAnnotationComponent
4145
MultilineAnnotation?: MultilineAnnotationComponent
42-
} & BrightComponents
46+
} & Partial<BrightComponents>
4347
>
4448

4549
export type Extensions = Extension[]
@@ -60,12 +64,12 @@ type MdMultiCodeText = {
6064
children: MdCodeText[]
6165
}
6266
}
63-
type CodeText = string | MdCodeText | MdMultiCodeText
67+
export type CodeText = string | MdCodeText | MdMultiCodeText
6468

6569
export type InputCodeProps = Prettify<
66-
Omit<CodeProps, "mode" | "code" | "theme"> & {
67-
theme: Theme | DoubleTheme
68-
children: CodeText
70+
Omit<Partial<CodeProps>, "mode" | "code" | "theme"> & {
71+
theme?: Theme | DoubleTheme
72+
children?: ReactNode
6973
lang?: LanguageAlias
7074
code?: string
7175
}

web/.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"typescript.tsdk": "../node_modules/typescript/lib",
3+
"typescript.enablePromptUseWorkspaceTsdk": true
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from "react"
12
import { Code } from "bright"
23

34
Code.theme = {
@@ -20,12 +21,15 @@ export default function Page() {
2021
return (
2122
<main>
2223
<div className="light">
24+
{/* @ts-expect-error */}
2325
<Code lang="jsx">{code}</Code>
2426
</div>
2527
<div>
28+
{/* @ts-expect-error */}
2629
<Code lang="jsx">{code}</Code>
2730
</div>
2831
<div data-theme="dark">
32+
{/* @ts-expect-error */}
2933
<Code lang="jsx">{code}</Code>
3034
</div>
3135
</main>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Code } from "bright"
22
import { Fira_Code } from "@next/font/google"
3+
import type { MDXComponents } from "mdx/types"
4+
import React from "react"
35

46
const font = Fira_Code({ subsets: ["latin"] })
57

@@ -41,6 +43,8 @@ Code.extensions = [
4143
let newRanges = [{ fromLineNumber: 1, toLineNumber: lineCount }]
4244

4345
for (const range of ranges) {
46+
if (!("fromLineNumber" in range)) continue
47+
4448
const { fromLineNumber, toLineNumber } = range
4549
newRanges = newRanges.flatMap((r) => {
4650
if (
@@ -81,6 +85,7 @@ Code.extensions = [
8185
toLineNumber: r.toLineNumber,
8286
},
8387
]
88+
return []
8489
})
8590
}
8691

@@ -122,6 +127,6 @@ Code.extensions = [
122127
// },
123128
]
124129

125-
export function useMDXComponents(components) {
130+
export function useMDXComponents(components: MDXComponents): MDXComponents {
126131
return { ...components, pre: Code }
127132
}

web/next-env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

web/tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"dom",
5+
"dom.iterable",
6+
"esnext"
7+
],
8+
"allowJs": true,
9+
"skipLibCheck": true,
10+
"strict": false,
11+
"forceConsistentCasingInFileNames": true,
12+
"noEmit": true,
13+
"incremental": true,
14+
"esModuleInterop": true,
15+
"module": "esnext",
16+
"moduleResolution": "node",
17+
"resolveJsonModule": true,
18+
"isolatedModules": true,
19+
"jsx": "preserve",
20+
"plugins": [
21+
{
22+
"name": "next"
23+
}
24+
]
25+
},
26+
"include": [
27+
"next-env.d.ts",
28+
".next/types/**/*.ts",
29+
"**/*.ts",
30+
"**/*.tsx"
31+
],
32+
"exclude": [
33+
"node_modules"
34+
]
35+
}

0 commit comments

Comments
 (0)