Skip to content

Commit

Permalink
support mermaid diagrams in issue comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ghackenberg committed Dec 1, 2023
1 parent 76370ee commit 16e6384
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"recharts": "^2.1.9",
"rehype-mermaid": "^2.0.1",
"rehype-react": "^8.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export const CommentView = (props: { productId: string, issueId: string, comment
const initialTextView = comment && comment.text
const initialTextEdit = ''

const initialHtmlView = initialTextView && createProcessor(over, out, handleClick).processSync(initialTextView).result
const initialHtmlEdit = createProcessor(over, out, handleClick).processSync(initialTextEdit).result
const initialHtmlView = initialTextView && createProcessor(over, out, handleClick, false).processSync(initialTextView).result
const initialHtmlEdit = createProcessor(over, out, handleClick, false).processSync(initialTextEdit).result

const initialPartsView = initialTextView && collectParts(initialTextView)
const initialPartsEdit = collectParts(initialTextEdit)
Expand Down Expand Up @@ -126,7 +126,7 @@ export const CommentView = (props: { productId: string, issueId: string, comment

React.useEffect(() => {
if (textView) {
setHtmlView(createProcessor(over, out, handleClick).processSync(textView).result)
createProcessor(over, out, handleClick, true).process(textView).then(vfile => setTimeout(() => setHtmlView(vfile.result), 0))
setPartsView(collectParts(textView))
} else {
setPartsView(undefined)
Expand Down Expand Up @@ -218,7 +218,7 @@ export const CommentView = (props: { productId: string, issueId: string, comment

async function handlePreview() {
if (textEdit) {
setHtmlEdit(createProcessor(over, out, handleClick).processSync(textEdit).result)
createProcessor(over, out, handleClick, true).process(textEdit).then(vfile => setHtmlEdit(vfile.result))
setMode(Mode.PREVIEW)
} else {
alert('Please enter some text!')
Expand Down
10 changes: 8 additions & 2 deletions packages/frontend/src/scripts/functions/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import { MouseEvent } from "react"

import * as jsxRuntime from 'react/jsx-runtime'
import rehypeMermaid from 'rehype-mermaid'
import rehypeReact, { Options } from "rehype-react"
import remarkParse from "remark-parse"
import remarkRehype from "remark-rehype"
Expand Down Expand Up @@ -64,7 +65,7 @@ function collectPartsInternal(parent: Node, parts: Part[]) {
}
}

export function createProcessor(handleMouseOver: Handler, handleMouseOut: Handler, handleClick: Handler) {
export function createProcessor(handleMouseOver: Handler, handleMouseOut: Handler, handleClick: Handler, mermaid: boolean) {
const options: Options = {
...JSX_RUNTIME,
components: {
Expand Down Expand Up @@ -106,5 +107,10 @@ export function createProcessor(handleMouseOver: Handler, handleMouseOut: Handle
}
}
}
return unified().use(remarkParse).use(remarkRehype).use(rehypeReact, options)
const rehype = unified().use(remarkParse).use(remarkRehype)
if (mermaid) {
return rehype.use(rehypeMermaid).use(rehypeReact, options)
} else {
return rehype.use(rehypeReact, options)
}
}

0 comments on commit 16e6384

Please sign in to comment.