Skip to content

Commit ce21c7d

Browse files
authored
Merge pull request #5 from ukhsa-collaboration/bc-add-section-links
Add section links
2 parents 170c599 + e2acc29 commit ce21c7d

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

docs/_layouts/guidance-page.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
2525

2626
<!--Scripts-->
27-
<script> {% include expandable-sections.js %} </script>
27+
<script src="assets/scripts/expandable-sections.js"></script>
28+
<script src="assets/scripts/section-links.js" defer></script>
2829

2930
</head>
3031
<body>
@@ -57,4 +58,4 @@ <h1>{{ page.title }}</h1>
5758

5859
</body>
5960

60-
</html>
61+
</html>

docs/assets/img/permalink.png

10.2 KB
Loading

docs/assets/scripts/section-links.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Function to copy link to clipboard
2+
function copylink(text) {
3+
navigator.clipboard.writeText(text).then(() => {
4+
alert(`Link copied to clipboard:\n${text}`);
5+
}).catch(err => {
6+
console.error('Failed to copy link', err);
7+
});
8+
}
9+
10+
// Add copy link icons to all heading elements
11+
document.querySelectorAll('h1, h2, h3, h4, h5').forEach(heading => {
12+
// Ensure the heading has an ID for the anchor link
13+
if (!heading.id) {
14+
heading.id = heading.textContent.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]/g, '');
15+
}
16+
17+
// Create the icon element
18+
const iconWrapper = document.createElement('span');
19+
iconWrapper.className = 'section-link';
20+
iconWrapper.title = 'Section link';
21+
iconWrapper.innerHTML = ' ';
22+
23+
// Append the icon to the heading
24+
heading.appendChild(iconWrapper);
25+
26+
// Add event listener for copying the link
27+
iconWrapper.addEventListener('click', (e) => {
28+
e.stopPropagation(); // Prevent the click event from bubbling up
29+
const url = `${window.location.origin}${window.location.pathname}#${heading.id}`;
30+
copylink(url);
31+
});
32+
});

docs/assets/styles.css

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ ul, ol{
143143
}
144144

145145

146+
/*Section links*/
147+
.section-link{
148+
background-image: url('img/permalink.png');
149+
width: 18px;
150+
height: 18px;
151+
opacity: 0;
152+
margin-left: 5px;
153+
display: inline-block;
154+
background-size: cover;
155+
}
156+
157+
.section-link:hover{
158+
cursor: pointer;
159+
}
160+
161+
h1 span, h2 span, h3 span, h4 span, h5 span {
162+
transition: opacity 0.3s ease-in-out;
163+
}
164+
165+
h1:hover span, h2:hover span, h3:hover span, h4:hover span, h5:hover span{
166+
opacity: 0.5;
167+
}
168+
169+
146170
/*Fancy elements*/
147171
blockquote{
148172
margin: 1em 0 2em 0;
@@ -342,4 +366,4 @@ td:first-child {
342366
@media only screen and (max-width: 985px) {
343367
#copyright-block{display:block;}
344368
#copyright-notice{margin-left:0; padding-left:0;}
345-
}
369+
}

0 commit comments

Comments
 (0)