Skip to content

Commit

Permalink
Simplify the syntax highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
dguo committed Feb 23, 2024
1 parent ab783d9 commit fb40783
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions components/SyntaxHighlighter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import {LightAsync} from "react-syntax-highlighter";
import {a11yDark} from "react-syntax-highlighter/dist/cjs/styles/hljs";

function SyntaxHighlighter(props) {
const {children, className, node, ...rest} = props;
const match = /language-(\w+)/.exec(className || "");

const {children, className} = props;
if (!children) {
return null;
}

// An example className is "language-python"
const languageMatch = /language-(\w+)/.exec(className || "");

return (
<LightAsync
{...rest}
PreTag="div"
children={String(children).replace(/\n$/, "")}
language={match?.[1] ?? "txt"}
children={children}
language={languageMatch?.[1] ?? "txt"}
style={a11yDark}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions components/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactMarkdown from "react-markdown";
import TextareaAutosize from "react-textarea-autosize";
import SyntaxHighlighter from "./SyntaxHighlighter";

const initialTemplate = `# Foobar
const INITIAL_TEMPLATE = `# Foobar
Foobar is a Python library for dealing with word pluralization.
Expand Down Expand Up @@ -42,7 +42,7 @@ Please make sure to update tests as appropriate.
[MIT](https://choosealicense.com/licenses/mit/)`;

export function Template() {
const [template, setTemplate] = useState(initialTemplate);
const [template, setTemplate] = useState(INITIAL_TEMPLATE);

return (
<section id="template" className="bg-medium-gray pb-10">
Expand Down

0 comments on commit fb40783

Please sign in to comment.