From 95bbf5f40593c6f96cf175c22db21cea264dd88c Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 8 Jul 2022 22:17:56 +0200 Subject: [PATCH] fix TOC widget not finding the heading by index, closes #2962 --- src/public/app/widgets/toc.js | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/public/app/widgets/toc.js b/src/public/app/widgets/toc.js index 3a95620a3..f8f7a2352 100644 --- a/src/public/app/widgets/toc.js +++ b/src/public/app/widgets/toc.js @@ -67,26 +67,6 @@ function findHeadingNodeByIndex(parent, headingIndex) { return headingNode; } -function findHeadingElementByIndex(parent, headingIndex) { - let headingElement = null; - for (let i = 0; i < parent.children.length; ++i) { - const child = parent.children[i]; - // Headings appear as flattened top level children in the DOM named as - // "H" plus the level, eg "H2", "H3", "H2", etc and not nested wrt the - // heading level. If a heading node is found, decrement the headingIndex - // until zero is reached - - if (child.tagName.match(/H\d+/i) !== null) { - if (headingIndex === 0) { - headingElement = child; - break; - } - headingIndex--; - } - } - return headingElement; -} - const MIN_HEADING_COUNT = 3; export default class TocWidget extends CollapsibleWidget { @@ -190,9 +170,8 @@ export default class TocWidget extends CollapsibleWidget { const isReadOnly = await this.noteContext.isReadOnly(); if (isReadOnly) { - const $readonlyTextContent = await this.noteContext.getContentElement(); - - const headingElement = findHeadingElementByIndex($readonlyTextContent[0], headingIndex); + const $container = await this.noteContext.getContentElement(); + const headingElement = $container.find(":header")[headingIndex]; if (headingElement != null) { headingElement.scrollIntoView();