Skip to content

Commit e61621c

Browse files
committed
feat: mermaid support
- fix link render - add mermaid support - change menu/nav style
1 parent 4be4632 commit e61621c

File tree

10 files changed

+114
-22
lines changed

10 files changed

+114
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ Markdown 渲染引擎为 [markdown-it](https://github.com/markdown-it/),可引
88

99
- 代码高亮:[highlight.js](https://highlightjs.org/)
1010
- 数学公式:[katex](https://katex.org/)
11+
- 图表:[mermaid](https://mermaid.js.org/)
12+
13+
文档:[GitHub Pages](https://ifyun.github.io/jian-doc/)

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
rel="stylesheet"
1414
href="https://unpkg.com/katex@0.16.9/dist/katex.min.css" />
1515
<script src="https://unpkg.com/katex@0.16.9/dist/katex.min.js"></script>
16+
<script type="module">
17+
import mermaid from "https://unpkg.com/mermaid@10/dist/mermaid.esm.min.mjs"
18+
window.mermaid = mermaid
19+
</script>
1620
<script>
1721
window.$config = {
1822
prefix: "docs",

public/docs/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- 创建一个 `<div id="app"></div>` 元素
77
- 引入 `jian-doc.umd.js`
88

9+
可以从此处获取:[jian-doc.umd.js](https://ifyun.github.io/jian-doc/jian-doc.umd.js)
10+
911
```html
1012
<!doctype html>
1113
<html lang="en">
@@ -102,6 +104,19 @@
102104
<script src="https://unpkg.com/katex@0.16.9/dist/katex.min.js"></script>
103105
```
104106

107+
### 图表
108+
109+
引入 `mermaid`,挂载到 `window` 对象:
110+
111+
```html
112+
<script type="module">
113+
import mermaid from "https://unpkg.com/mermaid@10/dist/mermaid.esm.min.mjs"
114+
window.mermaid = mermaid
115+
</script>
116+
```
117+
118+
代码块语言为 `mermaid` 即可编写图表。
119+
105120
## 文档样式测试
106121

107122
### 段落
@@ -144,3 +159,47 @@ A_{m,n} =
144159
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
145160
\end{pmatrix}
146161
$$
162+
163+
### Mermaid
164+
165+
序列图:
166+
167+
```mermaid
168+
sequenceDiagram
169+
participant C as Client
170+
participant S as Server
171+
172+
Note right of S: LISTEN
173+
C ->> S : SYN = 1, seq = x
174+
Note left of C: SYN_SENT
175+
S ->> C : SYN = 1, ACK = 1, seq = y, ack = x + 1
176+
Note right of S: SYN_RECV
177+
C ->> S : ACK = 1, seq = x + 1, ack = y + 1
178+
Note left of C: ESTABLISHED
179+
Note right of S: ESTABLISHED
180+
```
181+
182+
状态图:
183+
184+
```mermaid
185+
stateDiagram-v2
186+
[*] --> NEW
187+
NEW --> RUNNABLE: Thread.start()
188+
189+
state RUNNABLE {
190+
RUNNING --> READY
191+
READY --> RUNNING
192+
}
193+
194+
RUNNABLE --> BLOCKED: 未获取到锁
195+
BLOCKED --> RUNNABLE: 获取到锁
196+
197+
RUNNABLE --> TIMED_WAITING: Thread.sleep(long)\nObject.wait(long)
198+
TIMED_WAITING --> RUNNABLE: Object.notify()
199+
200+
RUNNABLE --> WAITING: Thread.join()
201+
WAITING --> RUNNABLE: Object.notify()
202+
203+
RUNNABLE --> TERMINATED: 执行完成
204+
TERMINATED --> [*]
205+
```

src/core.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,26 @@ function renderMenu() {
101101
})
102102
}
103103

104-
export function renderContent(path: string) {
104+
export function renderContent(path: string, rerender: boolean = false) {
105+
const colorScheme = window.localStorage.getItem("color-scheme")
105106
const content = document.querySelector("#content .markdown-body")!
106107

107108
getDoc(path)
108109
.then((val) => {
109110
content.innerHTML = md.render(val)
110-
setTimeout(() => renderNav(path), 0)
111+
setTimeout(() => {
112+
if (!rerender) {
113+
renderNav(path)
114+
}
115+
116+
window.mermaid.initialize({
117+
startOnLoad: false,
118+
theme: colorScheme ?? "",
119+
themeVariables: { fontSize: 14 }
120+
})
121+
122+
window.mermaid.run()
123+
}, 0)
111124
})
112125
.catch((err) => {
113126
content.innerHTML = err

src/event.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export function initUI() {
88

99
if (colorScheme === "dark") {
1010
document.documentElement.setAttribute("color-scheme", colorScheme)
11-
document.getElementById("theme-btn")!.innerHTML = darkMode
12-
} else {
1311
document.getElementById("theme-btn")!.innerHTML = lightMode
12+
} else {
13+
document.getElementById("theme-btn")!.innerHTML = darkMode
1414
}
1515

1616
if (window.$config.logoRound) {
@@ -34,16 +34,22 @@ export function toggleMenu() {
3434
}
3535

3636
export function toggleTheme() {
37-
const themeButton = document.getElementById("theme-btn")
3837
const root = document.documentElement
3938
const colorScheme = root.getAttribute("color-scheme") == "dark" ? "" : "dark"
39+
const themeButton = document.getElementById("theme-btn")
40+
4041
root.setAttribute("color-scheme", colorScheme)
4142
window.localStorage.setItem("color-scheme", colorScheme)
4243

4344
if (colorScheme === "dark") {
44-
themeButton!.innerHTML = darkMode
45-
} else {
4645
themeButton!.innerHTML = lightMode
46+
} else {
47+
themeButton!.innerHTML = darkMode
48+
}
49+
50+
if (document.querySelector(".mermaid")) {
51+
// Rerender for mermaid
52+
renderContent(window.location.hash.split("#")[1], true)
4753
}
4854
}
4955

@@ -87,12 +93,6 @@ export function hashChange(e: HashChangeEvent) {
8793
const url = e.newURL.split("#")[1]
8894
const docAndId = url.split("?id=")
8995

90-
const paddingTop = parseInt(
91-
getComputedStyle(document.documentElement).getPropertyValue(
92-
"--content-padding"
93-
)
94-
)
95-
9696
if (oldDoc !== docAndId[0] && docAndId[0].trim().length > 0) {
9797
menuActive({ doc: docAndId[0] })
9898
renderContent(docAndId[0])
@@ -101,10 +101,12 @@ export function hashChange(e: HashChangeEvent) {
101101
const anchor = document.getElementById(id)
102102

103103
if (anchor) {
104+
const marginTop = getComputedStyle(anchor)["marginTop"]
105+
104106
document.getElementById("content")!.scrollTop =
105107
anchor.offsetTop -
106108
(anchor.parentNode as HTMLElement).offsetTop +
107-
paddingTop / 2
109+
parseInt(marginTop)
108110
}
109111

110112
menuActive({ doc: docAndId[0], id })

src/plugins/fence-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const FencePlugin = (md: MarkdownIt) => {
1010
let content = tokens[index].content
1111
let info = tokens[index].info
1212

13-
if (info.trim() === "") {
14-
info = "text"
13+
if (info === "mermaid") {
14+
return `<pre class="mermaid">${content}</pre>`
1515
}
1616

1717
// Render code and line number

src/plugins/link-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const LinkPlugin = (md: MarkdownIt) => {
1414
const title = tokens[index + 1].content
1515
const href = tokens[index].attrGet("href")!
1616

17-
if (!href.startsWith("http://") || !href.startsWith("https://")) {
17+
if (!href.startsWith("http://") && !href.startsWith("https://")) {
1818
tokens[index].attrSet("data-title", title)
1919
tokens[index].attrSet("data-doc", href)
2020
tokens[index].attrSet("href", `./#${href}`)

src/style/global.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:root {
22
font-size: 14px;
3+
--markdown-font-size: 16px;
34
--font: "Source Han Sans SC", "Noto Sans CJK SC", sans-serif;
45
--background: #ffffff;
56
--content-padding: 28px;
@@ -11,7 +12,7 @@
1112
/* element color */
1213
--side-color-back: #f8f8fa;
1314
--active-color: #f7ab1d;
14-
--link-color: #2f81f7;
15+
--link-color: #5bae50;
1516
--inline-code-color: #61aeee;
1617
--inline-code-back: #f8f8fa;
1718
--block-code-back: #f8f8fa;
@@ -147,8 +148,9 @@ strong {
147148

148149
&.active,
149150
&:hover {
151+
font-weight: var(--font-weight-bold);
150152
color: var(--active-color);
151-
transition: all 300ms linear;
153+
transition: all 200ms linear;
152154
}
153155
}
154156
}
@@ -252,8 +254,9 @@ strong {
252254

253255
&.active,
254256
&:hover {
257+
font-weight: var(--font-weight-bold);
255258
color: var(--font-color);
256-
transition: all 300ms linear;
259+
transition: all 200ms linear;
257260
}
258261
}
259262
}

src/style/markdown.scss

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.markdown-body {
2+
font-size: var(--markdown-font-size);
23
line-height: 1.5em;
34
padding: 0 2.5em;
45

@@ -47,11 +48,17 @@
4748
padding-left: 24px;
4849
}
4950

51+
pre.mermaid {
52+
display: flex;
53+
align-items: center;
54+
background: transparent;
55+
}
56+
5057
pre {
51-
font-family: "JetBrains Mono", "Consolas", monospace;
52-
background: var(--block-code-back);
5358
display: flex;
5459
flex-direction: column;
60+
font-family: "JetBrains Mono", "Consolas", monospace;
61+
background: var(--block-code-back);
5562

5663
> .code-title {
5764
display: flex;

src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ declare interface Window {
1010

1111
hljs: any
1212
katex: any
13+
mermaid: any
1314
}

0 commit comments

Comments
 (0)