Skip to content

Commit

Permalink
fix: #289 use highlight.js instead of prism
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Apr 2, 2023
1 parent 7b5af27 commit 4f0108b
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 128 deletions.
38 changes: 35 additions & 3 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import RemarkMath from "remark-math";
import RemarkBreaks from "remark-breaks";
import RehypeKatex from "rehype-katex";
import RemarkGfm from "remark-gfm";
import RehypePrsim from "rehype-prism-plus";
import { useRef } from "react";
import RehypeHighlight from "rehype-highlight";
import { useRef, useState, RefObject, useEffect } from "react";
import { copyToClipboard } from "../utils";

export function PreCode(props: { children: any }) {
Expand All @@ -27,11 +27,43 @@ export function PreCode(props: { children: any }) {
);
}

const useLazyLoad = (ref: RefObject<Element>): boolean => {
const [isIntersecting, setIntersecting] = useState<boolean>(false);

useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
setIntersecting(true);
observer.disconnect();
}
});

if (ref.current) {
observer.observe(ref.current);
}

return () => {
observer.disconnect();
};
}, [ref]);

return isIntersecting;
};

export function Markdown(props: { content: string }) {
return (
<ReactMarkdown
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
rehypePlugins={[RehypeKatex, [RehypePrsim, { ignoreMissing: true }]]}
rehypePlugins={[
RehypeKatex,
[
RehypeHighlight,
{
detect: true,
ignoreMissing: true,
},
],
]}
components={{
pre: PreCode,
}}
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @next/next/no-page-custom-font */
import "./styles/globals.scss";
import "./styles/markdown.scss";
import "./styles/prism.scss";
import "./styles/highlight.scss";
import process from "child_process";
import { ACCESS_CODES, IS_IN_DOCKER } from "./api/access";

Expand Down
1 change: 1 addition & 0 deletions app/styles/globals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pre {
.copy-code-button {
position: absolute;
right: 10px;
top: 1em;
cursor: pointer;
padding: 0px 5px;
background-color: var(--black);
Expand Down
114 changes: 114 additions & 0 deletions app/styles/highlight.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
.markdown-body {
pre {
padding: 0;
}

pre,
code {
font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
}

pre code.hljs {
display: block;
overflow-x: auto;
padding: 1em;
}

code.hljs {
padding: 3px 5px;
}

/*!
Theme: Tokyo-night-Dark
origin: https://github.com/enkia/tokyo-night-vscode-theme
Description: Original highlight.js style
Author: (c) Henri Vandersleyen <[email protected]>
License: see project LICENSE
Touched: 2022
*/
.hljs-comment,
.hljs-meta {
color: #565f89;
}

.hljs-deletion,
.hljs-doctag,
.hljs-regexp,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-id,
.hljs-selector-pseudo,
.hljs-tag,
.hljs-template-tag,
.hljs-variable.language_ {
color: #f7768e;
}

.hljs-link,
.hljs-literal,
.hljs-number,
.hljs-params,
.hljs-template-variable,
.hljs-type,
.hljs-variable {
color: #ff9e64;
}

.hljs-attribute,
.hljs-built_in {
color: #e0af68;
}

.hljs-keyword,
.hljs-property,
.hljs-subst,
.hljs-title,
.hljs-title.class_,
.hljs-title.class_.inherited__,
.hljs-title.function_ {
color: #7dcfff;
}

.hljs-selector-tag {
color: #73daca;
}

.hljs-addition,
.hljs-bullet,
.hljs-quote,
.hljs-string,
.hljs-symbol {
color: #9ece6a;
}

.hljs-code,
.hljs-formula,
.hljs-section {
color: #7aa2f7;
}

.hljs-attr,
.hljs-char.escape_,
.hljs-keyword,
.hljs-name,
.hljs-operator {
color: #bb9af7;
}

.hljs-punctuation {
color: #c0caf5;
}

.hljs {
background: #1a1b26;
color: #9aa5ce;
}

.hljs-emphasis {
font-style: italic;
}

.hljs-strong {
font-weight: 700;
}
}
122 changes: 0 additions & 122 deletions app/styles/prism.scss

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^8.0.5",
"rehype-highlight": "^6.0.0",
"rehype-katex": "^6.0.2",
"rehype-prism-plus": "^1.5.1",
"remark-breaks": "^3.0.2",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
Expand Down
Loading

0 comments on commit 4f0108b

Please sign in to comment.