Skip to content

Commit

Permalink
close #29
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerZANP committed Nov 28, 2023
1 parent b4ff0d7 commit 11ac850
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/web/components/CodePreview/CodePreviewMarked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import markedShakuCodeAnnotate from "marked-shaku-code-annotate";
import { useDeferredValue } from "react";
import { Fetcher } from "../Fetcher";
import styles from "./CodePreview.module.css";
import { sanitize } from "./sanitize";

import { View } from "../bare";

Expand Down Expand Up @@ -30,7 +31,7 @@ const getProcessedResult = (code: string) => {
processedResultStore.set(
key,
// @ts-ignore
new Fetcher(() => marked.parse(code))
new Fetcher(() => marked.parse(code).then(sanitize))
);

if (processedResultStore.size > 5) {
Expand Down
5 changes: 4 additions & 1 deletion examples/web/components/CodePreview/CodePreviewRemark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { remark } from "remark";
import html from "remark-html";
import { remarkShakuCodeAnnotate } from "remark-shaku-code-annotate";
import { Fetcher } from "../Fetcher";
import { sanitize } from "./sanitize";

let fetcher: Fetcher<ReturnType<typeof remark>> | null = null;
const getProcessor = () => {
Expand All @@ -26,7 +27,9 @@ const getProcessedResult = (
if (!processedResultStore.has(key)) {
processedResultStore.set(
key,
new Fetcher(() => processor.process(code).then((data) => data.toString()))
new Fetcher(() =>
processor.process(code).then((data) => sanitize(data.toString()))
)
);

if (processedResultStore.size > 5) {
Expand Down
45 changes: 45 additions & 0 deletions examples/web/components/CodePreview/sanitize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sanitizeHtml from "sanitize-html";

export const sanitize = (html: string) => {
return sanitizeHtml(html, {
allowedTags: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"p",
"b",
"i",
"code",
"em",
"blockquote",
"strong",
"pre",
"a",
"ul",
"ol",
"li",
"span",
"hr",
"br",
"table",
"thead",
"tbody",
"tr",
"th",
"td",
"mark",
"div",
],
allowedAttributes: {
a: ["href", "target"],
span: ["class", "style"],
div: ["class", "style"],
pre: ["class", "style"],
p: ["class", "style"],
mark: ["data-*", "class"],
},
});
};
5 changes: 3 additions & 2 deletions examples/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"@types/mdx": "^2.0.5",
"copy-webpack-plugin": "^11.0.0",
"dom-to-image": "2.6.0",
"migacss": "^0.0.6",
"marked": "^10.0.0",
"marked-shaku-code-annotate": "*",
"migacss": "^0.0.6",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.10.1",
"remark-shaku-code-annotate": "*",
"marked-shaku-code-annotate": "*",
"sanitize-html": "^2.11.0",
"shaku-ui": "*",
"shiki": "^0.14.5"
},
Expand Down
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 11ac850

@vercel
Copy link

@vercel vercel bot commented on 11ac850 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shaku-web – ./

shaku-web.vercel.app
shaku-web-git-main-jserzanp.vercel.app
shaku-web-jserzanp.vercel.app

Please sign in to comment.