2024-07-07 09:32:30 -06:00
|
|
|
import {svg} from '../svg.ts';
|
2020-06-26 07:04:22 -06:00
|
|
|
|
2024-11-11 04:13:57 -07:00
|
|
|
const addPrefix = (str: string): string => `user-content-${str}`;
|
|
|
|
const removePrefix = (str: string): string => str.replace(/^user-content-/, '');
|
|
|
|
const hasPrefix = (str: string): boolean => str.startsWith('user-content-');
|
2024-03-22 06:28:38 -06:00
|
|
|
|
2024-03-08 02:53:01 -07:00
|
|
|
// scroll to anchor while respecting the `user-content` prefix that exists on the target
|
2024-11-11 04:13:57 -07:00
|
|
|
function scrollToAnchor(encodedId: string): void {
|
2024-03-22 06:28:38 -06:00
|
|
|
if (!encodedId) return;
|
2024-03-20 17:02:53 -06:00
|
|
|
const id = decodeURIComponent(encodedId);
|
2024-03-22 06:28:38 -06:00
|
|
|
const prefixedId = addPrefix(id);
|
2024-06-10 14:49:33 -06:00
|
|
|
let el = document.querySelector(`#${prefixedId}`);
|
2024-03-20 17:02:53 -06:00
|
|
|
|
|
|
|
// check for matching user-generated `a[name]`
|
|
|
|
if (!el) {
|
2024-06-10 14:49:33 -06:00
|
|
|
el = document.querySelector(`a[name="${CSS.escape(prefixedId)}"]`);
|
2024-03-20 17:02:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// compat for links with old 'user-content-' prefixed hashes
|
2024-03-22 06:28:38 -06:00
|
|
|
if (!el && hasPrefix(id)) {
|
2024-06-10 14:49:33 -06:00
|
|
|
return document.querySelector(`#${id}`)?.scrollIntoView();
|
2020-06-26 07:04:22 -06:00
|
|
|
}
|
2024-03-20 17:02:53 -06:00
|
|
|
|
2024-03-22 06:28:38 -06:00
|
|
|
el?.scrollIntoView();
|
2020-06-26 07:04:22 -06:00
|
|
|
}
|
|
|
|
|
2024-11-11 04:13:57 -07:00
|
|
|
export function initMarkupAnchors(): void {
|
2024-03-20 17:02:53 -06:00
|
|
|
const markupEls = document.querySelectorAll('.markup');
|
|
|
|
if (!markupEls.length) return;
|
|
|
|
|
|
|
|
for (const markupEl of markupEls) {
|
|
|
|
// create link icons for markup headings, the resulting link href will remove `user-content-`
|
2024-03-22 06:28:38 -06:00
|
|
|
for (const heading of markupEl.querySelectorAll('h1, h2, h3, h4, h5, h6')) {
|
2024-03-20 17:02:53 -06:00
|
|
|
const a = document.createElement('a');
|
|
|
|
a.classList.add('anchor');
|
2024-03-22 06:28:38 -06:00
|
|
|
a.setAttribute('href', `#${encodeURIComponent(removePrefix(heading.id))}`);
|
2024-03-20 17:02:53 -06:00
|
|
|
a.innerHTML = svg('octicon-link');
|
|
|
|
heading.prepend(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove `user-content-` prefix from links so they don't show in url bar when clicked
|
2024-11-11 04:13:57 -07:00
|
|
|
for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')) {
|
2024-03-20 17:02:53 -06:00
|
|
|
const href = a.getAttribute('href');
|
|
|
|
if (!href.startsWith('#user-content-')) continue;
|
2024-03-22 06:28:38 -06:00
|
|
|
a.setAttribute('href', `#${removePrefix(href.substring(1))}`);
|
2024-03-20 17:02:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// add `user-content-` prefix to user-generated `a[name]` link targets
|
|
|
|
// TODO: this prefix should be added in backend instead
|
2024-11-11 04:13:57 -07:00
|
|
|
for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[name]')) {
|
2024-03-20 17:02:53 -06:00
|
|
|
const name = a.getAttribute('name');
|
|
|
|
if (!name) continue;
|
2024-11-11 04:13:57 -07:00
|
|
|
a.setAttribute('name', addPrefix(name));
|
2024-03-20 17:02:53 -06:00
|
|
|
}
|
2020-06-26 07:04:22 -06:00
|
|
|
|
2024-11-11 04:13:57 -07:00
|
|
|
for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')) {
|
2024-03-08 02:53:01 -07:00
|
|
|
a.addEventListener('click', (e) => {
|
2024-11-11 04:13:57 -07:00
|
|
|
scrollToAnchor((e.currentTarget as HTMLAnchorElement).getAttribute('href')?.substring(1));
|
2024-03-08 02:53:01 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 06:28:38 -06:00
|
|
|
// scroll to anchor unless the browser has already scrolled somewhere during page load
|
|
|
|
if (!document.querySelector(':target')) {
|
|
|
|
scrollToAnchor(window.location.hash?.substring(1));
|
|
|
|
}
|
2020-06-26 07:04:22 -06:00
|
|
|
}
|