Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5899af6

Browse files
committedNov 7, 2024
Added code highlighter
1 parent b1e7c4c commit 5899af6

File tree

6 files changed

+613
-254
lines changed

6 files changed

+613
-254
lines changed
 

‎package-lock.json

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

‎package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13+
"prismjs": "^1.29.0",
1314
"react": "^18.3.1",
1415
"react-dom": "^18.3.1",
15-
"react-router-dom": "^6.27.0"
16+
"react-router-dom": "^6.27.0",
17+
"react-syntax-highlighter": "^15.6.1"
1618
},
1719
"devDependencies": {
1820
"@eslint/js": "^9.11.1",
1921
"@types/react": "^18.3.10",
2022
"@types/react-dom": "^18.3.0",
23+
"@types/react-syntax-highlighter": "^15.5.13",
2124
"@vitejs/plugin-react-swc": "^3.5.0",
2225
"eslint": "^9.11.1",
2326
"eslint-plugin-react-hooks": "^5.1.0-rc.0",

‎src/components/CodePreview.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
2+
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
3+
4+
type CodePreviewProps = {
5+
language?: string;
6+
children: string;
7+
};
8+
9+
const CodePreview = ({ language, children }: CodePreviewProps) => {
10+
if (!language) {
11+
language = "markdown"; // for fallback
12+
}
13+
14+
return (
15+
<SyntaxHighlighter
16+
language={language}
17+
style={oneDark}
18+
wrapLines={true}
19+
customStyle={{ background: "transparent" }}
20+
>
21+
{children}
22+
</SyntaxHighlighter>
23+
);
24+
};
25+
26+
export default CodePreview;

‎src/components/SnippetCard.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import { CopyIcon, ExpandIcon } from "./Icons";
44
import slugify from "../utils/slugify";
55
import Button from "./Button";
66

7-
const SnippetCard = ({ title, language, category }: SnippetCardProps) => {
7+
const SnippetCard = ({ title, language, category, icon }: SnippetCardProps) => {
88
return (
99
<li className="snippet">
1010
<div className="snippet__preview">
1111
<Button isIcon={true} className="snippet__copy">
1212
<CopyIcon />
1313
</Button>
1414
</div>
15+
1516
<div className="snippet__content">
1617
<h3 className="snippet__title">{title}</h3>
17-
<Link
18-
to={`/${slugify(language)}/${slugify(category)}/${slugify(title)}`}
19-
>
18+
<Link to={`/${language}/${category}/${slugify(title)}`}>
2019
<ExpandIcon />
2120
</Link>
2221
</div>

‎src/components/SnippetModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LanguageData, SnippetType } from "../types";
55
import { getSnippetByTitleAndCategory } from "../utils/filters";
66
import { CloseIcon } from "./Icons";
77
import Button from "./Button";
8+
import CodePreview from "./CodePreview";
89

910
const SnippetModal = () => {
1011
const modalRoot = document.getElementById("modal-root");
@@ -51,7 +52,7 @@ const SnippetModal = () => {
5152
</Button>
5253
</div>
5354
<div className="code-preview">
54-
<pre>{snippet.code}</pre>
55+
<CodePreview language={language}>{snippet.code}</CodePreview>
5556
</div>
5657
<p>
5758
<b>Description: </b>

‎src/styles/main.css

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ ol:where([role="list"]) {
253253
}
254254
}
255255

256+
.code-preview {
257+
border: 1px solid var(--border-color);
258+
border-radius: var(--br-md);
259+
width: 100%;
260+
overflow-x: auto;
261+
height: 100%;
262+
}
263+
256264
/*------------------------------------*\
257265
#HEADER
258266
\*------------------------------------*/
@@ -362,11 +370,15 @@ ol:where([role="list"]) {
362370

363371
.snippet__preview {
364372
width: 100%;
373+
overflow: hidden;
365374
aspect-ratio: 16 / 9;
366375
background-image: var(--gradient-secondary);
367376
border: 1px solid var(--border-color);
368377
border-radius: var(--br-md);
369378
position: relative;
379+
padding-inline: 1em;
380+
display: grid;
381+
place-items: center;
370382
}
371383

372384
.snippet__copy {
@@ -407,8 +419,8 @@ ol:where([role="list"]) {
407419
.modal {
408420
background-color: var(--bg-secondary);
409421
padding: 2rem;
410-
width: 80%;
411-
max-width: 600px;
422+
width: 90%;
423+
max-width: 800px;
412424
border-radius: var(--br-lg);
413425
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
414426
position: relative;
@@ -422,12 +434,6 @@ ol:where([role="list"]) {
422434
gap: 1rem;
423435
}
424436

425-
.code-preview {
426-
background-color: var(--bg-primary);
427-
padding: 0.5em 1em;
428-
border-radius: var(--br-md);
429-
}
430-
431437
.modal__tags {
432438
display: flex;
433439
gap: 1em;

0 commit comments

Comments
 (0)
Please sign in to comment.