// "On this page" outlines shared by the Markdown viewer, gists and pull requests.
// Text split around XXXX-n masks, so templates can draw each one as a slot.
export function redactParts(text) {
const out = [];
const re = /\bXXXX-(\d+)\b/g;
let last = 0, m;
text = String(text ?? "");
while ((m = re.exec(text))) {
if (m.index > last) out.push({ t: text.slice(last, m.index) });
out.push({ n: m[1] });
last = m.index + m[0].length;
}
if (last < text.length) out.push({ t: text.slice(last) });
return out;
}
// A heading's text with its redaction chips read back as XXXX-n.
const headingText = (h) => {
const copy = h.cloneNode(true);
copy.querySelectorAll("mark.redact-chip[data-term]").forEach((m) => m.replaceWith(m.dataset.term));
copy.querySelectorAll("a.anchor, .heading-anchor").forEach((a) => a.remove());
return copy.textContent.replace(/\s+/g, " ").trim();
};
/**
* h1-h3 of rendered Markdown (a DOM node or an HTML string) as outline items:
* { id, level, depth, parts }. depth is relative to the shallowest heading, so a
* document without an h1 still starts at depth 0.
*/
export function headingOutline(root, { max = 40 } = {}) {
if (typeof root === "string") {
if (typeof window.DOMParser !== "function") return [];
root = new window.DOMParser().parseFromString("
" + root + "
", "text/html").body;
}
if (!root?.querySelectorAll) return [];
const list = [...root.querySelectorAll("h1[id], h2[id], h3[id]")].slice(0, max)
.map((h) => ({ id: h.id, level: Number(h.tagName[1]), parts: redactParts(headingText(h)) }))
.filter((h) => h.parts.length);
const top = Math.min(...list.map((h) => h.level));
list.forEach((h) => { h.depth = h.level - top; });
return list;
}
/**
* Scroll spy: the last section whose anchor has crossed the reading line (a
* fifth of the way down the viewport, below any sticky chrome). At the end of
* the page the last anchor on screen wins, so a short final section can be
* highlighted. Returns "" before the first anchor.
*/
export function activeSection(ids, { offset = 0, doc = window.document, view = window, scroller = null } = {}) {
const line = Math.max(offset + 24, view.innerHeight * 0.2);
let current = "";
const tops = [];
for (const id of ids) {
const el = doc.getElementById(id);
if (!el) continue;
const top = el.getBoundingClientRect().top;
tops.push([id, top]);
if (top <= line) current = id;
}
// The page scrolls, or an inner pane does (the explorer's content column).
const box = scroller && scroller.scrollHeight > scroller.clientHeight + 4 ? scroller : doc.scrollingElement || doc.documentElement;
const atEnd = !!box && box.scrollTop > 0 && box.scrollTop + box.clientHeight >= box.scrollHeight - 4;
if (atEnd) {
const onScreen = tops.filter(([, top]) => top < view.innerHeight - 40);
if (onScreen.length) current = onScreen[onScreen.length - 1][0];
}
return current;
}
// Smoothly bring an anchor under the sticky chrome and move focus there for
// keyboard and screen-reader users.
export function jumpTo(id, doc = window.document) {
const el = id && doc.getElementById(id);
if (!el) return false;
el.scrollIntoView({ behavior: "smooth", block: "start" });
if (!el.hasAttribute("tabindex")) el.setAttribute("tabindex", "-1");
el.focus?.({ preventScroll: true });
return true;
}