rspress-plugin-file-tree 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/FileTree/FileTree.css +1 -0
- package/dist/index.js +27 -13
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3,10 +3,11 @@ import { RemarkCodeBlockToGlobalComponentPluginFactory } from "rspress-plugin-de
|
|
|
3
3
|
function parseTreeContent(content) {
|
|
4
4
|
let lines = content.split('\n').filter((line)=>line.trim());
|
|
5
5
|
if (lines.length > 0 && '.' === lines[0].trim()) lines = lines.slice(1);
|
|
6
|
+
const indentSize = detectIndentSize(lines);
|
|
6
7
|
const nodes = [];
|
|
7
8
|
const stack = [];
|
|
8
9
|
for (const line of lines){
|
|
9
|
-
const indent = calculateIndent(line);
|
|
10
|
+
const indent = calculateIndent(line, indentSize);
|
|
10
11
|
const fullName = extractName(line);
|
|
11
12
|
const { name, comment } = extractNameAndComment(fullName);
|
|
12
13
|
if (!name) continue;
|
|
@@ -31,30 +32,43 @@ function parseTreeContent(content) {
|
|
|
31
32
|
raw: content
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
|
-
function
|
|
35
|
+
function detectIndentSize(lines) {
|
|
36
|
+
for (const line of lines){
|
|
37
|
+
const match = line.match(/^( +)[├└]/);
|
|
38
|
+
if (match) {
|
|
39
|
+
const spaceCount = match[1].length;
|
|
40
|
+
if (2 === spaceCount) return 2;
|
|
41
|
+
}
|
|
42
|
+
if (/│ [├└]/.test(line)) return 2;
|
|
43
|
+
}
|
|
44
|
+
return 4;
|
|
45
|
+
}
|
|
46
|
+
function calculateIndent(line, indentSize) {
|
|
35
47
|
let indent = 0;
|
|
36
48
|
let i = 0;
|
|
37
49
|
while(i < line.length){
|
|
38
50
|
const char = line[i];
|
|
39
|
-
if ('│' === char && '│ ' === line.substring(i, i + 4)) {
|
|
51
|
+
if (4 === indentSize && '│' === char && '│ ' === line.substring(i, i + 4)) {
|
|
40
52
|
indent++;
|
|
41
53
|
i += 4;
|
|
42
54
|
continue;
|
|
43
55
|
}
|
|
44
|
-
if ('│' === char && ' ' === line[i + 1]) {
|
|
56
|
+
if (2 === indentSize && '│' === char && ' ' === line[i + 1]) {
|
|
45
57
|
indent++;
|
|
46
58
|
i += 2;
|
|
47
59
|
continue;
|
|
48
60
|
}
|
|
49
|
-
if ('
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
if (' ' === char) {
|
|
62
|
+
if (2 === indentSize && ' ' === line.substring(i, i + 2)) {
|
|
63
|
+
indent++;
|
|
64
|
+
i += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (4 === indentSize && ' ' === line.substring(i, i + 4)) {
|
|
68
|
+
indent++;
|
|
69
|
+
i += 4;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
58
72
|
}
|
|
59
73
|
if ('├' === char || '└' === char) {
|
|
60
74
|
if ('├──' === line.substring(i, i + 3) || '└──' === line.substring(i, i + 3)) indent++;
|