|
1 | | -import { CopyButton } from "@/components/ui/copy-button"; |
2 | 1 | import { getPost, getPosts } from "@/lib/ghost"; |
3 | 2 | import type { Metadata, ResolvingMetadata } from "next"; |
4 | | -import { getTranslations, setRequestLocale } from "next-intl/server"; |
| 3 | +import { getTranslations } from "next-intl/server"; |
5 | 4 | import Image from "next/image"; |
6 | 5 | import Link from "next/link"; |
7 | 6 | import { notFound } from "next/navigation"; |
8 | | -import prettier from "prettier"; |
9 | | -import type { DetailedHTMLProps, HTMLAttributes } from "react"; |
10 | 7 | import type React from "react"; |
11 | 8 | import ReactMarkdown from "react-markdown"; |
12 | 9 | import type { Components } from "react-markdown"; |
13 | 10 | import rehypeRaw from "rehype-raw"; |
14 | 11 | import remarkGfm from "remark-gfm"; |
15 | 12 | import remarkToc from "remark-toc"; |
16 | | -import { codeToHtml } from "shiki"; |
17 | 13 | import type { BundledLanguage } from "shiki/bundle/web"; |
18 | | -import slugify from "slugify"; |
19 | 14 | import TurndownService from "turndown"; |
20 | 15 | // @ts-ignore |
21 | 16 | import * as turndownPluginGfm from "turndown-plugin-gfm"; |
| 17 | +import { CodeBlock } from "./components/CodeBlock"; |
22 | 18 | import { H1, H2, H3 } from "./components/Headings"; |
23 | 19 | import { TableOfContents } from "./components/TableOfContents"; |
24 | 20 | import { ZoomableImage } from "./components/ZoomableImage"; |
25 | | - |
26 | 21 | type Props = { |
27 | 22 | params: { locale: string; slug: string }; |
28 | 23 | }; |
@@ -84,62 +79,6 @@ export async function generateStaticParams() { |
84 | 79 | ); |
85 | 80 | } |
86 | 81 |
|
87 | | -interface CodeProps |
88 | | - extends DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> { |
89 | | - inline?: boolean; |
90 | | - className?: string; |
91 | | - children?: React.ReactNode; |
92 | | -} |
93 | | -interface LanguageProps { |
94 | | - children: string; |
95 | | - lang: BundledLanguage; |
96 | | -} |
97 | | - |
98 | | -const getParserForLanguage = (language: string): string => { |
99 | | - const languageMap: { [key: string]: string } = { |
100 | | - js: "babel", |
101 | | - jsx: "babel", |
102 | | - ts: "typescript", |
103 | | - tsx: "typescript", |
104 | | - json: "json", |
105 | | - css: "css", |
106 | | - scss: "scss", |
107 | | - less: "less", |
108 | | - html: "html", |
109 | | - xml: "xml", |
110 | | - markdown: "markdown", |
111 | | - md: "markdown", |
112 | | - yaml: "yaml", |
113 | | - yml: "yaml", |
114 | | - }; |
115 | | - |
116 | | - return languageMap[language.toLowerCase()] || "babel"; |
117 | | -}; |
118 | | - |
119 | | -async function CodeBlock(props: LanguageProps) { |
120 | | - const format = await prettier.format(props.children, { |
121 | | - semi: true, |
122 | | - singleQuote: true, |
123 | | - tabWidth: 2, |
124 | | - useTabs: false, |
125 | | - printWidth: 120, |
126 | | - parser: getParserForLanguage(props.lang), |
127 | | - }); |
128 | | - const out = await codeToHtml(format, { |
129 | | - lang: props.lang, |
130 | | - theme: "houston", |
131 | | - }); |
132 | | - |
133 | | - return ( |
134 | | - <div className="group relative"> |
135 | | - <CopyButton text={format} /> |
136 | | - <div |
137 | | - dangerouslySetInnerHTML={{ __html: out }} |
138 | | - className="text-sm p-4 rounded-lg bg-[#18191F] overflow-auto" |
139 | | - /> |
140 | | - </div> |
141 | | - ); |
142 | | -} |
143 | 82 | export default async function BlogPostPage({ params }: Props) { |
144 | 83 | const { locale, slug } = await params; |
145 | 84 | // setRequestLocale(locale); |
@@ -235,9 +174,8 @@ export default async function BlogPostPage({ params }: Props) { |
235 | 174 | className="object-cover max-w-lg mx-auto rounded-lg border max-lg:w-64 border-border overflow-hidden" |
236 | 175 | /> |
237 | 176 | ), |
238 | | - code: ({ inline, className, children, ...props }: CodeProps) => { |
| 177 | + code: ({ className, children }) => { |
239 | 178 | const match = /language-(\w+)/.exec(className || ""); |
240 | | - |
241 | 179 | return ( |
242 | 180 | <CodeBlock lang={match ? (match[1] as BundledLanguage) : "ts"}> |
243 | 181 | {children?.toString() || ""} |
|
0 commit comments