Skip to content

Commit

Permalink
make digest heading selector more specific and only update anchor lin…
Browse files Browse the repository at this point in the history
…ks in headings. Fixes #330
  • Loading branch information
zebapy committed Jul 7, 2020
1 parent 8f6fb5d commit dfccd32
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/js/digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const DigestNav = ({ items = [] }) => {
function addHeadingAnchors() {
// Store the selector since we need to manually update headings
// as well as apply AnochrJS to them.
const headingSelector = '[data-digest-content] h2';
// first-child gets section__titles and > h2 gets children of text components
const headingSelector =
'[data-digest-content] h2:first-child, [data-digest-content] > h2';

// Get all headings in the data-digest-content
const headings = $$(headingSelector);
Expand All @@ -45,18 +47,18 @@ function addHeadingAnchors() {
headings.forEach(heading => {
// Replace nbsps in heading id caused by d8 typogrify module.
// These nonbreaking spaces are intended to prevent tyographic widows.
let newId = heading.id.replace(/\s/g, '-').replace(/-+/g, '-');
let id = heading.id.replace(/\s/g, '-').replace(/-+/g, '-');

// if heading text begins with a number, we need to prefix some a-z text
// so selectors in digest nav are valid
if (!isNaN(newId.charAt(0))) {
newId = 'section-' + newId;
if (!isNaN(id.charAt(0))) {
id = 'section-' + id;
}

const anchor = $('a', heading);
anchor.href = '#' + newId;
const anchor = $('.anchorjs-link', heading);
anchor.href = '#' + id;

heading.id = newId;
heading.id = id;
});

return headings;
Expand Down

0 comments on commit dfccd32

Please sign in to comment.