|
1 | 1 | 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'; |
3 | 9 | import ExportedImage from 'next-image-export-optimizer'; |
4 | 10 | import InlineFilter from './src/components/InlineFilter'; |
5 | 11 | import { YoutubeEmbed } from './src/components/YoutubeEmbed'; |
@@ -34,43 +40,71 @@ const MDXHeading4 = (props) => <MDXHeading level={4} {...props} />; |
34 | 40 | const MDXHeading5 = (props) => <MDXHeading level={5} {...props} />; |
35 | 41 | const MDXHeading6 = (props) => <MDXHeading level={6} {...props} />; |
36 | 42 |
|
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 | +}; |
56 | 96 |
|
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, |
74 | 108 | ...components |
75 | 109 | }; |
76 | 110 | } |
0 commit comments