Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Mermaid #7645

Merged
merged 6 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"lucene-query-parser": "^1.2.0",
"markdown-table": "^1.1.1",
"md5": "^2.2.1",
"mermaid": "^10.1.0",
"method-override": "^3.0.0",
"migrate-mongo": "^8.2.3",
"mkdirp": "^1.0.3",
Expand Down
11 changes: 11 additions & 0 deletions apps/app/src/client/services/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import type { Pluggable } from 'unified';

import { DrawioViewerWithEditButton } from '~/components/ReactMarkdownComponents/DrawioViewerWithEditButton';
import { Header } from '~/components/ReactMarkdownComponents/Header';
import { MermaidViewer } from '~/components/ReactMarkdownComponents/MermaidViewer';
import { TableWithEditButton } from '~/components/ReactMarkdownComponents/TableWithEditButton';
import { RehypeSanitizeOption } from '~/interfaces/rehype';
import type { RendererOptions } from '~/interfaces/renderer-options';
import type { RendererConfig } from '~/interfaces/services/renderer';
import * as addLineNumberAttribute from '~/services/renderer/rehype-plugins/add-line-number-attribute';
import * as keywordHighlighter from '~/services/renderer/rehype-plugins/keyword-highlighter';
import * as relocateToc from '~/services/renderer/rehype-plugins/relocate-toc';
import * as mermaid from '~/services/renderer/remark-plugins/mermaid';
import * as plantuml from '~/services/renderer/remark-plugins/plantuml';
import * as xsvToTable from '~/services/renderer/remark-plugins/xsv-to-table';
import {
Expand Down Expand Up @@ -61,6 +63,7 @@ export const generateViewOptions = (
xsvToTable.remarkPlugin,
lsxGrowiPlugin.remarkPlugin,
refsGrowiPlugin.remarkPlugin,
mermaid.remarkPlugin,
);
if (config.isEnabledLinebreaks) {
remarkPlugins.push(breaks);
Expand All @@ -76,6 +79,7 @@ export const generateViewOptions = (
drawioPlugin.sanitizeOption,
lsxGrowiPlugin.sanitizeOption,
refsGrowiPlugin.sanitizeOption,
mermaid.sanitizeOption,
)]
: () => {};

Expand Down Expand Up @@ -105,6 +109,7 @@ export const generateViewOptions = (
components.gallery = refsGrowiPlugin.Gallery;
components.drawio = DrawioViewerWithEditButton;
components.table = TableWithEditButton;
components.mermaid = MermaidViewer;
}

if (config.isEnabledXssPrevention) {
Expand Down Expand Up @@ -164,6 +169,7 @@ export const generateSimpleViewOptions = (
xsvToTable.remarkPlugin,
lsxGrowiPlugin.remarkPlugin,
refsGrowiPlugin.remarkPlugin,
mermaid.remarkPlugin,
);

const isEnabledLinebreaks = overrideIsEnabledLinebreaks ?? config.isEnabledLinebreaks;
Expand All @@ -183,6 +189,7 @@ export const generateSimpleViewOptions = (
drawioPlugin.sanitizeOption,
lsxGrowiPlugin.sanitizeOption,
refsGrowiPlugin.sanitizeOption,
mermaid.sanitizeOption,
)]
: () => {};

Expand All @@ -204,6 +211,7 @@ export const generateSimpleViewOptions = (
components.refsimg = refsGrowiPlugin.RefsImgImmutable;
components.gallery = refsGrowiPlugin.GalleryImmutable;
components.drawio = drawioPlugin.DrawioViewer;
components.mermaid = MermaidViewer;
}

if (config.isEnabledXssPrevention) {
Expand Down Expand Up @@ -238,6 +246,7 @@ export const generatePreviewOptions = (config: RendererConfig, pagePath: string)
xsvToTable.remarkPlugin,
lsxGrowiPlugin.remarkPlugin,
refsGrowiPlugin.remarkPlugin,
mermaid.remarkPlugin,
);
if (config.isEnabledLinebreaks) {
remarkPlugins.push(breaks);
Expand All @@ -254,6 +263,7 @@ export const generatePreviewOptions = (config: RendererConfig, pagePath: string)
refsGrowiPlugin.sanitizeOption,
drawioPlugin.sanitizeOption,
addLineNumberAttribute.sanitizeOption,
mermaid.sanitizeOption,
)]
: () => {};

Expand All @@ -275,6 +285,7 @@ export const generatePreviewOptions = (config: RendererConfig, pagePath: string)
components.refsimg = refsGrowiPlugin.RefsImgImmutable;
components.gallery = refsGrowiPlugin.GalleryImmutable;
components.drawio = drawioPlugin.DrawioViewer;
components.mermaid = MermaidViewer;
}

if (config.isEnabledXssPrevention) {
Expand Down
28 changes: 28 additions & 0 deletions apps/app/src/components/ReactMarkdownComponents/MermaidViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useRef, useEffect, ReactNode } from 'react';

import mermaid from 'mermaid';

type MermaidViewerProps = {
children: ReactNode,
}

export const MermaidViewer = React.memo((props: MermaidViewerProps): JSX.Element => {
const { children } = props;

const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
if (ref.current != null && children != null) {
mermaid.init({}, ref.current);
}
}, [children]);

return (
children
? <div ref={ref} key={children as string}>
{children}
</div>
: <div key={children as string} />
);
});
MermaidViewer.displayName = 'MermaidViewer';
yuki-takei marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions apps/app/src/services/renderer/remark-plugins/mermaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Schema as SanitizeOption } from 'hast-util-sanitize';
import { Plugin } from 'unified';
import { Node } from 'unist';
import { visit } from 'unist-util-visit';

function rewriteNode(node: Node) {
// replace node
const data = node.data ?? (node.data = {});
data.hName = 'mermaid';
}

export const remarkPlugin: Plugin = function() {
return (tree) => {
visit(tree, (node) => {
if (node.type === 'code' && node.lang === 'mermaid') {
rewriteNode(node);
}
});
};
};

export const sanitizeOption: SanitizeOption = {
tagNames: ['mermaid'],
};
yuki-takei marked this conversation as resolved.
Show resolved Hide resolved