Skip to content

Commit f9d513f

Browse files
committed
optimize markmap
1 parent e4e333e commit f9d513f

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

blog/2025-04/_partials/calculus.mdx

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{m1} & \cdots & A_{mn}
110110
\end{bmatrix}$"}
111111

112112
<Collapse label="$$
113-
\nabla_x (A x) = A^T
114-
$$">
113+
\nabla_x (A x) = A^T
114+
$$">
115115

116116
$$
117117
\begin{split}
@@ -136,23 +136,23 @@ A_{11} & \cdots & A_{1m} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nm}
136136
\end{bmatrix}$"}
137137

138138
<Collapse label="$$
139-
\nabla_x (x^T A) = A
140-
$$">
141-
142-
$$
143-
\begin{split}
144-
&\ \nabla_x (x^T A) = \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\
145-
=&\ \nabla_x (x_1 [A_{11} \cdots A_{1m}] + x_2 [A_{21} \cdots A_{2m}] + \cdots + x_n [A_{n1} \cdots A_{nm}]) \\
146-
=&\ \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\
147-
=&\ \nabla_x (x_1 A_1) + \nabla_x (x_2 A_2) + \cdots + \nabla_x (x_n A_n) \\
148-
=&\ \begin{bmatrix} \
149-
\frac{\partial}{\partial x_1} (x_1 A_1) , \frac{\partial}{\partial x_2} (x_2 A_2) , \cdots , \frac{\partial}{\partial x_n} (x_n A_n)
150-
\end{bmatrix} \\
151-
=&\ \begin{bmatrix}
152-
A_1 , A_2 , \cdots , A_n
153-
\end{bmatrix} = A
154-
\end{split}
155-
$$
139+
\nabla_x (x^T A) = A
140+
$$">
141+
142+
$$
143+
\begin{split}
144+
&\ \nabla_x (x^T A) = \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\
145+
=&\ \nabla_x (x_1 [A_{11} \cdots A_{1m}] + x_2 [A_{21} \cdots A_{2m}] + \cdots + x_n [A_{n1} \cdots A_{nm}]) \\
146+
=&\ \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\
147+
=&\ \nabla_x (x_1 A_1) + \nabla_x (x_2 A_2) + \cdots + \nabla_x (x_n A_n) \\
148+
=&\ \begin{bmatrix} \
149+
\frac{\partial}{\partial x_1} (x_1 A_1) , \frac{\partial}{\partial x_2} (x_2 A_2) , \cdots , \frac{\partial}{\partial x_n} (x_n A_n)
150+
\end{bmatrix} \\
151+
=&\ \begin{bmatrix}
152+
A_1 , A_2 , \cdots , A_n
153+
\end{bmatrix} = A
154+
\end{split}
155+
$$
156156

157157
</Collapse>
158158

@@ -164,20 +164,20 @@ A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nn}
164164
\end{bmatrix}$"}
165165

166166
<Collapse label="$$
167-
\nabla_x x^T A x = (A + A^T) x
168-
$$">
169-
170-
$$
171-
\begin{split}
172-
&\ \nabla_x x^T A x = \nabla_x \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j \\
173-
=&\ \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j + \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_j A_{ji} x_i \\
174-
=&\ \sum_{i=1}^n \sum_{j=1}^n A_{ij} x_j + \sum_{i=1}^n \sum_{j=1}^n A_{ji} x_i \\
175-
=&\ \sum_{j=1}^n A_{kj} x_j + \sum_{i=1}^n A_{ik} x_i \text{($i, j = k$ 时,$A_{kj} x_j, A_{ik} x_i$ 分别存在一项 $A_{kk} x_k$)} \\
176-
=&\ \sum_{i=1}^n (\sum_{j=1}^n A_{ij} x_j) \cdot e_i + \sum_{j=1}^n (\sum_{i=1}^n A_{ji} x_i) \cdot e_j \\
177-
=&\ \sum_{i=1}^n (\mathbf{A} \mathbf{x})_i \cdot e_i + \sum_{j=1}^n (\mathbf{A^T} \mathbf{x})_j \cdot e_j \\ \
178-
=&\ (A + A^T) x
179-
\end{split}
180-
$$
167+
\nabla_x x^T A x = (A + A^T) x
168+
$$">
169+
170+
$$
171+
\begin{split}
172+
&\ \nabla_x x^T A x = \nabla_x \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j \\
173+
=&\ \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j + \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_j A_{ji} x_i \\
174+
=&\ \sum_{i=1}^n \sum_{j=1}^n A_{ij} x_j + \sum_{i=1}^n \sum_{j=1}^n A_{ji} x_i \\
175+
=&\ \sum_{j=1}^n A_{kj} x_j + \sum_{i=1}^n A_{ik} x_i \text{($i, j = k$ 时,$A_{kj} x_j, A_{ik} x_i$ 分别存在一项 $A_{kk} x_k$)} \\
176+
=&\ \sum_{i=1}^n (\sum_{j=1}^n A_{ij} x_j) \cdot e_i + \sum_{j=1}^n (\sum_{i=1}^n A_{ji} x_i) \cdot e_j \\
177+
=&\ \sum_{i=1}^n (\mathbf{A} \mathbf{x})_i \cdot e_i + \sum_{j=1}^n (\mathbf{A^T} \mathbf{x})_j \cdot e_j \\ \
178+
=&\ (A + A^T) x
179+
\end{split}
180+
$$
181181

182182
</Collapse>
183183

@@ -196,16 +196,16 @@ A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nn}
196196
\end{bmatrix}$"}
197197

198198
<Collapse label="$$
199-
\nabla_x \|x\|_2^2 = \nabla_x (x^T x) = 2x
200-
$$">
201-
202-
$$
203-
\begin{split}
204-
&\ \nabla_x \|x\|_2 = \nabla_x (\sqrt{x^T x}) ^ 2 \\
205-
=&\ \nabla_x (x^T x) = \nabla_x (x_1^2 + x_2^2 + \cdots + x_n^2) \\
206-
=&\ 2x
207-
\end{split}
208-
$$
199+
\nabla_x \|x\|_2^2 = \nabla_x (x^T x) = 2x
200+
$$">
201+
202+
$$
203+
\begin{split}
204+
&\ \nabla_x \|x\|_2 = \nabla_x (\sqrt{x^T x}) ^ 2 \\
205+
=&\ \nabla_x (x^T x) = \nabla_x (x_1^2 + x_2^2 + \cdots + x_n^2) \\
206+
=&\ 2x
207+
\end{split}
208+
$$
209209

210210
</Collapse>
211211

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"canvas": "^3.1.0",
4444
"canvg": "^4.0.2",
4545
"clsx": "^2.0.0",
46+
"d3": "^7.9.0",
4647
"echarts": "^5.5.1",
4748
"file-saver": "^2.0.5",
4849
"heliannuuthus-docusaurus-authors": "file:./plugins/docusaurus-authors",
@@ -88,6 +89,7 @@
8889
"@docusaurus/tsconfig": "3.8.1",
8990
"@docusaurus/types": "3.8.1",
9091
"@trivago/prettier-plugin-sort-imports": "^5.2.0",
92+
"@types/d3": "^7.4.3",
9193
"@types/file-saver": "^2.0.7",
9294
"@types/react": "^19.0.0",
9395
"@types/react-dom": "^19.0.0",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/markdown/markmap/context.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ export const transformPath = (svg: markmap.ID3SVGElement) => {
6363
const cyCollections = new Map<string, number>();
6464
svg
6565
.node()
66-
.querySelectorAll("g")
66+
?.querySelectorAll(".markmap-node")
6767
.forEach((g: SVGGElement) => {
68+
console.log(g);
6869
const dataPath = g.getAttribute("data-path");
6970
const circle = g.querySelector("circle");
7071
if (circle && dataPath) {
@@ -89,21 +90,32 @@ export const transformPath = (svg: markmap.ID3SVGElement) => {
8990
}
9091
svg
9192
.node()
92-
?.querySelectorAll("path")
93+
?.querySelectorAll(".markmap-link")
9394
.forEach((path: SVGPathElement) => {
95+
console.log(path);
9496
const dataPath = path.getAttribute("data-path");
95-
if (dataPath) {
96-
const parentPath = dataPath.split(".").slice(0, -1).join(".");
97-
const parsed = path.getPathData();
98-
for (const seg of parsed) {
99-
const argc = seg.values.length;
100-
if (argc >= 2) {
101-
for (let i = 1; i < argc; i += 2) {
102-
seg.values[i] =
103-
Number(seg.values[i]) - cyCollections.get(parentPath);
104-
}
105-
}
106-
}
97+
if (!dataPath) return;
98+
const parentPath = dataPath.split(".").slice(0, -1).join(".");
99+
const parentCy = cyCollections.get(parentPath) ?? 0;
100+
const currentCy = cyCollections.get(dataPath) ?? parentCy;
101+
const parsed = path.getPathData();
102+
103+
if (
104+
parsed &&
105+
parsed.length >= 2 &&
106+
parsed[0].type === "M" &&
107+
parsed[1].type === "C"
108+
) {
109+
parsed[0].values[1] -= parentCy;
110+
parsed[1].values[5] -= currentCy;
111+
112+
const offset = currentCy - parentCy;
113+
const c1 = 1 / 3;
114+
const c2 = 2 / 3;
115+
116+
parsed[1].values[1] -= parentCy + offset * c1;
117+
parsed[1].values[3] -= parentCy + offset * c2;
118+
107119
const result = parsed
108120
.map((seg) => seg.type + seg.values.join(","))
109121
.join("");

0 commit comments

Comments
 (0)