Skip to content

Commit

Permalink
feat: shiki
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 8, 2023
1 parent 6da7851 commit 6e4b5e9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 62 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ A theme for [Mix Space](https://github.com/mx-space)

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FInnei%2FShiro&env=NEXT_PUBLIC_GATEWAY_URL,NEXT_PUBLIC_API_URL,NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,CLERK_SECRET_KEY&project-name=shiro&demo-title=%E9%9D%99%E3%81%8B%E3%81%AA%E6%A3%AE&demo-description=Innei's%20site%20using%20Shiro&demo-url=https%3A%2F%2Finnei.in)

Demo: <innei.in>
Demo:

- [innei.in](innei.in)

## License

Expand Down
3 changes: 3 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ let nextConfig = {
},
experimental: {
appDir: true,
serverComponentsExternalPackages: ['shiki', 'vscode-oniguruma'],
serverActions: true,
serverMinification: true,
},

images: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]
},
"scripts": {
"prepare": "pnpm exec simple-git-hooks && test -f .env || cp .env.example .env",
"prepare": "pnpm exec simple-git-hooks && test -f .env || cp .env.template .env",
"start": "npm run dev",
"prebuild": "rimraf .next",
"dev": "cross-env NODE_ENV=development next dev -p 2323",
Expand Down Expand Up @@ -75,6 +75,7 @@
"react-tweet": "3.0.1",
"react-wrap-balancer": "1.0.0",
"remove-markdown": "0.5.0",
"shiki": "0.14.3",
"socket.io-client": "4.7.1",
"tailwind-merge": "1.13.2"
},
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/components/ui/code-highlighter/CodeHighlighter.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

background: transparent !important;
}

:global(.shiki) {
code {
counter-reset: step;
counter-increment: step 0;
}

code .line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
color: rgba(115, 138, 148, 0.4);
}
}
}

.copy-tip {
Expand Down
75 changes: 15 additions & 60 deletions src/components/ui/code-highlighter/CodeHighlighter.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useCallback, useInsertionEffect, useRef } from 'react'
import React, { useCallback, useLayoutEffect, useRef } from 'react'
import type { FC } from 'react'

import { useIsPrintMode } from '~/atoms/css-media'
import { useIsDark } from '~/hooks/common/use-is-dark'
import { loadScript, loadStyleSheet } from '~/lib/load-script'
import { toast } from '~/lib/toast'

import styles from './CodeHighlighter.module.css'
import { renderCodeHighlighter } from './render.server'

declare global {
interface Window {
Expand All @@ -24,70 +24,25 @@ export const HighLighter: FC<Props> = (props) => {

const handleCopy = useCallback(() => {
navigator.clipboard.writeText(value)
toast('COPIED!', 'success')
toast.success('COPIED!')
}, [value])

const prevThemeCSS = useRef<ReturnType<typeof loadStyleSheet>>()
const isPrintMode = useIsPrintMode()
const isDark = useIsDark()

useInsertionEffect(() => {
const css = loadStyleSheet(
`https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism-themes/1.9.0/prism-one-${
isPrintMode ? 'light' : isDark ? 'dark' : 'light'
}.css`,
)

if (prevThemeCSS.current) {
const $prev = prevThemeCSS.current
css.$link.onload = () => {
$prev.remove()
}
}

prevThemeCSS.current = css
}, [isDark, isPrintMode])
useInsertionEffect(() => {
loadStyleSheet(
'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.css',
)

Promise.all([
loadScript(
'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.23.0/components/prism-core.min.js',
),
])
.then(() =>
Promise.all([
loadScript(
'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.23.0/plugins/autoloader/prism-autoloader.min.js',
),
loadScript(
'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.js',
),
]),
useLayoutEffect(() => {
;(async () => {
const html = await renderCodeHighlighter(
value,
language as string,
isPrintMode ? 'github-light' : isDark ? 'github-dark' : 'github-light',
)
.then(() => {
if (ref.current) {
requestAnimationFrame(() => {
window.Prism?.highlightElement(ref.current)

requestAnimationFrame(() => {
window.Prism?.highlightElement(ref.current)
})
})
} else {
requestAnimationFrame(() => {
window.Prism?.highlightAll()
// highlightAll twice

requestAnimationFrame(() => {
window.Prism?.highlightAll()
})
})
}
})
}, [])
if (!ref.current) {
return
}
ref.current.innerHTML = html
})()
}, [isDark, value, language, isPrintMode])

const ref = useRef<HTMLElement>(null)
return (
Expand Down
19 changes: 19 additions & 0 deletions src/components/ui/code-highlighter/render.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use server'

import { getHighlighter } from 'shiki'
import type { Theme } from 'shiki'

export const renderCodeHighlighter = async (
code: string,
lang: string,
theme: Theme,
) => {
return await getHighlighter({
langs: [lang as any],
theme,
}).then((highlighter) => {
return highlighter.codeToHtml(code, {
lang,
})
})
}

1 comment on commit 6e4b5e9

@vercel
Copy link

@vercel vercel bot commented on 6e4b5e9 Jul 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

springtide.vercel.app
shiro-git-main-innei.vercel.app
shiro-innei.vercel.app
innei.in

Please sign in to comment.