Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 905f700

Browse files
committed
Add syntax highlighting
1 parent f4c3a67 commit 905f700

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"next-themes": "0.0.11",
3232
"nightwind": "1.1.6",
3333
"phosphor-react": "1.1.2",
34+
"prism-react-renderer": "1.2.0",
3435
"react": "17.0.1",
3536
"react-dom": "17.0.1",
3637
"react-hook-form": "6.15.4",

src/components/Code.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Highlight, { defaultProps, Language } from 'prism-react-renderer'
2+
import theme from 'prism-react-renderer/themes/nightOwl'
3+
4+
const aliasesMap: Record<Language, string[]> = {
5+
markup: [],
6+
bash: ['sh', 'shellscript'],
7+
clike: [],
8+
c: [],
9+
cpp: [],
10+
css: [],
11+
javascript: ['js'],
12+
jsx: [],
13+
coffeescript: [],
14+
actionscript: [],
15+
'css-extr': [],
16+
diff: [],
17+
git: [],
18+
go: [],
19+
graphql: [],
20+
handlebars: [],
21+
json: [],
22+
less: [],
23+
makefile: [],
24+
markdown: [],
25+
objectivec: [],
26+
ocaml: [],
27+
python: ['py', 'py3'],
28+
reason: [],
29+
sass: [],
30+
scss: [],
31+
sql: [],
32+
stylus: [],
33+
tsx: [],
34+
typescript: ['ts'],
35+
wasm: [],
36+
yaml: [],
37+
}
38+
39+
function Code({
40+
children,
41+
className,
42+
}: {
43+
children: string
44+
className: string
45+
}) {
46+
let languageOfCode = (className?.split('-')?.[1] ?? '') as any
47+
const languages = Object.keys(aliasesMap) as Array<keyof typeof aliasesMap>
48+
if (!languages.includes(languageOfCode)) {
49+
for (const lang of languages) {
50+
const aliases = aliasesMap[lang]
51+
const match = aliases.find(
52+
// eslint-disable-next-line no-loop-func
53+
(language) => language === languageOfCode
54+
)
55+
if (match) {
56+
languageOfCode = lang
57+
break
58+
}
59+
}
60+
}
61+
62+
return (
63+
<Highlight
64+
{...defaultProps}
65+
theme={theme}
66+
code={children}
67+
language={languageOfCode}
68+
>
69+
{({ className, style, tokens, getLineProps, getTokenProps }) => (
70+
<pre className={className} style={style}>
71+
<code>
72+
{tokens.map((line, i) => (
73+
<div {...getLineProps({ line, key: i })}>
74+
{line.map((token, key) => (
75+
<span {...getTokenProps({ token, key })} />
76+
))}
77+
</div>
78+
))}
79+
</code>
80+
</pre>
81+
)}
82+
</Highlight>
83+
)
84+
}
85+
86+
export function Pre({ children }) {
87+
const props = children.props
88+
return <Code {...props}></Code>
89+
}

src/components/Markdown.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import MarkdownOriginal, { MarkdownToJSX } from 'markdown-to-jsx'
22
import React from 'react'
3-
import { A } from '.'
3+
import { A, Pre } from '@/components'
44

55
export default function Markdown(props: {
66
[key: string]: any
@@ -18,6 +18,9 @@ export default function Markdown(props: {
1818
a: {
1919
component: A,
2020
},
21+
pre: {
22+
component: Pre,
23+
},
2124
},
2225
}}
2326
/>

src/components/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export { default as Markdown } from './Markdown'
1111
export { default as HomePageFeed } from './HomePageFeed'
1212
export { default as Hero } from './Hero'
1313
export * from './modal'
14+
export { Pre } from './Code'

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,6 +4643,11 @@ pretty-hrtime@^1.0.3:
46434643
resolved "https://registry.yarnpkg.com/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz#b7e3ea42435a4c9b2759d99e0f201eb195802ee1"
46444644
integrity sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=
46454645

4646+
4647+
version "1.2.0"
4648+
resolved "https://registry.yarnpkg.com/prism-react-renderer/-/prism-react-renderer-1.2.0.tgz#5ad4f90c3e447069426c8a53a0eafde60909cdf4"
4649+
integrity sha512-GHqzxLYImx1iKN1jJURcuRoA/0ygCcNhfGw1IT8nPIMzarmKQ3Nc+JcG0gi8JXQzuh0C5ShE4npMIoqNin40hg==
4650+
46464651
process-nextick-args@~2.0.0:
46474652
version "2.0.1"
46484653
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"

0 commit comments

Comments
 (0)