-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
konfig-bot
committed
Oct 20, 2023
1 parent
fc68569
commit d6f0b35
Showing
6 changed files
with
121 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { PropsWithChildren } from "react"; | ||
import highlightjs from "highlight.js"; | ||
import "highlight.js/styles/base16/solarized-dark.css"; | ||
|
||
export const MarkdownRenderer: React.FC<PropsWithChildren<{}>> = ({ | ||
children, | ||
}) => { | ||
if (typeof children !== "string") | ||
throw new Error("MarkdownRenderer only accepts string children"); | ||
|
||
// Split the markdown content at code block boundaries | ||
const parts = children.split(/(```[a-z]*\n[\s\S]*?\n```)/); | ||
|
||
// Highlight each part | ||
const highlightedParts = parts.map((part) => { | ||
// Check if the part is a code block | ||
if (part.startsWith("```")) { | ||
const match = part.match(/```([a-z]+)\n([\s\S]*?)\n```/); | ||
if (match) { | ||
const [, lang, code] = match; | ||
return ( | ||
"```" + | ||
lang + | ||
"\n" + | ||
highlightjs.highlight(code, { language: lang }).value + | ||
"\n```" | ||
); | ||
} | ||
} | ||
// Highlight the part as markdown if it's not a code block | ||
return highlightjs.highlight("markdown", part).value; | ||
}); | ||
|
||
// Combine the highlighted parts | ||
const highlightedMarkdown = highlightedParts.join(""); | ||
|
||
return ( | ||
<pre> | ||
<code | ||
className="hljs markdown" | ||
dangerouslySetInnerHTML={{ __html: highlightedMarkdown }} | ||
></code> | ||
</pre> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -815,11 +815,25 @@ | |
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249" | ||
integrity sha512-Nmh0K3iWQJzniTuPRcJn5hxXkfB1T1pgB89SBig5PlJQU5yocazeu4jATJlaA0GYFKWMqDdvYemoSnF2pXgLVA== | ||
|
||
"@types/highlight.js@^10.1.0": | ||
version "10.1.0" | ||
resolved "https://registry.yarnpkg.com/@types/highlight.js/-/highlight.js-10.1.0.tgz#89bb0c202997d7a90a07bd2ec1f7d00c56bb90b4" | ||
integrity sha512-77hF2dGBsOgnvZll1vymYiNUtqJ8cJfXPD6GG/2M0aLRc29PkvB7Au6sIDjIEFcSICBhCh2+Pyq6WSRS7LUm6A== | ||
dependencies: | ||
highlight.js "*" | ||
|
||
"@types/json5@^0.0.29": | ||
version "0.0.29" | ||
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" | ||
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== | ||
|
||
"@types/marked@^6.0.0": | ||
version "6.0.0" | ||
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-6.0.0.tgz#4dbc7df4add578ef87eb6ca2c6927c97b0531abf" | ||
integrity sha512-jmjpa4BwUsmhxcfsgUit/7A9KbrC48Q0q8KvnY107ogcjGgTFDlIL3RpihNpx2Mu1hM4mdFQjoVc4O6JoGKHsA== | ||
dependencies: | ||
marked "*" | ||
|
||
"@types/[email protected]": | ||
version "20.2.5" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.2.5.tgz#26d295f3570323b2837d322180dfbf1ba156fefb" | ||
|
@@ -2277,6 +2291,11 @@ has@^1.0.3: | |
dependencies: | ||
function-bind "^1.1.1" | ||
|
||
highlight.js@*, highlight.js@^11.9.0: | ||
version "11.9.0" | ||
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.9.0.tgz#04ab9ee43b52a41a047432c8103e2158a1b8b5b0" | ||
integrity sha512-fJ7cW7fQGCYAkgv4CPfwFHrfd/cLS4Hau96JuJ+ZTOWhjnhoeN1ub1tFmALm/+lW5z4WCAuAV9bm05AP0mS6Gw== | ||
|
||
hoist-non-react-statics@^3.3.1: | ||
version "3.3.2" | ||
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45" | ||
|
@@ -2729,6 +2748,11 @@ lru-cache@^6.0.0: | |
dependencies: | ||
yallist "^4.0.0" | ||
|
||
marked@*, marked@^9.1.2: | ||
version "9.1.2" | ||
resolved "https://registry.yarnpkg.com/marked/-/marked-9.1.2.tgz#a54ca772d2b5a43de7d8ed40111354b4b7985527" | ||
integrity sha512-qoKMJqK0w6vkLk8+KnKZAH6neUZSNaQqVZ/h2yZ9S7CbLuFHyS2viB0jnqcWF9UKjwsAbMrQtnQhdmdvOVOw9w== | ||
|
||
merge-stream@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60" | ||
|