refactor(toc): support skipping heading level#5653
Conversation
How to testgit clone -b toc https://github.com/hexojs/hexo.git
cd hexo
npm install
npm test |
Pull Request Test Coverage Report for Build 18782287697Details
💛 - Coveralls |
dfdecdb to
ce6512d
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the Table of Contents (TOC) helper to support skipping heading levels by replacing the previous linear processing approach with a tree-based structure. The refactor addresses issue #2137 by properly handling cases where heading levels are not sequential (e.g., h1 followed directly by h3).
Key changes:
- Replaced linear TOC generation with tree-based approach using
buildTree()function - Separated numbering logic into dedicated
assignNumbers()function - Added comprehensive test case for skipped heading levels scenario
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/plugins/helper/toc.ts | Complete refactor from linear to tree-based TOC generation with new helper functions |
| test/scripts/helpers/toc.ts | Added test case verifying correct handling of skipped heading levels |
| * @param {string} str Raw markdown/html string | ||
| * @param {Options} options Configuration options | ||
| */ | ||
| function tocHelper(str, options: Options = {}) { |
There was a problem hiding this comment.
The str parameter should have explicit type annotation string to maintain consistency with TypeScript best practices and the original function signature.
| function tocHelper(str, options: Options = {}) { | |
| function tocHelper(str: string, options: Options = {}) { |
| /** | ||
| * Extract flat TOC data and enforce max_items | ||
| */ | ||
| function getAndTruncateTocObj(str, { min_depth, max_depth }, max_items) { |
There was a problem hiding this comment.
Function parameters lack type annotations. Should be str: string, { min_depth, max_depth }: {min_depth: number, max_depth: number}, and max_items: number to maintain TypeScript type safety.
| function getAndTruncateTocObj(str, { min_depth, max_depth }, max_items) { | |
| function getAndTruncateTocObj(str: string, { min_depth, max_depth }: { min_depth: number, max_depth: number }, max_items: number) { |
| /** | ||
| * Build nested tree from flat heading list | ||
| */ | ||
| function buildTree(headings) { |
There was a problem hiding this comment.
The headings parameter should have explicit type annotation to maintain TypeScript consistency. Consider adding appropriate interface or type definition.
| function buildTree(headings) { | |
| function buildTree(headings: Heading[]) { |
| function assignNumbers(nodes) { | ||
| const counters = []; | ||
| function dfs(list, depth) { |
There was a problem hiding this comment.
The nodes parameter should have explicit type annotation to maintain TypeScript consistency. Consider adding appropriate interface or type definition.
| function assignNumbers(nodes) { | |
| const counters = []; | |
| function dfs(list, depth) { | |
| function assignNumbers(nodes: Node[]) { | |
| const counters: number[] = []; | |
| function dfs(list: Node[], depth: number) { |
What does it do?
Issue resolved: #2137
Screenshots
Pull request tasks