From 0ac690339083f01a0b12a74ec117eeccd055e932 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 22 Nov 2023 17:45:03 +0100 Subject: refactor(components,pages): extract MDX components mapping from pages Instead of repeating the overriding on each pages, we should define it in one place and reuse it in pages. By default it is not possible to override native HTML tags with MDX so I added a plugin in next config to allow it. --- src/pages/cv.tsx | 108 ++----------------------------------- src/pages/index.tsx | 74 ++------------------------ src/pages/mentions-legales.tsx | 18 +------ src/pages/projets/[slug].tsx | 117 ++--------------------------------------- src/pages/projets/index.tsx | 9 +--- 5 files changed, 14 insertions(+), 312 deletions(-) (limited to 'src/pages') diff --git a/src/pages/cv.tsx b/src/pages/cv.tsx index fd19a83..edf267d 100644 --- a/src/pages/cv.tsx +++ b/src/pages/cv.tsx @@ -1,30 +1,24 @@ /* eslint-disable max-statements */ -import type { MDXComponents } from 'mdx/types'; import type { GetStaticProps } from 'next'; import Head from 'next/head'; import NextImage from 'next/image'; import { useRouter } from 'next/router'; import Script from 'next/script'; -import React, { - type AnchorHTMLAttributes, - type HTMLAttributes, - type ReactNode, -} from 'react'; +import React, { type ReactNode } from 'react'; import { useIntl } from 'react-intl'; import { getLayout, Heading, ImageWidget, Link, - List, SocialMediaWidget, - ListItem, Page, PageHeader, PageSidebar, TocWidget, PageBody, } from '../components'; +import { mdxComponents } from '../components/mdx'; import CVContent, { data, meta } from '../content/pages/cv.mdx'; import type { NextPageWithLayout } from '../types'; import { CONFIG } from '../utils/config'; @@ -37,102 +31,6 @@ import { import { loadTranslation } from '../utils/helpers/server'; import { useBreadcrumb, useHeadingsTree } from '../utils/hooks'; -const ExternalLink = ({ - children = '', - href = '', - ...props -}: AnchorHTMLAttributes) => ( - - {children} - -); - -const H1 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H2 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H3 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H4 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H5 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H6 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const OrderedList = ({ - children, - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const UnorderedList = ({ - children, - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const components: MDXComponents = { - a: ExternalLink, - h1: H1, - h2: H2, - h3: H3, - h4: H4, - h5: H5, - h6: H6, - li: ({ ref, ...props }) => , - Link, - ol: OrderedList, - ul: UnorderedList, -}; - /** * CV page. */ @@ -258,7 +156,7 @@ const CVPage: NextPageWithLayout = () => { /> - + ) => (
{children}
); -const H1 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H2 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H3 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H4 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H5 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H6 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const ResponsiveImage = (props: NextImageProps) => ( -
- -
-); - /** * Retrieve a list of coding links. * @@ -368,17 +306,11 @@ const HomePage: NextPageWithLayout = ({ recentPosts }) => { }; const components: MDXComponents = { + ...mdxComponents, CodingLinks, ColdarkRepos, Column, Grid: StyledGrid, - h1: H1, - h2: H2, - h3: H3, - h4: H4, - h5: H5, - h6: H6, - Image: ResponsiveImage, LibreLinks, MoreLinks, RecentPosts: getRecentPosts, diff --git a/src/pages/mentions-legales.tsx b/src/pages/mentions-legales.tsx index e3aabc5..d5958a6 100644 --- a/src/pages/mentions-legales.tsx +++ b/src/pages/mentions-legales.tsx @@ -1,15 +1,11 @@ /* eslint-disable max-statements */ -import type { MDXComponents } from 'mdx/types'; import type { GetStaticProps } from 'next'; import Head from 'next/head'; -import NextImage, { type ImageProps as NextImageProps } from 'next/image'; import { useRouter } from 'next/router'; import Script from 'next/script'; import { useIntl } from 'react-intl'; import { getLayout, - Link, - Figure, Page, PageHeader, PageSidebar, @@ -17,6 +13,7 @@ import { Heading, PageBody, } from '../components'; +import { mdxComponents } from '../components/mdx'; import LegalNoticeContent, { meta } from '../content/pages/legal-notice.mdx'; import type { NextPageWithLayout } from '../types'; import { CONFIG } from '../utils/config'; @@ -29,17 +26,6 @@ import { import { loadTranslation } from '../utils/helpers/server'; import { useBreadcrumb, useHeadingsTree } from '../utils/hooks'; -const ResponsiveImage = (props: NextImageProps) => ( -
- -
-); - -const components: MDXComponents = { - Image: ResponsiveImage, - Link, -}; - /** * Legal Notice page. */ @@ -119,7 +105,7 @@ const LegalNoticePage: NextPageWithLayout = () => { />
- + ); diff --git a/src/pages/projets/[slug].tsx b/src/pages/projets/[slug].tsx index 82d9149..2911951 100644 --- a/src/pages/projets/[slug].tsx +++ b/src/pages/projets/[slug].tsx @@ -3,22 +3,16 @@ import type { MDXComponents } from 'mdx/types'; import type { GetStaticPaths, GetStaticProps } from 'next'; import dynamic from 'next/dynamic'; import Head from 'next/head'; -import NextImage, { type ImageProps as NextImageProps } from 'next/image'; +import NextImage from 'next/image'; import { useRouter } from 'next/router'; import Script from 'next/script'; -import type { ComponentType, HTMLAttributes, ReactNode } from 'react'; +import type { ComponentType } from 'react'; import { useIntl } from 'react-intl'; import { - Code, getLayout, - Link, SharingWidget, Spinner, Heading, - List, - ListItem, - Figure, - Grid, ProjectOverview, type ProjectMeta, type Repository, @@ -28,6 +22,7 @@ import { TocWidget, PageBody, } from '../../components'; +import { mdxComponents } from '../../components/mdx'; import styles from '../../styles/pages/project.module.scss'; import type { NextPageWithLayout, ProjectPreview, Repos } from '../../types'; import { CONFIG } from '../../utils/config'; @@ -49,110 +44,6 @@ import { useHeadingsTree, } from '../../utils/hooks'; -const BorderedImage = (props: NextImageProps) => ( -
- -
-); - -const H1 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H2 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H3 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H4 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H5 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const H6 = ({ - children = '', - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const OrderedList = ({ - children, - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const UnorderedList = ({ - children, - ...props -}: HTMLAttributes) => ( - - {children} - -); - -const Gallery = ({ children }: { children: ReactNode[] }) => ( - { - return { id: `${index}`, item: child }; - })} - sizeMin="250px" - /> -); - -const components: MDXComponents = { - Code, - Gallery, - h1: H1, - h2: H2, - h3: H3, - h4: H4, - h5: H5, - h6: H6, - Image: BorderedImage, - li: ({ ref, ...props }) => , - Link, - ol: OrderedList, - ul: UnorderedList, -}; - type ProjectPageProps = { project: ProjectPreview; translation: Messages; @@ -326,7 +217,7 @@ const ProjectPage: NextPageWithLayout = ({ project }) => { meta={overviewMeta} name={project.title} /> - + = ({ projects }) => { /> } + intro={} />