Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type-checking to MDX files, narrow InlineFilter type #7865

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 72 additions & 38 deletions mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as React from 'react';
import type { MDXComponents } from 'mdx/types';
import type { Metadata } from 'next';
import type {
MDXComponents as BaseMDXComponents,
MDXProps as BaseMDXProps
} from 'mdx/types';
import type { PageNode } from './src/directory/directory';
import type { Platform } from './src/data/platforms';
import ExportedImage from 'next-image-export-optimizer';
import InlineFilter from './src/components/InlineFilter';
import { YoutubeEmbed } from './src/components/YoutubeEmbed';
Expand Down Expand Up @@ -35,44 +41,72 @@ const MDXHeading4 = (props) => <MDXHeading level={4} {...props} />;
const MDXHeading5 = (props) => <MDXHeading level={5} {...props} />;
const MDXHeading6 = (props) => <MDXHeading level={6} {...props} />;

export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Map markdown elements to custom components
a: MDXLink,
h1: MDXHeading1,
h2: MDXHeading2,
h3: MDXHeading3,
h4: MDXHeading4,
h5: MDXHeading5,
h6: MDXHeading6,
pre: (preProps) => {
const props = preToCodeBlock(preProps);
if (props) {
return <MDXCode {...props} />;
}
return <pre {...preProps} />;
},
img: ResponsiveImage,
table: MDXTable,
const components = {
// Map markdown elements to custom components
a: MDXLink,
h1: MDXHeading1,
h2: MDXHeading2,
h3: MDXHeading3,
h4: MDXHeading4,
h5: MDXHeading5,
h6: MDXHeading6,
pre: (preProps) => {
const props = preToCodeBlock(preProps);
if (props) {
return <MDXCode {...props} />;
}
return <pre {...preProps} />;
},
img: ResponsiveImage,
table: MDXTable,

// Make common custom components available to content authors
Accordion,
Block,
BlockSwitcher,
Callout,
Fragments,
InlineFilter,
MigrationAlert,
YoutubeEmbed,
Overview,
ExternalLink,
ExternalLinkButton,
InternalLinkButton,
Grid,
Columns,
Video,
View,
ReferencePage
} satisfies BaseMDXComponents;

/**
* MDX Page metadata
*/
type MDXPageMeta = Metadata & {
platforms: Platform[];
};

/**
* Type for Next.js's getStaticProps return in MDX pages
* this is needed to satisfy the type-check for <Overview>'s childPageNodes
*/
type MDXGetStaticPropsResult = {
meta: MDXPageMeta;
childPageNodes?: PageNode[];
};

// Make common custom components available to content authors
Accordion,
Block,
BlockSwitcher,
Callout,
Fragments,
InlineFilter,
MigrationAlert,
YoutubeEmbed,
Overview,
ExternalLink,
ExternalLinkButton,
InternalLinkButton,
Grid,
Columns,
Video,
View,
ReferencePage,
/**
* Declare types in the global scope for MDX to type-check components and function return statements
*/
declare global {
type MDXProvidedComponents = typeof components;
type MDXProps = BaseMDXProps & MDXGetStaticPropsResult;
}

export function useMDXComponents(_components: BaseMDXComponents) {
return {
..._components,
...components
};
}
3 changes: 2 additions & 1 deletion src/components/InlineFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Platform } from '@/data/platforms';
import { Fragment } from 'react';
import FilterChildren from '../FilterChildren';

type InlineFilterProps = {
children: React.ReactNode;
filters: string[];
filters: Platform[];
};

export default function InlineFilter({ filters, children }: InlineFilterProps) {
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
"extends": "./tsconfig.base.json",
"include": [
"next-env.d.ts",
"mdx-components.tsx",
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.mdx",
"tasks",
"adobe.d.ts",
"jest.setup.ts"
]
],
"mdx": {
"checkMdx": true
}
}