fix(toc): fix page number of some TOC items from section fragments (#3867)

This commit is contained in:
Huang Xin
2026-04-14 22:22:06 +08:00
committed by GitHub
parent 8cdf378b47
commit 73d30c103f
2 changed files with 11 additions and 5 deletions
@@ -1,10 +1,16 @@
import { describe, it, expect, beforeAll, vi } from 'vitest';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { DocumentLoader } from '@/libs/document';
import { DocumentLoader, CFI } from '@/libs/document';
import type { BookDoc, TOCItem } from '@/libs/document';
import { updateToc, findTocItemBS } from '@/utils/toc';
// Simulates an annotation deep inside a section, past the chapter heading.
// Section CFIs look like `epubcfi(/6/2)` and TOC item CFIs point at the heading
// (e.g. `epubcfi(/6/2!/4/2[ch01])`), so a real annotation lives after that —
// e.g. inside a <p> sibling, reached via `CFI.joinIndir`.
const ANNOTATION_PATH = '/4/4/1:0';
// Polyfill CSS.escape for jsdom
if (typeof globalThis['CSS'] === 'undefined') {
(globalThis as Record<string, unknown>)['CSS'] = {
@@ -82,8 +88,8 @@ describe('TOC-to-CFI mapping with fragment hrefs (#3688)', () => {
const firstSection = linearSections[0]!;
const lastSection = linearSections[linearSections.length - 1]!;
const firstTocItem = findTocItemBS(toc, firstSection.cfi);
const lastTocItem = findTocItemBS(toc, lastSection.cfi);
const firstTocItem = findTocItemBS(toc, CFI.joinIndir(firstSection.cfi, ANNOTATION_PATH));
const lastTocItem = findTocItemBS(toc, CFI.joinIndir(lastSection.cfi, ANNOTATION_PATH));
expect(firstTocItem).not.toBeNull();
expect(lastTocItem).not.toBeNull();
@@ -99,7 +105,7 @@ describe('TOC-to-CFI mapping with fragment hrefs (#3688)', () => {
const linearSections = sections.filter((s) => s.linear !== 'no' && s.cfi);
const midSection = linearSections[1]!;
const midTocItem = findTocItemBS(toc, midSection.cfi);
const midTocItem = findTocItemBS(toc, CFI.joinIndir(midSection.cfi, ANNOTATION_PATH));
expect(midTocItem).not.toBeNull();
expect(midTocItem!.label).toBe('Chapter Two');