Skip to content

Commit

Permalink
Hide toc headers (#5070)
Browse files Browse the repository at this point in the history
[Asana
task](https://app.asana.com/0/1200099998847559/1205976870784519/f)

## What are you changing in this pull request and why?

If h2 or h3 headers exist within the Tabs or Expandable components, they
appear in the TOC. However they are unable to link to the section in the
page from the TOC as they are hidden elements initially.

This filters out TOC headers which are nested in Tabs or Expandable
components.

## Previews

This page has tabs and a demo `expandable` component at the top of the
page. Verify the headers within these sections are removed from the TOC:

https://docs-getdbt-com-git-hide-toc-headers-dbt-labs.vercel.app/docs/cloud/cloud-cli-installation

This page has h2 & h3 headings, verify they remain on the page:

https://docs-getdbt-com-git-hide-toc-headers-dbt-labs.vercel.app/docs/cloud/about-cloud/architecture

## Checklist
- [x] Remove demo Expandable content from cloud-cli-installation page
  • Loading branch information
JKarlavige authored Mar 13, 2024
2 parents 6a0a199 + c496ba8 commit ff15fc3
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions website/src/theme/DocItem/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,63 @@ function useDocTOC() {

async function fetchElements() {
// get html elements
const headings = await getElements(".markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6")

const headings = await getElements(
".markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6"
);

// Headings to remove from TOC
const headingsToFilter = await getElements(
".tabs-container h2, .tabs-container h3, .expandable-anchor h2, .expandable-anchor h3"
);

// if headings exist on page
// compare against toc
if(toc && headings && headings.length) {
// make new TOC object
let updated = Array.from(headings).reduce((acc, item) => {
// Filter out TOC items from tabs
const shouldFilter = Array?.from(headingsToFilter)?.find(
(thisHeading) => thisHeading?.id === item?.id
);
if (shouldFilter) {
return acc;
}

// If heading id and toc item id match found
// include in updated toc
let found = toc.find(heading =>
heading.id.includes(item.id)
)
let found = toc.find((heading) => heading.id.includes(item.id));
// If toc item is not in headings
// do not include in toc
// This means heading is versioned

let makeToc = (heading) => {
let level;
if (heading.nodeName === "H2") {
level = 2
level = 2;
} else if (heading.nodeName === "H3") {
level = 3
level = 3;
} else {
level = null
level = null;
}

return {
value: heading.innerHTML,
id: heading.id,
level: level && level
}
}
level: level && level,
};
};

// If heading is in current version
// include in updated toc
if (found) {
acc.push(makeToc(item))
acc.push(makeToc(item));
} else if (!found) {
acc.push(makeToc(item))
acc.push(makeToc(item));
} else {
null
null;
}

return acc
return acc;
}, [])

// If updated toc different than current
Expand Down

0 comments on commit ff15fc3

Please sign in to comment.