diff --git a/web/package.json b/web/package.json index 89716fe21..55c1bc26d 100644 --- a/web/package.json +++ b/web/package.json @@ -13,6 +13,7 @@ "copy-to-clipboard": "^3.3.2", "dayjs": "^1.11.3", "emoji-picker-react": "^3.6.2", + "highlight.js": "^11.6.0", "i18next": "^21.9.2", "lodash-es": "^4.17.21", "qs": "^6.11.0", diff --git a/web/src/labs/marked/marked.test.ts b/web/src/labs/marked/marked.test.ts index 9581fd3ca..2f649f16a 100644 --- a/web/src/labs/marked/marked.test.ts +++ b/web/src/labs/marked/marked.test.ts @@ -9,9 +9,8 @@ describe("test marked parser", () => { markdown: `\`\`\` hello world! \`\`\``, - want: `
-hello world! -`, + want: `
hello world!
+
`,
},
{
markdown: `test code block
@@ -21,9 +20,8 @@ console.log("hello world!")
\`\`\``,
want: `test code block
-
-console.log("hello world!")
-
`,
+console.log("hello world!")
+
`,
},
];
diff --git a/web/src/labs/marked/parser/CodeBlock.ts b/web/src/labs/marked/parser/CodeBlock.ts
index 8eaee10e2..ca5afb3a8 100644
--- a/web/src/labs/marked/parser/CodeBlock.ts
+++ b/web/src/labs/marked/parser/CodeBlock.ts
@@ -1,4 +1,5 @@
import { escape } from "lodash-es";
+import hljs from "highlight.js";
export const CODE_BLOCK_REG = /^```(\S*?)\s([\s\S]*?)```(\n?)/;
@@ -8,7 +9,12 @@ const renderer = (rawStr: string): string => {
return rawStr;
}
- return `\n${escape(matchResult[2])}
${matchResult[3]}`;
+ const language = escape(matchResult[1]) || "plaintext";
+ const highlightedCodes = hljs.highlight(matchResult[2], {
+ language,
+ }).value;
+
+ return `${highlightedCodes}
${matchResult[3]}`;
};
export default {
diff --git a/web/src/less/memo-content.less b/web/src/less/memo-content.less
index b862c162c..d5c4b0a1f 100644
--- a/web/src/less/memo-content.less
+++ b/web/src/less/memo-content.less
@@ -19,7 +19,7 @@
}
.img {
- @apply float-left max-w-full;
+ @apply float-left max-w-full rounded cursor-pointer hover:shadow;
}
.tag-span {
@@ -48,7 +48,11 @@
}
pre {
- @apply w-full my-1 py-2 px-3 rounded text-sm bg-gray-100 whitespace-pre-wrap;
+ @apply w-full my-1 p-3 rounded bg-gray-100 whitespace-pre-wrap;
+
+ code {
+ @apply block;
+ }
}
code {
diff --git a/web/src/main.tsx b/web/src/main.tsx
index 8a1b1e5f1..dca35b18d 100644
--- a/web/src/main.tsx
+++ b/web/src/main.tsx
@@ -4,6 +4,7 @@ import store from "./store";
import App from "./App";
import "./i18n";
import "./helpers/polyfill";
+import "highlight.js/styles/github.css";
import "./less/global.less";
import "./css/index.css";
diff --git a/web/yarn.lock b/web/yarn.lock
index b16ebd61a..4c17f81cd 100644
--- a/web/yarn.lock
+++ b/web/yarn.lock
@@ -2160,6 +2160,11 @@ has@^1.0.3:
dependencies:
function-bind "^1.1.1"
+highlight.js@^11.6.0:
+ version "11.6.0"
+ resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.6.0.tgz#a50e9da05763f1bb0c1322c8f4f755242cff3f5a"
+ integrity sha512-ig1eqDzJaB0pqEvlPVIpSSyMaO92bH1N2rJpLMN/nX396wTpDA4Eq0uK+7I/2XG17pFaaKE0kjV/XPeGt7Evjw==
+
hoist-non-react-statics@^3.3.0, hoist-non-react-statics@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"