From 53c58b8b1fcdd14ff2bb3a705bc9efd2710e2ed4 Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 17 Jan 2024 12:05:56 +0100 Subject: [PATCH 1/3] clean toc from labels + float labels right when on headings --- src/css/labels.css | 8 ++++++++ src/js/02-on-this-page.js | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/css/labels.css b/src/css/labels.css index 36c5e7ee..a6556a7f 100644 --- a/src/css/labels.css +++ b/src/css/labels.css @@ -9,6 +9,14 @@ font-style: normal; } +h2 > span.label, +h3 > span.label, +h4 > span.label, +h5 > span.label, +h6 > span.label { + float: right; +} + .tableblock .label { margin-top: 0.2rem; } diff --git a/src/js/02-on-this-page.js b/src/js/02-on-this-page.js index 580fb0ab..aa26c91b 100644 --- a/src/js/02-on-this-page.js +++ b/src/js/02-on-this-page.js @@ -20,7 +20,10 @@ var links = {} var list = headings.reduce(function (accum, heading) { var link = document.createElement('a') - link.textContent = heading.textContent + var headingWithoutLabels = heading.cloneNode(true) + if (headingWithoutLabels.querySelector('span.label') != null) + headingWithoutLabels.removeChild(headingWithoutLabels.querySelector('span.label')) + link.textContent = headingWithoutLabels.textContent links[(link.href = '#' + heading.id)] = link var listItem = document.createElement('li') listItem.dataset.level = parseInt(heading.nodeName.slice(1)) - 1 From 376bacc124b58e1305221cb672bbfec782a17211 Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 17 Jan 2024 12:11:41 +0100 Subject: [PATCH 2/3] lint --- src/js/02-on-this-page.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/js/02-on-this-page.js b/src/js/02-on-this-page.js index aa26c91b..df30b670 100644 --- a/src/js/02-on-this-page.js +++ b/src/js/02-on-this-page.js @@ -21,8 +21,9 @@ var list = headings.reduce(function (accum, heading) { var link = document.createElement('a') var headingWithoutLabels = heading.cloneNode(true) - if (headingWithoutLabels.querySelector('span.label') != null) + if (headingWithoutLabels.querySelector('span.label') != null) { headingWithoutLabels.removeChild(headingWithoutLabels.querySelector('span.label')) + } link.textContent = headingWithoutLabels.textContent links[(link.href = '#' + heading.id)] = link var listItem = document.createElement('li') From 46b74a7fbfe761468a857fe25d9a66f4ef323783 Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 24 Jan 2024 11:09:29 +0100 Subject: [PATCH 3/3] shorter --- src/js/02-on-this-page.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/js/02-on-this-page.js b/src/js/02-on-this-page.js index df30b670..661391ad 100644 --- a/src/js/02-on-this-page.js +++ b/src/js/02-on-this-page.js @@ -20,11 +20,8 @@ var links = {} var list = headings.reduce(function (accum, heading) { var link = document.createElement('a') - var headingWithoutLabels = heading.cloneNode(true) - if (headingWithoutLabels.querySelector('span.label') != null) { - headingWithoutLabels.removeChild(headingWithoutLabels.querySelector('span.label')) - } - link.textContent = headingWithoutLabels.textContent + var headingTextWithoutLabels = Array.from(heading.childNodes).filter((el) => el.nodeType === Node.TEXT_NODE).map((el) => el.textContent).join(' ').trim() + link.textContent = headingTextWithoutLabels links[(link.href = '#' + heading.id)] = link var listItem = document.createElement('li') listItem.dataset.level = parseInt(heading.nodeName.slice(1)) - 1