Skip to content

Conversation

@SoonIter
Copy link
Member

@SoonIter SoonIter commented Jan 8, 2026

Summary

Related issues

AI Summary


What changes were made

  1. Added detectIndentSize() function - Scans all lines to detect whether the tree uses 2-space or 4-space indentation mode
  2. Updated calculateIndent() function - Now accepts an indentSize parameter to correctly calculate indent levels based on the detected mode
  3. Added comprehensive test case - Tests a docs structure with 3-level nesting using 2-space indentation

Why they were made

The original tree parser had a bug when handling 2-space indented trees with 3 or more nesting levels. For example:

```tree
docs
├── _meta.json
└── guides
├── _meta.json
└── basic
├── introduction.mdx
├── install.mdx
└── plugin-development.md
```

The files under `basic` directory (4 spaces = 2+2) were incorrectly placed as siblings of `basic` instead of children, because the algorithm treated 4 spaces as a single 4-space indent unit rather than two 2-space units.

Implementation details

  • The fix detects indentation mode by looking for lines with exactly 2 spaces before branch characters (`├──` or `└──`)
  • Once 2-space mode is detected, all space-based indentation is calculated in 2-space units
  • This ensures ` ├── file.mdx` (4 spaces) is correctly calculated as indent level 3 (2 units of 2-space + branch) instead of indent level 2

This PR was written using Vibe Kanban


## 总结

### 1. 添加了新测试用例 (`parser.spec.ts:901-962`)

测试用例验证了 `basic` 被正确识别为目录,并包含三个文件:
- `introduction.mdx`
- `install.mdx`
- `plugin-development.md`

### 2. 修复了 tree-parser 的缩进检测逻辑 (`tree-parser.ts`)

原有的代码无法正确处理 2 空格缩进模式下的深层嵌套(3 层及以上)。问题在于 4 空格会被误判为 4-space 模式的 1 级缩进,而不是 2-space 模式的 2 级缩进。

修复方案:
1. 新增 `detectIndentSize(lines)` 函数:扫描所有行,检测整个内容使用的是 2-space 还是 4-space 缩进模式
2. 修改 `calculateIndent(line, indentSize)` 函数:接受 `indentSize` 参数,根据检测到的缩进模式计算每行的缩进级别

这样确保了:
- `  ├── _meta.json` → indent 2 (2空格 + branch)
- `    ├── introduction.mdx` → indent 3 (4空格 = 2×2空格 + branch)

所有 11 个测试都已通过。
@SoonIter SoonIter changed the title 完善 rspress-plugin-tree-parser (vibe-kanban) fix(plugin-file-tree): fix 2-space indentation parsing for deeply nested tree structures Jan 8, 2026
@SoonIter SoonIter merged commit 06939cb into main Jan 8, 2026
2 checks passed
@SoonIter SoonIter deleted the syt-vibe-kanban/7618-rspress-plugin-t branch January 8, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants