Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use top 2 level of nodes to generate toc #757

Merged
merged 3 commits into from
Feb 10, 2021
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
14 changes: 13 additions & 1 deletion packages/content/parsers/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class Markdown {
}
}

flattenNode (node, maxDepth = 2, depth = 0) {
if (!Array.isArray(node.children) || depth === maxDepth) {
return [node]
}
return [
node,
...node.children.flatMap(child => this.flattenNode(child, maxDepth, depth + 1))
]
}

/**
* Generate table of contents
* @param {object} body - JSON AST generated from markdown.
Expand All @@ -37,7 +47,9 @@ class Markdown {
generateToc (body) {
const { tocTags } = this.options

return body.children.filter(node => tocTags.includes(node.tag)).map((node) => {
const children = this.flattenNode(body, 2)

return children.filter(node => tocTags.includes(node.tag)).map((node) => {
const id = node.props.id

const depth = ({
Expand Down
57 changes: 57 additions & 0 deletions packages/content/test/fixture/content/toc-dom-depth-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Table of Contents Tests
---

## Heading A-2

<div>

# Heading B-1

Pragraph.

## Heading B-2

Pragraph.

### Heading B-3

Pragraph.

#### Heading B-4

Pragraph.

##### Heading B-5

Pragraph.

###### Heading B-6

Pragraph.

# Heading C-1

Pragraph.

## Heading C-2

Pragraph.

### Heading C-3

Pragraph.

#### Heading C-4

Pragraph.

##### Heading C-5

Pragraph.

###### Heading C-6

Pragraph.

</div>
31 changes: 31 additions & 0 deletions packages/content/test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,37 @@ describe('options', () => {
})
})

describe('generated table of contents for document with wrapper div', () => {
let nuxt
let $content

beforeAll(async () => {
({ nuxt } = (await setup(loadConfig(__dirname))))
const module = require('@nuxt/content')
$content = module.$content
}, 60000)

afterAll(async () => {
await nuxt.close()
})

test('', async () => {
const item = await $content('toc-dom-depth-2').fetch()

expect(item).toEqual(
expect.objectContaining({
toc: expect.arrayContaining([
expect.objectContaining({ depth: 2, id: 'heading-a-2', text: 'Heading A-2' }),
expect.objectContaining({ depth: 2, id: 'heading-b-2', text: 'Heading B-2' }),
expect.objectContaining({ depth: 3, id: 'heading-b-3', text: 'Heading B-3' }),
expect.objectContaining({ depth: 2, id: 'heading-c-2', text: 'Heading C-2' }),
expect.objectContaining({ depth: 3, id: 'heading-c-3', text: 'Heading C-3' })
])
})
)
})
})

describe('generated table of contents with depth 1', () => {
let nuxt
let $content
Expand Down