Skip to content

refactor(toc): support skipping heading level#5653

Merged
stevenjoezhang merged 2 commits intomasterfrom
toc
Oct 24, 2025
Merged

refactor(toc): support skipping heading level#5653
stevenjoezhang merged 2 commits intomasterfrom
toc

Conversation

@stevenjoezhang
Copy link
Member

What does it do?

Issue resolved: #2137

Screenshots

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@github-actions
Copy link

How to test

git clone -b toc https://github.com/hexojs/hexo.git
cd hexo
npm install
npm test

@coveralls
Copy link

coveralls commented Apr 19, 2025

Pull Request Test Coverage Report for Build 18782287697

Details

  • 87 of 87 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 99.526%

Totals Coverage Status
Change from base Build 18782167202: 0.0%
Covered Lines: 9877
Relevant Lines: 9924

💛 - Coveralls

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = {}) {
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The str parameter should have explicit type annotation string to maintain consistency with TypeScript best practices and the original function signature.

Suggested change
function tocHelper(str, options: Options = {}) {
function tocHelper(str: string, options: Options = {}) {

Copilot uses AI. Check for mistakes.
/**
* Extract flat TOC data and enforce max_items
*/
function getAndTruncateTocObj(str, { min_depth, max_depth }, max_items) {
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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) {

Copilot uses AI. Check for mistakes.
/**
* Build nested tree from flat heading list
*/
function buildTree(headings) {
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headings parameter should have explicit type annotation to maintain TypeScript consistency. Consider adding appropriate interface or type definition.

Suggested change
function buildTree(headings) {
function buildTree(headings: Heading[]) {

Copilot uses AI. Check for mistakes.
Comment on lines +128 to +130
function assignNumbers(nodes) {
const counters = [];
function dfs(list, depth) {
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nodes parameter should have explicit type annotation to maintain TypeScript consistency. Consider adding appropriate interface or type definition.

Suggested change
function assignNumbers(nodes) {
const counters = [];
function dfs(list, depth) {
function assignNumbers(nodes: Node[]) {
const counters: number[] = [];
function dfs(list: Node[], depth: number) {

Copilot uses AI. Check for mistakes.
@stevenjoezhang stevenjoezhang merged commit 573c70f into master Oct 24, 2025
23 checks passed
@stevenjoezhang stevenjoezhang deleted the toc branch October 24, 2025 16:24
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.

TOC seems not suitable when skipping heading levels

4 participants