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 missing types to blog example #959

Open
wants to merge 3 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
12 changes: 9 additions & 3 deletions solutions/blog/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ export async function generateStaticParams() {
}))
}

export function generateMetadata({ params }) {
type Props = {
params: {
slug: string
}
}

export function generateMetadata({ params }: Props) {
let post = getBlogPosts().find((post) => post.slug === params.slug)
if (!post) {
return
return {}
}

let {
Expand Down Expand Up @@ -51,7 +57,7 @@ export function generateMetadata({ params }) {
}
}

export default function Blog({ params }) {
export default function Blog({ params }: Props) {
let post = getBlogPosts().find((post) => post.slug === params.slug)

if (!post) {
Expand Down
8 changes: 4 additions & 4 deletions solutions/blog/app/blog/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ function parseFrontmatter(fileContent: string) {
return { metadata: metadata as Metadata, content }
}

function getMDXFiles(dir) {
function getMDXFiles(dir: fs.PathLike) {
return fs.readdirSync(dir).filter((file) => path.extname(file) === '.mdx')
}

function readMDXFile(filePath) {
function readMDXFile(filePath: fs.PathOrFileDescriptor) {
let rawContent = fs.readFileSync(filePath, 'utf-8')
return parseFrontmatter(rawContent)
}

function getMDXData(dir) {
function getMDXData(dir: fs.PathLike) {
let mdxFiles = getMDXFiles(dir)
return mdxFiles.map((file) => {
let { metadata, content } = readMDXFile(path.join(dir, file))
let { metadata, content } = readMDXFile(path.join(dir.toString(), file))
let slug = path.basename(file, path.extname(file))

return {
Expand Down
45 changes: 25 additions & 20 deletions solutions/blog/app/components/mdx.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import Link from 'next/link'
import Image from 'next/image'
import { MDXRemote } from 'next-mdx-remote/rsc'
import Image, { type ImageProps } from 'next/image'
import { MDXRemote, type MDXRemoteProps } from 'next-mdx-remote/rsc'
import { highlight } from 'sugar-high'
import React from 'react'

function Table({ data }) {
type TableProps = {
data: {
headers: React.ReactNode[]
rows: React.ReactNode[][]
}
}

function Table({ data }: TableProps) {
let headers = data.headers.map((header, index) => (
<th key={index}>{header}</th>
))
Expand All @@ -26,34 +33,32 @@ function Table({ data }) {
)
}

function CustomLink(props) {
type CustomLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement>

function CustomLink(props: CustomLinkProps) {
let href = props.href

if (href.startsWith('/')) {
return (
<Link href={href} {...props}>
{props.children}
</Link>
)
if (href?.startsWith('/')) {
return <Link href={href} {...props} />
}

if (href.startsWith('#')) {
if (href?.startsWith('#')) {
return <a {...props} />
}

return <a target="_blank" rel="noopener noreferrer" {...props} />
}

function RoundedImage(props) {
return <Image alt={props.alt} className="rounded-lg" {...props} />
function RoundedImage(props: ImageProps) {
return <Image className="rounded-lg" {...props} />
}

function Code({ children, ...props }) {
let codeHTML = highlight(children)
function Code({ children, ...props }: React.PropsWithChildren) {
let codeHTML = highlight(children?.toString() ?? '')
return <code dangerouslySetInnerHTML={{ __html: codeHTML }} {...props} />
}

function slugify(str) {
export function slugify(str: string) {
return str
.toString()
.toLowerCase()
Expand All @@ -64,9 +69,9 @@ function slugify(str) {
.replace(/\-\-+/g, '-') // Replace multiple - with single -
}

function createHeading(level) {
const Heading = ({ children }) => {
let slug = slugify(children)
function createHeading(level: number) {
const Heading = ({ children }: React.PropsWithChildren) => {
let slug = slugify(children?.toString() ?? '')
return React.createElement(
`h${level}`,
{ id: slug },
Expand Down Expand Up @@ -99,7 +104,7 @@ let components = {
Table,
}

export function CustomMDX(props) {
export function CustomMDX(props: MDXRemoteProps) {
return (
<MDXRemote
{...props}
Expand Down
4 changes: 3 additions & 1 deletion solutions/blog/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const metadata: Metadata = {
},
}

const cx = (...classes) => classes.filter(Boolean).join(' ')
type ClassName = Array<React.HTMLAttributes<HTMLHtmlElement>['className']>

const cx = (...classes: ClassName) => classes.filter(Boolean).join(' ')

export default function RootLayout({
children,
Expand Down
2 changes: 1 addition & 1 deletion solutions/blog/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
Expand Down