Skip to content

Commit

Permalink
feat: use Mermaid (openRin#237)
Browse files Browse the repository at this point in the history
* feat: use Mermaid

* chore: use loadash to debounce rendering

* chore: update lockfile
  • Loading branch information
nick-cjyx9 authored Aug 10, 2024
1 parent f12910d commit 7bdaf76
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion client/src/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import rehypeKatex from "rehype-katex";
import rehypeRaw from "rehype-raw";
import gfm from "remark-gfm";
import remarkMermaid from "../remark/remarkMermaid";
import { remarkAlert } from "remark-github-blockquote-alert";
import remarkMath from "remark-math";
import Lightbox, { SlideImage } from "yet-another-react-lightbox";
Expand All @@ -18,6 +19,7 @@ import Zoom from "yet-another-react-lightbox/plugins/zoom";
import "yet-another-react-lightbox/styles.css";
import { useColorMode } from "../utils/darkModeUtils";


const countNewlinesBeforeNode = (text: string, offset: number) => {
let newlinesBefore = 0;
for (let i = offset - 1; i >= 0; i--) {
Expand Down Expand Up @@ -58,7 +60,7 @@ export function Markdown({ content }: { content: string }) {
const Content = useMemo(() => (
<ReactMarkdown
className="toc-content dark:text-neutral-300"
remarkPlugins={[gfm, remarkMath, remarkAlert]}
remarkPlugins={[gfm, remarkMermaid, remarkMath, remarkAlert]}
children={content}
rehypePlugins={[rehypeKatex, rehypeRaw]}
components={{
Expand Down
21 changes: 21 additions & 0 deletions client/src/page/feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { timeago } from "../utils/timeago";
import { Button } from "../components/button";
import { Tips } from "../components/tips";
import { useLoginModal } from "../hooks/useLoginModal";
import mermaid from "mermaid";

type Feed = {
id: number;
Expand Down Expand Up @@ -128,6 +129,26 @@ export function FeedPage({ id, TOC, clean }: { id: string, TOC: () => JSX.Elemen
});
ref.current = id;
}, [id]);
useEffect(() => {
mermaid.initialize({
startOnLoad: false,
theme: "default",
});
mermaid.run({
suppressErrors: true,
nodes: document.querySelectorAll("pre.mermaid_default")
}).then(()=>{
mermaid.initialize({
startOnLoad: false,
theme: "dark",
});
mermaid.run({
suppressErrors: true,
nodes: document.querySelectorAll("pre.mermaid_dark")
});
})
}, [feed]);

return (
<Waiting for={feed || error}>
{feed && (
Expand Down
31 changes: 29 additions & 2 deletions client/src/page/writing.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Editor from '@monaco-editor/react';
import i18n from 'i18next';
import _ from 'lodash';
import { editor } from 'monaco-editor';
import { Calendar } from 'primereact/calendar';
import 'primereact/resources/primereact.css';
import 'primereact/resources/themes/lara-light-indigo/theme.css';
import React, { useEffect, useRef, useState } from "react";
import React, { useEffect, useRef, useState, useCallback } from "react";
import { Helmet } from "react-helmet";
import { useTranslation } from "react-i18next";
import Loading from 'react-loading';
Expand All @@ -16,6 +17,7 @@ import { headersWithAuth } from "../utils/auth";
import { Cache, useCache } from '../utils/cache';
import { siteName } from "../utils/constants";
import { useColorMode } from "../utils/darkModeUtils";
import mermaid from 'mermaid';

async function publish({
title,
Expand Down Expand Up @@ -250,7 +252,7 @@ export function WritingPage({ id }: { id?: number }) {
const t = i18n.t
const upChange = (event: any) => {
for (let i = 0; i < event.currentTarget.files.length; i++) {
let file = event.currentTarget.files[i]; ///获得input的第一个图片
const file = event.currentTarget.files[i]; ///获得input的第一个图片
if (file.size > 5 * 1024000) {
showAlert(t("upload.failed$size", { size: 5 }))
uploadRef.current!.value = "";
Expand Down Expand Up @@ -306,6 +308,31 @@ export function WritingPage({ id }: { id?: number }) {
});
}
}, []);
const debouncedUpdate = useCallback(
_.debounce(() => {
mermaid.initialize({
startOnLoad: false,
theme: "default",
});
mermaid.run({
suppressErrors: true,
nodes: document.querySelectorAll("pre.mermaid_default")
}).then(()=>{
mermaid.initialize({
startOnLoad: false,
theme: "dark",
});
mermaid.run({
suppressErrors: true,
nodes: document.querySelectorAll("pre.mermaid_dark")
});
})
}, 100),
[]
);
useEffect(() => {
debouncedUpdate();
}, [content, debouncedUpdate]);
function MetaInput({ className }: { className?: string }) {
return (
<>
Expand Down
26 changes: 26 additions & 0 deletions client/src/remark/remarkMermaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Plugin } from 'unified';
import type { Content, Root } from 'mdast';

function processNode(child: Content, index: number, siblings: Content[]) {
if (child.type !== 'code') {
if ('children' in child) {
child.children.map(processNode);
}
return;
}
const { lang, value } = child;
if (lang !== 'mermaid') return;
siblings[index] = {
type: 'html',
value: `
<pre class="mermaid_default dark:hidden">${value}</pre>
<pre class="mermaid_dark dark:block hidden">${value}</pre>
`
}
}

const remarkMermaid: Plugin<[], Root> = () => (root: Root) => {
root.children.map(processNode)
};

export default remarkMermaid;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
"vitest": "1.3.0"
},
"dependencies": {
"@types/lodash": "^4.17.7",
"@types/react-syntax-highlighter": "^15.5.13",
"fast-xml-parser": "^4.4.0",
"html-to-md": "^0.8.5",
"lodash": "^4.17.21",
"mermaid": "^10.9.1",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"rehype-katex": "^7.0.0",
Expand Down

0 comments on commit 7bdaf76

Please sign in to comment.