Connect, collaborate, and grow with a community of TypeScript developers
Don't render html comments (#197)
Sean Boult57bbf0881b5f
diff --git a/package.json b/package.jsonindex 7e4caf77..62ea8074 100644--- a/package.json+++ b/package.json@@ -69,6 +69,8 @@"tailwind-merge": "^1.12.0","tailwindcss-animate": "^1.0.5","typescript": "5.1.3",+ "unified": "^10.1.2",+ "unist-util-visit": "^5.0.0","uuid-by-string": "^4.0.0","zod": "^3.21.4","zustand": "^4.3.8"diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4e3690e7..23e7d134 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -166,6 +166,12 @@ dependencies:typescript:specifier: 5.1.3version: 5.1.3+ unified:+ specifier: ^10.1.2+ version: 10.1.2+ unist-util-visit:+ specifier: ^5.0.0+ version: 5.0.0uuid-by-string:specifier: ^4.0.0version: 4.0.0@@ -2158,7 +2164,7 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-wLEm0QvaoawEDoTRwzTXp4b4jpwiJDvR5KMnFnVodm3scufTlBOWRD6N1OBf9TZMhjlNsSfcO5V+7AF4+Vy+9g==}dependencies:- '@types/unist': 2.0.6+ '@types/unist': 3.0.0dev: false/@types/[email protected]:@@ -2180,7 +2186,7 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-Y/uImid8aAwrEA24/1tcRZwpxX3pIFTSilcNDKSPn+Y2iDywSEachzRuvgAYYLR3wpGXAsMbv5lvKLDZLeYPAw==}dependencies:- '@types/unist': 2.0.6+ '@types/unist': 3.0.0dev: false/@types/[email protected]:@@ -2275,6 +2281,10 @@ packages:resolution: {integrity: sha512-PBjIUxZHOuj0R15/xuwJYjFi+KZdNFrehocChv4g5hu6aFroHue8m0lBP0POdK2nKzbw0cgV1mws8+V/JAcEkQ==}dev: false+ /@types/[email protected]:+ resolution: {integrity: sha512-MFETx3tbTjE7Uk6vvnWINA/1iJ7LuMdO4fcq8UfF0pRbj01aGLduVvQcRyswuACJdpnHgg8E3rQLhaRdNEJS0w==}+ dev: false+/@typescript-eslint/[email protected](@typescript-eslint/[email protected])([email protected])([email protected]):resolution: {integrity: sha512-sXtOgJNEuRU5RLwPUb1jxtToZbgvq3M6FPpY4QENxoOggK+UpTxUBpj6tD8+Qh2g46Pi9We87E+eHnUw8YcGsw==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}@@ -6755,6 +6765,12 @@ packages:'@types/unist': 2.0.6dev: false+ resolution: {integrity: sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==}+ dependencies:+ '@types/unist': 3.0.0+ dev: false+resolution: {integrity: sha512-kUBE91efOWfIVBo8xzh/uZQ7p9ffYRtUbMRZBNFYwf0RK8koUMx6dGUfwylLOKmaT2cs4wSW96QoYUSXAyEtpg==}dependencies:@@ -6774,6 +6790,13 @@ packages:unist-util-is: 5.2.1dev: false+ resolution: {integrity: sha512-L/PqWzfTP9lzzEa6CKs0k2nARxTdZduw3zyh8d2NVBnsyvHjSX4TWse388YrrQKbvI8w20fGjGlhgT96WwKykw==}+ dependencies:+ '@types/unist': 3.0.0+ unist-util-is: 6.0.0+ dev: false+resolution: {integrity: sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==}dependencies:@@ -6782,6 +6805,14 @@ packages:unist-util-visit-parents: 5.1.3dev: false+ resolution: {integrity: sha512-MR04uvD+07cwl/yhVuVWAtw+3GOR/knlL55Nd/wAdblk27GCVt3lqpTivy/tkJcZoNPzTwS1Y+KMojlLDhoTzg==}+ dependencies:+ '@types/unist': 3.0.0+ unist-util-is: 6.0.0+ unist-util-visit-parents: 6.0.1+ dev: false+resolution: {integrity: sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==}engines: {node: '>=8'}diff --git a/src/components/ui/markdown.tsx b/src/components/ui/markdown.tsxindex 64ec8793..bf3aeff3 100644--- a/src/components/ui/markdown.tsx+++ b/src/components/ui/markdown.tsx@@ -5,11 +5,39 @@ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';import remarkGfm from 'remark-gfm';import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';import clsx from 'clsx';+import { visit, SKIP, type BuildVisitor } from 'unist-util-visit';+import type { Transformer } from 'unified';++const HTML_COMMENT_REGEX = new RegExp('<!--([\\s\\S]*?)-->', 'g');++/**+ * Remove HTML comments from Markdown+ */+function removeHtmlComments(): Transformer {+ return (tree) => {+ // TODO: PRs are welcomed to fix the any type+ const handler: BuildVisitor<any> = (node, index, parent) => {+ const isComment = node.value.match(HTML_COMMENT_REGEX);++ if (isComment) {+ // remove node+ parent.children.splice(index, 1);+ // Do not traverse `node`, continue at the node *now* at `index`. http://unifiedjs.com/learn/recipe/remove-node/+ return [SKIP, index];+ }+ };++ visit(tree, 'html', handler);++ // TODO: is this needed+ visit(tree, 'jsx', handler);+ };+}export function Markdown({ children }: { children: string }) {return (<ReactMarkdown- remarkPlugins={[remarkGfm]}+ remarkPlugins={[remarkGfm, removeHtmlComments]}components={{h1: ({ className, ...props }) => (<h1 className={clsx(className, 'mb-2 pb-2 text-3xl font-bold')} {...props} />