Skip to content

Commit

Permalink
fix: remove shiki vercel not support this
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jul 8, 2023
1 parent dd9619f commit dbf1dd4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 16 deletions.
75 changes: 60 additions & 15 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, useLayoutEffect, useRef } from 'react'
import React, { useCallback, useInsertionEffect, 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,25 +24,70 @@ export const HighLighter: FC<Props> = (props) => {

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

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

useLayoutEffect(() => {
;(async () => {
const html = await renderCodeHighlighter(
value,
language as string,
isPrintMode ? 'github-light' : isDark ? 'github-dark' : 'github-light',
)
if (!ref.current) {
return
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()
}
ref.current.innerHTML = html
})()
}, [isDark, value, language, isPrintMode])
}

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',
),
]),
)
.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()
})
})
}
})
}, [])

const ref = useRef<HTMLElement>(null)
return (
Expand Down
68 changes: 68 additions & 0 deletions src/components/ui/code-highlighter/CodeHighlighter.tsx.backup_
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 { toast } from '~/lib/toast'

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

declare global {
interface Window {
Prism: any
}
}

interface Props {
lang: string | undefined
content: string
}

export const HighLighter: FC<Props> = (props) => {
const { lang: language, content: value } = props

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

const isPrintMode = useIsPrintMode()
const isDark = useIsDark()

useLayoutEffect(() => {
;(async () => {
const html = await renderCodeHighlighter(
value,
language as string,
isPrintMode ? 'github-light' : isDark ? 'github-dark' : 'github-light',
)
if (!ref.current) {
return
}
ref.current.innerHTML = html
})()
}, [isDark, value, language, isPrintMode])

const ref = useRef<HTMLElement>(null)
return (
<div className={styles['code-wrap']}>
<span className={styles['language-tip']} aria-hidden>
{language?.toUpperCase()}
</span>

<pre className="line-numbers !bg-transparent" data-start="1">
<code
className={`language-${language ?? 'markup'} !bg-transparent`}
ref={ref}
>
{value}
</code>
</pre>

<div className={styles['copy-tip']} onClick={handleCopy} aria-hidden>
Copy
</div>
</div>
)
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
".next/types/**/*.ts",
],
"exclude": [
"node_modules",
Expand Down

1 comment on commit dbf1dd4

@vercel
Copy link

@vercel vercel bot commented on dbf1dd4 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-innei.vercel.app
innei.in
shiro-git-main-innei.vercel.app

Please sign in to comment.