Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/plugins/extractHeadings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default function extractHeadings(doc: Node) {
}

doc.descendants((node, offset) => {
// Don't descent into details/blockquote nodes - their headings are hidden
if (node.type.name === 'details' || node.type.name === 'blockquote') {
return false
}
if (node.type.name !== 'heading') {
return
}
Expand Down
33 changes: 31 additions & 2 deletions src/tests/plugins/extractHeadings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Blockquote } from '@tiptap/extension-blockquote'
import Details from '../../nodes/Details.js'
import Heading from '../../nodes/Heading.js'
import extractHeadings from '../../plugins/extractHeadings.ts'
import createCustomEditor from '../testHelpers/createCustomEditor.ts'
Expand Down Expand Up @@ -35,6 +37,33 @@ describe('extractHeadings', () => {
expect(headings).toEqual([])
})

it('ignores headings inside a details block', () => {
const content = `
<h1>Visible heading</h1>
<details>
<summary>Details summary</summary>
<div data-type="detailsContent"><h1>Hidden heading</h1></div>
</details>
`
const doc = prepareDoc(content, [Details])
const headings = extractHeadings(doc)
expect(headings).toHaveLength(1)
expect(headings[0].text).toBe('Visible heading')
})

it('ignores headings inside a block quote block', () => {
const content = `
<h1>Visible heading</h1>
<blockquote>
<h1>Quoted heading</h1>
</blockquote>
`
const doc = prepareDoc(content, [Blockquote])
const headings = extractHeadings(doc)
expect(headings).toHaveLength(1)
expect(headings[0].text).toBe('Visible heading')
})

it('creates unique ids with a counter', () => {
const content = `
<h1>Level 1 heading</h1>
Expand All @@ -46,8 +75,8 @@ describe('extractHeadings', () => {
})
})

const prepareDoc = (content) => {
const editor = createCustomEditor(content, [Heading])
const prepareDoc = (content, extraExtensions = []) => {
const editor = createCustomEditor(content, [Heading, ...extraExtensions])
const doc = editor.state.doc
editor.destroy()
return doc
Expand Down
Loading