Skip to content

Commit 203486e

Browse files
committed
Improve readability
1 parent 24939d4 commit 203486e

File tree

1 file changed

+55
-58
lines changed

1 file changed

+55
-58
lines changed

assets/js/anchor.js

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,57 @@
1-
(function () {
2-
'use strict';
3-
4-
document.addEventListener('DOMContentLoaded', function () {
5-
// Append anchor links to headings in Markdown
6-
var article = document.getElementsByTagName('main')[0];
7-
if (!article) {
8-
return;
9-
}
10-
11-
// Select all headings in the article
12-
var headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
13-
headings.forEach(function (heading) {
14-
if (heading.id) {
15-
var a = document.createElement('a');
16-
a.setAttribute('aria-hidden', 'true');
17-
a.style.display = 'inline-block';
1+
'use strict';
2+
3+
document.addEventListener('DOMContentLoaded', () => {
4+
// Append anchor links to headings in Markdown
5+
const article = document.getElementsByTagName('main')[0];
6+
if (!article) {
7+
return;
8+
}
9+
10+
// Select all headings in the article
11+
const headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
12+
headings.forEach((heading) => {
13+
if (heading.id) {
14+
const a = document.createElement('a');
15+
a.setAttribute('aria-hidden', 'true');
16+
a.style.display = 'inline-block';
17+
a.style.visibility = 'hidden';
18+
a.style.padding = '.1rem';
19+
a.style.borderRadius = '4px';
20+
a.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" fill="var(--bs-gray-900)" width="24" height="24" viewBox="0 0 32 32"><path d="M18.9001 7.22C20.5201 5.6 23.1601 5.6 24.7801 7.22C26.4001 8.84 26.4001 11.48 24.7801 13.1L20.4001 17.48L21.8101 18.89L26.1901 14.51C28.5901 12.11 28.5901 8.2 26.1901 5.8C23.7901 3.4 19.8801 3.4 17.4801 5.8L13.1001 10.18L14.5101 11.59L18.8901 7.21L18.9001 7.22Z"/><path d="M10.16 28C11.74 28 13.32 27.4 14.52 26.2L18.9 21.82L17.49 20.41L13.11 24.79C11.49 26.41 8.85002 26.41 7.23002 24.79C5.61002 23.17 5.61002 20.53 7.23002 18.91L11.61 14.53L10.2 13.12L5.82002 17.5C3.42002 19.9 3.42002 23.81 5.82002 26.21C7.02002 27.41 8.60002 28.01 10.18 28.01L10.16 28Z"/><path d="M9.44971 21.1336L21.124 9.45927L22.5383 10.8735L10.8639 22.5478L9.44971 21.1336Z"/></svg>';
21+
a.href = '#' + heading.id;
22+
23+
// Append anchor element to heading
24+
heading.appendChild(a);
25+
26+
// Show icon when hovering over heading
27+
heading.addEventListener('mouseenter', () => {
28+
a.style.visibility = 'initial';
29+
});
30+
31+
// Hide icon when mouse leaves heading
32+
heading.addEventListener('mouseleave', () => {
1833
a.style.visibility = 'hidden';
19-
a.style.padding = '.1rem';
20-
a.style.borderRadius = '4px';
21-
a.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" fill="var(--bs-gray-900)" width="24" height="24" viewBox="0 0 32 32"><path d="M18.9001 7.22C20.5201 5.6 23.1601 5.6 24.7801 7.22C26.4001 8.84 26.4001 11.48 24.7801 13.1L20.4001 17.48L21.8101 18.89L26.1901 14.51C28.5901 12.11 28.5901 8.2 26.1901 5.8C23.7901 3.4 19.8801 3.4 17.4801 5.8L13.1001 10.18L14.5101 11.59L18.8901 7.21L18.9001 7.22Z"/><path d="M10.16 28C11.74 28 13.32 27.4 14.52 26.2L18.9 21.82L17.49 20.41L13.11 24.79C11.49 26.41 8.85002 26.41 7.23002 24.79C5.61002 23.17 5.61002 20.53 7.23002 18.91L11.61 14.53L10.2 13.12L5.82002 17.5C3.42002 19.9 3.42002 23.81 5.82002 26.21C7.02002 27.41 8.60002 28.01 10.18 28.01L10.16 28Z"/><path d="M9.44971 21.1336L21.124 9.45927L22.5383 10.8735L10.8639 22.5478L9.44971 21.1336Z"/></svg>';
22-
a.href = '#' + heading.id;
23-
24-
// Append anchor element to heading
25-
heading.appendChild(a);
26-
27-
// Show icon when hovering over heading
28-
heading.addEventListener('mouseenter', function () {
29-
a.style.visibility = 'initial';
30-
});
31-
32-
// Hide icon when mouse leaves heading
33-
heading.addEventListener('mouseleave', function () {
34-
a.style.visibility = 'hidden';
35-
});
36-
37-
// Initialize Bootstrap tooltip
38-
var tooltip = new bootstrap.Tooltip(a, {
39-
title: 'Copy link to clipboard',
40-
placement: 'right',
41-
trigger: 'hover'
42-
});
43-
44-
// Add on-click behavior
45-
a.addEventListener('click', function (event) {
46-
event.preventDefault(); // Prevent scrolling
47-
var href = a.href;
48-
navigator.clipboard.writeText(href); // Copy to clipboard
49-
window.history.pushState({}, document.title, href); // Update URL
50-
// Update tooltip text
51-
var tooltipInner = document.querySelector('div.tooltip-inner');
52-
if (tooltipInner) {
53-
tooltipInner.textContent = 'Copied!';
54-
}
55-
});
56-
}
57-
});
34+
});
35+
36+
// Initialize Bootstrap tooltip
37+
const tooltip = new bootstrap.Tooltip(a, {
38+
title: 'Copy link to clipboard',
39+
placement: 'right',
40+
trigger: 'hover'
41+
});
42+
43+
// Add on-click behavior
44+
a.addEventListener('click', (event) => {
45+
event.preventDefault(); // Prevent scrolling
46+
const href = a.href;
47+
navigator.clipboard.writeText(href); // Copy to clipboard
48+
window.history.pushState({}, document.title, href); // Update URL
49+
// Update tooltip text
50+
const tooltipInner = document.querySelector('div.tooltip-inner');
51+
if (tooltipInner) {
52+
tooltipInner.textContent = 'Copied!';
53+
}
54+
});
55+
}
5856
});
59-
60-
}(jQuery));
57+
});

0 commit comments

Comments
 (0)