mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.4 KiB
68 lines
2.4 KiB
5 months ago
|
import {svg} from '../svg.ts';
|
||
4 years ago
|
|
||
8 months ago
|
const addPrefix = (str) => `user-content-${str}`;
|
||
|
const removePrefix = (str) => str.replace(/^user-content-/, '');
|
||
|
const hasPrefix = (str) => str.startsWith('user-content-');
|
||
|
|
||
9 months ago
|
// scroll to anchor while respecting the `user-content` prefix that exists on the target
|
||
8 months ago
|
function scrollToAnchor(encodedId) {
|
||
|
if (!encodedId) return;
|
||
8 months ago
|
const id = decodeURIComponent(encodedId);
|
||
8 months ago
|
const prefixedId = addPrefix(id);
|
||
6 months ago
|
let el = document.querySelector(`#${prefixedId}`);
|
||
8 months ago
|
|
||
|
// check for matching user-generated `a[name]`
|
||
|
if (!el) {
|
||
6 months ago
|
el = document.querySelector(`a[name="${CSS.escape(prefixedId)}"]`);
|
||
8 months ago
|
}
|
||
|
|
||
|
// compat for links with old 'user-content-' prefixed hashes
|
||
8 months ago
|
if (!el && hasPrefix(id)) {
|
||
6 months ago
|
return document.querySelector(`#${id}`)?.scrollIntoView();
|
||
4 years ago
|
}
|
||
8 months ago
|
|
||
8 months ago
|
el?.scrollIntoView();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function initMarkupAnchors() {
|
||
8 months ago
|
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-`
|
||
8 months ago
|
for (const heading of markupEl.querySelectorAll('h1, h2, h3, h4, h5, h6')) {
|
||
8 months ago
|
const a = document.createElement('a');
|
||
|
a.classList.add('anchor');
|
||
8 months ago
|
a.setAttribute('href', `#${encodeURIComponent(removePrefix(heading.id))}`);
|
||
8 months ago
|
a.innerHTML = svg('octicon-link');
|
||
|
heading.prepend(a);
|
||
|
}
|
||
|
|
||
|
// remove `user-content-` prefix from links so they don't show in url bar when clicked
|
||
|
for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
|
||
|
const href = a.getAttribute('href');
|
||
|
if (!href.startsWith('#user-content-')) continue;
|
||
8 months ago
|
a.setAttribute('href', `#${removePrefix(href.substring(1))}`);
|
||
8 months ago
|
}
|
||
|
|
||
|
// add `user-content-` prefix to user-generated `a[name]` link targets
|
||
|
// TODO: this prefix should be added in backend instead
|
||
|
for (const a of markupEl.querySelectorAll('a[name]')) {
|
||
|
const name = a.getAttribute('name');
|
||
|
if (!name) continue;
|
||
8 months ago
|
a.setAttribute('name', addPrefix(a.name));
|
||
8 months ago
|
}
|
||
4 years ago
|
|
||
8 months ago
|
for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
|
||
9 months ago
|
a.addEventListener('click', (e) => {
|
||
8 months ago
|
scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1));
|
||
9 months ago
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
// scroll to anchor unless the browser has already scrolled somewhere during page load
|
||
|
if (!document.querySelector(':target')) {
|
||
|
scrollToAnchor(window.location.hash?.substring(1));
|
||
|
}
|
||
4 years ago
|
}
|