Skip to content

Commit 29f8cc6

Browse files
author
ACR1209
committed
Add extension to the snippet to help rendering with highlights
1 parent 10e8791 commit 29f8cc6

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

src/components/SnippetList.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import { SnippetType } from "@types";
88
import { LeftAngleArrowIcon } from "./Icons";
99
import SnippetModal from "./SnippetModal";
1010

11-
const extensions: { [key: string]: string } = {
12-
REACT: "tsx",
13-
FASTAPI: "py",
14-
};
15-
1611
const SnippetList = () => {
1712
const { language, snippet, setSnippet } = useAppContext();
1813
const { fetchedSnippets } = useSnippets();
@@ -92,7 +87,7 @@ const SnippetList = () => {
9287
<SnippetModal
9388
snippet={snippet}
9489
handleCloseModal={handleCloseModal}
95-
language={extensions[language.name] || language.name}
90+
language={snippet.extension}
9691
/>
9792
)}
9893
</AnimatePresence>

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type SnippetType = {
1919
code: string;
2020
tags: string[];
2121
author: string;
22+
extension: string;
2223
};
2324

2425
export type AppState = {

utils/snippetParser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function parseSnippet(path, name, text) {
5555
match = headerEndCodeStartRegex.exec(fromCursor());
5656
if(match === null) return raise('Missing header end \'---\' or code start \'```\'', path);
5757
cursor += match[0].length;
58+
const extension = match[0].replace(/[\r\n`-]/g, '');
5859

5960
match = codeRegex.exec(fromCursor());
6061
if(match === null) return raise('Missing code block end \'```\'', path);
@@ -67,6 +68,7 @@ function parseSnippet(path, name, text) {
6768
tags: properties.tags.split(',').map((tag) => tag.trim()).filter((tag) => tag),
6869
contributors: 'contributors' in properties ? properties.contributors.split(',').map((contributor) => contributor.trim()).filter((contributor) => contributor) : [],
6970
code: code.replace(/\r\n/g, '\n'),
71+
extension
7072
}
7173
}
7274

0 commit comments

Comments
 (0)