feat: migrate to shiki, support custom code theme (#1692)

* refactor: migrate to shiki

* read code theme from character metadata

* update

* remove refractor

* save code theme

* update

* fix post editor

* update

* update

* update
This commit is contained in:
Stephen Zhou
2024-04-11 10:41:59 +08:00
committed by GitHub
parent 5c9214634d
commit ce6e64ff3c
22 changed files with 244 additions and 160 deletions
+4
View File
@@ -18,6 +18,7 @@ import { Avatar } from "~/components/ui/Avatar"
import { Button } from "~/components/ui/Button"
import { UniLink } from "~/components/ui/UniLink"
import { useDate } from "~/hooks/useDate"
import { useHighlighter } from "~/hooks/useHighlighter"
import { CSB_SCAN } from "~/lib/env"
import { getSiteLink } from "~/lib/helpers"
import { cn } from "~/lib/utils"
@@ -57,6 +58,8 @@ export const CommentItem = ({
])
}, [deletePage.isSuccess, originalCharacterId, originalNoteId, queryClient])
const highlighter = useHighlighter()
if (!comment.metadata?.content?.content) {
return null
}
@@ -139,6 +142,7 @@ export const CommentItem = ({
<MarkdownContent
content={comment.metadata?.content?.content}
strictMode={true}
highlighter={highlighter}
></MarkdownContent>
<div className="mt-1 flex items-center">
<div
+15 -1
View File
@@ -1,5 +1,7 @@
import type { Result as TocResult } from "mdast-util-toc"
import { memo, type MutableRefObject } from "react"
import { Highlighter } from "shiki"
import type { BundledTheme } from "shiki/themes"
import PostActions from "~/components/site/PostActions"
import PostToc from "~/components/site/PostToc"
@@ -22,6 +24,8 @@ const MarkdownContent = memo(function PageContent({
site,
withActions,
onlyContent,
highlighter,
codeTheme,
}: {
content?: string
className?: string
@@ -35,12 +39,22 @@ const MarkdownContent = memo(function PageContent({
site?: ExpandedCharacter
withActions?: boolean
onlyContent?: boolean
highlighter?: Highlighter
codeTheme?: {
light?: BundledTheme
dark?: BundledTheme
}
}) {
let inParsedContent
if (parsedContent) {
inParsedContent = parsedContent
} else if (content) {
inParsedContent = renderPageContent(content, strictMode)
inParsedContent = renderPageContent({
content,
strictMode,
codeTheme,
highlighter,
})
}
let toc: TocResult | undefined = undefined
@@ -0,0 +1,15 @@
import { createHighlighter } from "~/lib/highlighter"
import MarkdownContent from "./MarkdownContent"
type MarkdownContentServerProps = Omit<
React.ComponentProps<typeof MarkdownContent>,
"highlighter"
>
const MarkdownContentServer = async (props: MarkdownContentServerProps) => {
const highlighter = await createHighlighter()
return <MarkdownContent {...props} highlighter={highlighter} />
}
export default MarkdownContentServer
+18 -2
View File
@@ -2,6 +2,7 @@ import { useDebounceEffect } from "ahooks"
import type { Root } from "hast"
import { useTranslations } from "next-intl"
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import type { BundledTheme } from "shiki/themes"
import { EditorView } from "@codemirror/view"
@@ -9,6 +10,7 @@ import MarkdownContent from "~/components/common/MarkdownContent"
import { toolbarShortcuts } from "~/components/dashboard/toolbars"
import { editorUpload } from "~/components/dashboard/toolbars/Multimedia"
import CodeMirror from "~/components/ui/CodeMirror"
import { useHighlighter } from "~/hooks/useHighlighter"
import { useIsMobileLayout } from "~/hooks/useMobileLayout"
import { cn } from "~/lib/utils"
import { renderPageContent } from "~/markdown"
@@ -19,13 +21,19 @@ export default function DualColumnEditor({
onCreateEditor,
isRendering,
setIsRendering,
codeTheme,
}: {
initialContent: string
onChange: (value: string) => void
onCreateEditor: (view: EditorView) => void
isRendering: boolean
setIsRendering: (value: boolean) => void
codeTheme?: {
light?: BundledTheme
dark?: BundledTheme
}
}) {
const highlighter = useHighlighter()
const isMobileLayout = useIsMobileLayout()
const t = useTranslations()
@@ -45,11 +53,17 @@ export default function DualColumnEditor({
useDebounceEffect(
() => {
const result = renderPageContent(values)
if (!highlighter) return
const result = renderPageContent({
content: values,
highlighter,
strictMode: true,
codeTheme,
})
setTree(result.tree)
setParsedContent(result)
},
[values],
[values, codeTheme, highlighter],
{
wait: 500,
},
@@ -234,6 +248,8 @@ export default function DualColumnEditor({
onMouseEnter={() => {
setCurrentScrollArea("preview")
}}
highlighter={highlighter}
codeTheme={codeTheme}
/>
)}
</div>
@@ -4,6 +4,7 @@ import dynamic from "next/dynamic"
import { useState } from "react"
import { useDate } from "~/hooks/useDate"
import { useHighlighter } from "~/hooks/useHighlighter"
import { RESERVED_TAGS } from "~/lib/constants"
import { cn } from "~/lib/utils"
@@ -18,6 +19,7 @@ export const ImportPreview = ({ note }: { note: NoteMetadata }) => {
const date = useDate()
const [showcaseMore, setShowcaseMore] = useState(false)
const t = useTranslations()
const highlighter = useHighlighter()
return (
<article className="border rounded-xl p-6 mt-4">
@@ -60,6 +62,7 @@ export const ImportPreview = ({ note }: { note: NoteMetadata }) => {
<DynamicMarkdownContent
className="mt-4"
content={note?.content}
highlighter={highlighter}
></DynamicMarkdownContent>
</div>
</article>
+2 -2
View File
@@ -2,7 +2,7 @@ import { getTranslations } from "next-intl/server"
import Script from "next/script"
import { Logo } from "~/components/common/Logo"
import MarkdownContent from "~/components/common/MarkdownContent"
import MarkdownContentServer from "~/components/common/MarkdownContentServer"
import { SITE_URL } from "~/lib/env"
import { ExpandedCharacter } from "~/lib/types"
@@ -35,7 +35,7 @@ export default async function SiteFooter({
<footer className="text-zinc-500 border-t">
<div className="max-w-screen-lg mx-auto px-5 py-10">
{site?.metadata?.content?.footer && (
<MarkdownContent
<MarkdownContentServer
className="[&>.prose]:text-sm"
content={site.metadata.content.footer}
strictMode={true}