From 94b33badd78d2b36b365e91b11265985c7b7f360 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 7 Nov 2022 20:46:21 +0100 Subject: [PATCH] exclude headings from minitoc that don't have links (#32407) --- lib/get-mini-toc-items.js | 9 +++++++++ tests/unit/mini-toc-items.js | 31 +++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/get-mini-toc-items.js b/lib/get-mini-toc-items.js index 73152536e6..d59be2de60 100644 --- a/lib/get-mini-toc-items.js +++ b/lib/get-mini-toc-items.js @@ -35,6 +35,14 @@ export default function getMiniTocItems(html, maxHeadingLevel = 2, headingScope // Capture the anchor tag nested within the header, get its href and remove it const anchor = $('a.doctocat-link', item) const href = anchor.attr('href') + if (!href) { + // Can happen if the, for example, `

` tag was put there + // manually with HTML into the Markdown content. Then it wouldn't + // be rendered with an expected ` tags but leave content @@ -52,6 +60,7 @@ export default function getMiniTocItems(html, maxHeadingLevel = 2, headingScope return { contents, headingLevel, platform } }) + .filter(Boolean) .map((item) => { // set the indentation level for each item based on the most important // heading level in the current article diff --git a/tests/unit/mini-toc-items.js b/tests/unit/mini-toc-items.js index 5344c77377..fe4c73bbbc 100644 --- a/tests/unit/mini-toc-items.js +++ b/tests/unit/mini-toc-items.js @@ -1,10 +1,24 @@ import { expect } from '@jest/globals' import getMiniTocItems from '../../lib/get-mini-toc-items' +// The getMiniTocItems() function requires that every

and

+// contains a... +// +// +// +// tag within the tag. Having to manually put that into every HTML +// snippet in each test is tediuous so this function makes it convenient. +function injectDoctocatLinks(html) { + let counter = 0 + return html.replace(//g, (m) => { + return `${m}\n🔗\n` + }) +} + describe('mini toc items', () => { // Mock scenario from: /en/rest/reference/activity test('basic nested structure is created', async () => { - const html = ` + const html = injectDoctocatLinks(`

Test

Section 1

@@ -14,8 +28,9 @@ describe('mini toc items', () => {

Section 2

Section 2 A

- ` + `) const tocItems = getMiniTocItems(html, 3) + expect(tocItems.length).toBe(2) expect(tocItems[0].items.length).toBe(3) }) @@ -31,7 +46,7 @@ describe('mini toc items', () => { * 3 */ test('creates toc that starts with lower importance headers', async () => { - const html = ` + const html = injectDoctocatLinks(`

Test

Section 1 A

Section 1 B

@@ -39,7 +54,7 @@ describe('mini toc items', () => {

Section 2 A

Section 3

Section 3 A

- ` + `) const tocItems = getMiniTocItems(html, 3) expect(tocItems.length).toBe(4) expect(tocItems[3].items.length).toBe(1) @@ -56,18 +71,18 @@ describe('mini toc items', () => { // Mock scenario from: /en/repositories/creating-and-managing-repositories/about-repositories test('creates flat toc', async () => { - const html = ` + const html = injectDoctocatLinks(`

Test

Section 1

Section 2

- ` + `) const tocItems = getMiniTocItems(html, 3) expect(tocItems.length).toBe(2) expect(tocItems[0].items).toBeUndefined() }) test('handles deeply nested toc', async () => { - const html = ` + const html = injectDoctocatLinks(`

Test

Section 1

Section 2

@@ -75,7 +90,7 @@ describe('mini toc items', () => {

Section 2 A 1

Section 2 A 1 a

Section 3

- ` + `) const tocItems = getMiniTocItems(html, 5) expect(tocItems.length).toBe(3) expect(tocItems[1].items[0].items[0].items.length).toBe(1)