@redocly/theme
Version:
Shared UI components lib
43 lines (39 loc) • 1.03 kB
text/typescript
import type { MdHeading } from '../types/markdown';
export function getDisplayedHeadings(
headings: Array<MdHeading | null> | null | undefined,
tocDepth: number,
): Array<MdHeading | null> {
if (!headings) {
return [];
}
return headings.filter((heading, idx) => {
if (!heading) {
return false;
}
if (idx === 0 && heading.depth === 1) {
return false;
}
return !(heading.depth && heading.depth > tocDepth);
});
}
export function getDisplayedHeadingsIds(
headings: Array<MdHeading | null> | null | undefined,
): Array<string | undefined> {
if (!headings) {
return [];
}
return headings.map((header) => header?.id);
}
export function getLeastDepth(headings: Array<MdHeading | null> | null | undefined): number {
if (!headings || headings.length === 0) {
return 1;
}
let depth = null;
for (const heading of headings) {
if (!heading) continue;
if (depth === null || depth > heading.depth) {
depth = heading.depth;
}
}
return depth ?? 1;
}