Skip to content

Commit 88de99b

Browse files
committed
add type-checking to MDX files, narrow InlineFilter type
1 parent ab3d9e9 commit 88de99b

File tree

3 files changed

+79
-39
lines changed

3 files changed

+79
-39
lines changed

mdx-components.tsx

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import * as React from 'react';
2-
import type { MDXComponents } from 'mdx/types';
2+
import type { Metadata } from 'next';
3+
import type {
4+
MDXComponents as BaseMDXComponents,
5+
MDXProps as BaseMDXProps
6+
} from 'mdx/types';
7+
import type { PageNode } from './src/directory/directory';
8+
import type { Platform } from './src/data/platforms';
39
import ExportedImage from 'next-image-export-optimizer';
410
import InlineFilter from './src/components/InlineFilter';
511
import { YoutubeEmbed } from './src/components/YoutubeEmbed';
@@ -34,43 +40,71 @@ const MDXHeading4 = (props) => <MDXHeading level={4} {...props} />;
3440
const MDXHeading5 = (props) => <MDXHeading level={5} {...props} />;
3541
const MDXHeading6 = (props) => <MDXHeading level={6} {...props} />;
3642

37-
export function useMDXComponents(components: MDXComponents): MDXComponents {
38-
return {
39-
// Map markdown elements to custom components
40-
a: MDXLink,
41-
h1: MDXHeading1,
42-
h2: MDXHeading2,
43-
h3: MDXHeading3,
44-
h4: MDXHeading4,
45-
h5: MDXHeading5,
46-
h6: MDXHeading6,
47-
pre: (preProps) => {
48-
const props = preToCodeBlock(preProps);
49-
if (props) {
50-
return <MDXCode {...props} />;
51-
}
52-
return <pre {...preProps} />;
53-
},
54-
img: ResponsiveImage,
55-
table: MDXTable,
43+
const components = {
44+
// Map markdown elements to custom components
45+
a: MDXLink,
46+
h1: MDXHeading1,
47+
h2: MDXHeading2,
48+
h3: MDXHeading3,
49+
h4: MDXHeading4,
50+
h5: MDXHeading5,
51+
h6: MDXHeading6,
52+
pre: (preProps) => {
53+
const props = preToCodeBlock(preProps);
54+
if (props) {
55+
return <MDXCode {...props} />;
56+
}
57+
return <pre {...preProps} />;
58+
},
59+
img: ResponsiveImage,
60+
table: MDXTable,
61+
62+
// Make common custom components available to content authors
63+
Accordion,
64+
Block,
65+
BlockSwitcher,
66+
Callout,
67+
Fragments,
68+
InlineFilter,
69+
MigrationAlert,
70+
YoutubeEmbed,
71+
Overview,
72+
ExternalLink,
73+
ExternalLinkButton,
74+
InternalLinkButton,
75+
Grid,
76+
Columns,
77+
Video,
78+
View
79+
} satisfies BaseMDXComponents;
80+
81+
/**
82+
* MDX Page metadata
83+
*/
84+
type MDXPageMeta = Metadata & {
85+
platforms: Platform[];
86+
};
87+
88+
/**
89+
* Type for Next.js's getStaticProps return in MDX pages
90+
* this is needed to satisfy the type-check for <Overview>'s childPageNodes
91+
*/
92+
type MDXGetStaticPropsResult = {
93+
meta: MDXPageMeta;
94+
childPageNodes?: PageNode[];
95+
};
5696

57-
// Make common custom components available to content authors
58-
Accordion,
59-
Block,
60-
BlockSwitcher,
61-
Callout,
62-
Fragments,
63-
InlineFilter,
64-
MigrationAlert,
65-
YoutubeEmbed,
66-
Overview,
67-
ExternalLink,
68-
ExternalLinkButton,
69-
InternalLinkButton,
70-
Grid,
71-
Columns,
72-
Video,
73-
View,
97+
/**
98+
* Declare types in the global scope for MDX to type-check components and function return statements
99+
*/
100+
declare global {
101+
type MDXProvidedComponents = typeof components;
102+
type MDXProps = BaseMDXProps & MDXGetStaticPropsResult;
103+
}
104+
105+
export function useMDXComponents(_components: BaseMDXComponents) {
106+
return {
107+
..._components,
74108
...components
75109
};
76110
}

src/components/InlineFilter/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import type { Platform } from '@/data/platforms';
12
import { Fragment } from 'react';
23
import FilterChildren from '../FilterChildren';
34

45
type InlineFilterProps = {
56
children: React.ReactNode;
6-
filters: string[];
7+
filters: Platform[];
78
};
89

910
export default function InlineFilter({ filters, children }: InlineFilterProps) {

tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
"extends": "./tsconfig.base.json",
2828
"include": [
2929
"next-env.d.ts",
30+
"mdx-components.tsx",
3031
"src/**/*.ts",
3132
"src/**/*.tsx",
33+
"src/**/*.mdx",
3234
"tasks",
3335
"adobe.d.ts",
3436
"jest.setup.ts"
35-
]
37+
],
38+
"mdx": {
39+
"checkMdx": true
40+
}
3641
}

0 commit comments

Comments
 (0)