Skip to content

Commit

Permalink
Merge pull request #284 from RyoJerryYu/feat/add-obsidian-rich-feature
Browse files Browse the repository at this point in the history
feat: beautify excalidraw components
  • Loading branch information
RyoJerryYu authored Apr 14, 2024
2 parents 56d0caf + 414b400 commit 1f55055
Show file tree
Hide file tree
Showing 15 changed files with 642 additions and 361 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dependencies": {
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@excalidraw/excalidraw": "^0.17.0",
"@excalidraw/excalidraw": "^0.17.3",
"@excalidraw/mermaid-to-excalidraw": "^0.3.0",
"@mdx-js/mdx": "^2.3.0",
"@mdx-js/react": "^2.3.0",
"@mui/base": "^5.0.0-alpha.119",
Expand All @@ -31,7 +32,7 @@
"gray-matter": "^4.0.3",
"isomorphic-git": "^1.21.0",
"mdast-util-mdx-jsx": "^3.1.2",
"mermaid": "^9.4.0",
"mermaid": "10.9.0",
"next": "13.2.1",
"next-mdx-remote": "^4.3.0",
"next-sitemap": "^3.1.55",
Expand All @@ -55,6 +56,7 @@
"sass": "^1.58.3",
"shiki": "^0.14.1",
"svgo": "^3.0.2",
"swr": "^2.2.5",
"typescript": "4.9.5",
"unified": "^10.1.2",
"unist-util-visit": "^4.1.2"
Expand Down
18 changes: 12 additions & 6 deletions public/content/articles/2021-03-21-Handy-heap-cheat-sheet.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created_at: 2021-08-28 23:09:14
updated_at: 2022-03-27 21:30:33+08:00
updated_at: 2024-04-14 21:30:33+08:00
layout: post
title: "如何手撕一个堆"
subtitle: "如果哪一天你把编程语言的类库全忘光了,又遇到一题明知到要用堆的题目,咋办?对着一道自己明显会的题干着急,愣是想不起PriorityQueue的名字。这时候只能自己实现一个堆出来了。"
Expand Down Expand Up @@ -95,11 +95,13 @@ $$T(i) = logi$$

那么把所有元素上浮,则总时间复杂度为:

$$\begin{aligned}
$$
\begin{aligned}
T(n) &= \sum_{i=1}^{n}logi\\
&= 1\times0 + 2\times1 + ... + 2^{logn}\times{logn} \\
&=O(nlogn)
\end{aligned}$$
\end{aligned}
$$

通过把元素逐个入堆来建堆时,元素的时间复杂度可以用下图直观显示:

Expand All @@ -115,15 +117,19 @@ T(n) &= \sum_{i=1}^{n}logi\\

下沉第i个元素(从顶到底数)时,以其为顶点的树高度约为$logn-logi$,则有下沉时间复杂度为:

$$T(i) = logn-logi$$
$$
T(i) = logn-logi
$$

那么把所有元素下沉,则总时间复杂度为:

$$\begin{aligned}
$$
\begin{aligned}
T(n) &= \sum_{i=1}^{n}logn-logi \\
&= \frac{n}{2^{logn}}\times{logn}+ ... + \frac{n}{4}\times2+\frac{n}{2}\times1 \\
&= O(n)
\end{aligned}$$
\end{aligned}
$$

同样的,我们也可以把逐个元素下沉所耗费的时间用下图来示意:

Expand Down
6 changes: 6 additions & 0 deletions public/content/ideas/blog-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def func_echo(s: str):
```
````

行内反引号围栏: `` ` `` 或者 ``` `` ``` 的模式

```markdown
`段落反引号内的行内反引号`
```

## 列表

- 无序列表
Expand Down
35 changes: 17 additions & 18 deletions src/components/ExcalidrawScene/EmbededExcalidraw.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";

const ExcalidrawScene = dynamic(
async () => (await import("./index")).ExcalidrawScene,
{
ssr: false,
}
);
import useSWR from "swr";
import { ExcalidrawScene } from "./ExcalidrawScene";

export type EmbededExcalidrawProps = {
file: string;
Expand All @@ -20,13 +13,19 @@ export default function EmbededExcalidraw({
url,
label,
}: EmbededExcalidrawProps) {
const [elements, setElements] = useState<ExcalidrawElement[]>([]);
useEffect(() => {
fetch(url)
.then((res) => res.json())
.then((data) => {
setElements(data.elements);
});
}, [file, url, label]);
return <ExcalidrawScene elements={elements} />;
const { data: refData, isLoading } = useSWR(url, async (url) => {
const res = await fetch(url).then((res) => res.json());
return res.elements as ExcalidrawElement[];
});
return (
<div className="relative h-[600px] py-4">
{isLoading ? (
<div className="absolute inset-0 flex items-center justify-center">
Loading...
</div>
) : (
<ExcalidrawScene elements={refData!} />
)}
</div>
);
}
8 changes: 8 additions & 0 deletions src/components/ExcalidrawScene/ExcalidrawScene.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import dynamic from "next/dynamic";

export const ExcalidrawScene = dynamic(
async () => (await import("./ExcalidrawSceneImpl")).ExcalidrawSceneImpl,
{
ssr: false,
}
);
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"use client";
import { Excalidraw } from "@excalidraw/excalidraw";
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
import { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types/types";
import { useRef } from "react";

type ExcalidrawSceneProps = {
elements: ExcalidrawElement[];
};

export function ExcalidrawScene({ elements }: ExcalidrawSceneProps) {
export function ExcalidrawSceneImpl({ elements }: ExcalidrawSceneProps) {
const excalidrawAPIRef = useRef<ExcalidrawImperativeAPI>();
return (
<>
<div className="h-96">
<div className="w-full h-full">
<Excalidraw
initialData={{
elements,
Expand All @@ -18,17 +21,25 @@ export function ExcalidrawScene({ elements }: ExcalidrawSceneProps) {
UIOptions={{
canvasActions: {
changeViewBackgroundColor: false,
clearCanvas: false,
export: false,
loadScene: false,
saveToActiveFile: false,
toggleTheme: false,
saveAsImage: false,
},
}}
viewModeEnabled
zenModeEnabled={false}
theme="light"
viewModeEnabled={true}
zenModeEnabled={true}
gridModeEnabled={false}
detectScroll={false}
excalidrawAPI={(api) => {
excalidrawAPIRef.current = api;

setTimeout(() => {
api.scrollToContent(undefined, { fitToContent: true });
}, 1000);
}}
></Excalidraw>
</div>
</>
Expand Down
6 changes: 6 additions & 0 deletions src/components/ExcalidrawScene/MermaidCodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dynamic from "next/dynamic";

export const MermaidCodeBlock = dynamic(
async () => (await import("./MermaidCodeBlockImpl")).MermaidCodeBlockImpl,
{ ssr: false }
);
54 changes: 54 additions & 0 deletions src/components/ExcalidrawScene/MermaidCodeBlockImpl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client";
import { convertToExcalidrawElements } from "@excalidraw/excalidraw";
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
import { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";
import { useEffect, useState } from "react";
import { ExcalidrawSceneImpl } from "./ExcalidrawSceneImpl";

export type MermaidCodeBlockProps = {
name?: string;
children: string;
className?: string;
};
export function MermaidCodeBlockImpl({
name = "mermaid",
children,
className,
}: MermaidCodeBlockProps) {
console.log(children);
const [elements, setElements] = useState<ExcalidrawElement[]>([]);
const [parseDone, setParseDone] = useState(false);
useEffect(() => {
const parseMermaid = async () => {
if (parseDone) return;
try {
const { elements: elementSkeletons } = await parseMermaidToExcalidraw(
children,
{}
);
const excalidrawElements =
convertToExcalidrawElements(elementSkeletons);
setElements(excalidrawElements);
setParseDone(true);
console.log("elements", excalidrawElements);
} catch (e) {
console.error("error", e);
}
};

parseMermaid();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children, parseDone]);

return (
<div className="relative h-[600px] py-4">
{parseDone ? (
<ExcalidrawSceneImpl elements={elements} />
) : (
<div className="absolute inset-0 flex items-center justify-center">
Loading...
</div>
)}
</div>
);
}
54 changes: 26 additions & 28 deletions src/components/Mermaid/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
"use client";
export {};

import mermaid from "mermaid";
import React, { useEffect, useLayoutEffect, useMemo } from "react";
// // mermaid.init({
// // startOnLoad: false,
// // });

mermaid.init({
startOnLoad: false,
});
// type MermaidProps = {
// name?: string;
// children: string;
// className?: string;
// };

type MermaidProps = {
name?: string;
children: string;
className?: string;
};
// const Mermaid = ({ name = "mermaid", children, className }: MermaidProps) => {
// const [svg, setSvg] = React.useState("");
// useEffect(() => {
// const res = mermaid.render(name, children);
// setSvg(res);
// }, [name, children]);

const Mermaid = ({ name = "mermaid", children, className }: MermaidProps) => {
const [svg, setSvg] = React.useState("");
useEffect(() => {
const res = mermaid.render(name, children);
setSvg(res);
}, [name, children]);
// // TODO maybe caculate simple size and set default size
// return svg === "" ? (
// <code>{children}</code>
// ) : (
// <div
// className={className ? `mermaid ${className}` : `mermaid`}
// dangerouslySetInnerHTML={{ __html: svg }}
// />
// );
// };

// TODO maybe caculate simple size and set default size
return svg === "" ? (
<code>{children}</code>
) : (
<div
className={className ? `mermaid ${className}` : `mermaid`}
dangerouslySetInnerHTML={{ __html: svg }}
/>
);
};

export default Mermaid;
// export default Mermaid;
2 changes: 2 additions & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { MDXComponents } from "mdx/types";
import { Bar } from "react-chartjs-2";
import EmbededExcalidraw from "./ExcalidrawScene/EmbededExcalidraw";
import { MermaidCodeBlock } from "./ExcalidrawScene/MermaidCodeBlock";
import License from "./License";

ChartJS.register(
Expand All @@ -26,6 +27,7 @@ const components: MDXComponents = {
Bar,
// Mermaid,
License,
MermaidCodeBlock,
ObsidianRich: (props: ObsidianRichProps) => {
console.log("ObsidianRich:", props);
if (
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkUnwrapImages from "remark-unwrap-images";
import remarkMermaid from "./remark-mermaid";
import remarkExcalidrawMermaid from "./remark-excalidraw-mermaid";
import remarkObsidianRich, {
RemarkObsidianRichOptions,
} from "./remark-obsidian-rich";
Expand All @@ -24,7 +24,8 @@ const parseMdx = async (content: string, config?: ParseMdxConfig) => {
remarkPlugins: [
remarkGfm,
remarkMath,
[remarkMermaid, { wrap: true, className: ["mermaid"] }],
remarkExcalidrawMermaid,
// [remarkMermaid, { wrap: true, className: ["mermaid"] }],
[remarkObsidianRich, config?.remarkObsidianRichOptions],
remarkUnwrapImages,
],
Expand Down
Loading

0 comments on commit 1f55055

Please sign in to comment.