Skip to content

Commit e8fb27e

Browse files
committed
refactor: migrate to react-syntax-highlighter
- maintained library - enables SSR syntax highlighting - fixes syntax highlighting for alarm-clock - easier to customize (e.g. add line markers)
1 parent 52bd2e4 commit e8fb27e

File tree

4 files changed

+152
-34
lines changed

4 files changed

+152
-34
lines changed

components/annotated-source.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { IGACAnnotation } from '../services/gac-annotations';
2-
import Highlight from 'react-highlight';
3-
import { useRef, useState, useMemo, useLayoutEffect } from 'react';
1+
import { useLayoutEffect, useMemo, useRef, useState } from 'react';
42
import ReactMarkdown from 'react-markdown/with-html';
3+
import SyntaxHighlighter from 'react-syntax-highlighter';
4+
import { arduinoLight } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
5+
import { IGACAnnotation } from '../services/gac-annotations';
56

67
interface IAnnotatedSourceProps {
78
code: string;
@@ -39,7 +40,6 @@ export function AnnotatedSource({ code, annotations }: IAnnotatedSourceProps) {
3940

4041
const [activeAnnotation, setActiveAnnotation] = useState<IGACAnnotation | null>(null);
4142

42-
const highlightedCode = useMemo(() => <Highlight>{code}</Highlight>, [code]);
4343
const annotatedLines = useMemo(() => {
4444
const result = new Map<number, IGACAnnotation>();
4545
for (const annotation of annotations) {
@@ -112,7 +112,17 @@ export function AnnotatedSource({ code, annotations }: IAnnotatedSourceProps) {
112112
<div className="annotation-markers" ref={markersRef}>
113113
<AnnotationMarkers annotations={annotations} />
114114
</div>
115-
{highlightedCode}
115+
<SyntaxHighlighter
116+
language="arduino"
117+
style={arduinoLight}
118+
customStyle={{
119+
marginTop: 0,
120+
padding: 0,
121+
background: 'transparent',
122+
}}
123+
>
124+
{code}
125+
</SyntaxHighlighter>
116126
{activeAnnotation && (
117127
<div
118128
className="annotation-info"
@@ -133,13 +143,6 @@ export function AnnotatedSource({ code, annotations }: IAnnotatedSourceProps) {
133143
position: relative;
134144
display: flex;
135145
}
136-
.code-box > :global(pre) {
137-
margin-top: 0;
138-
}
139-
.code-box > :global(pre > code) {
140-
background: transparent;
141-
padding: 0;
142-
}
143146
144147
.annotation-markers {
145148
width: 8px;

package-lock.json

Lines changed: 135 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"puppeteer": "^5.2.1",
1818
"react": "16.13.1",
1919
"react-dom": "16.13.1",
20-
"react-highlight": "^0.12.0",
2120
"react-markdown": "^4.3.1",
2221
"react-scrollspy": "^3.4.3",
22+
"react-syntax-highlighter": "^13.2.1",
2323
"remark-parse": "^8.0.3",
2424
"unified": "^9.1.0",
2525
"zip-stream": "^4.0.2"
@@ -29,8 +29,8 @@
2929
"@types/node": "^14.0.27",
3030
"@types/puppeteer": "^3.0.1",
3131
"@types/react": "^16.9.43",
32-
"@types/react-highlight": "^0.12.2",
3332
"@types/react-scrollspy": "^3.3.2",
33+
"@types/react-syntax-highlighter": "^11.0.4",
3434
"@types/react-test-renderer": "^16.9.2",
3535
"@typescript-eslint/eslint-plugin": "^3.7.0",
3636
"@typescript-eslint/parser": "^3.7.0",

pages/_app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { AppPropsType } from 'next/dist/next-server/lib/utils';
2-
import '../node_modules/highlight.js/styles/arduino-light.css';
32
import 'github-markdown-css';
43
import { GoogleAnalyticsScript } from '../components/google-analytics';
54
import Head from 'next/head';

0 commit comments

Comments
 (0)