Skip to content

Commit 6d3d250

Browse files
authored
contrib: fix urls of the contribution buttons (#294)
2 parents ccda00e + 4df4cc5 commit 6d3d250

4 files changed

Lines changed: 102 additions & 8974 deletions

File tree

_includes/globals.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,107 @@
99
const searchFromMastHead = {% if site.masthead != false and site.search_from_masthead == true %} true {% else %} false {% endif %};
1010

1111
const docRoot = '{{ site.baseurl }}';
12+
const repoUrl = '{{ site.github.repository_url }}';
13+
const collectionsDir = '{{ site.collections_dir }}';
1214

1315
function docPrefix() {
1416
return (docRoot != '' ? docRoot + '/' : '');
1517
}
1618

19+
function getRelativePathFromUrl() {
20+
// First, try to get the path from the data attribute on the article element
21+
var articleElement = document.querySelector('article.page');
22+
console.log('[ContribButtons] Article element found:', !!articleElement);
23+
24+
if (articleElement) {
25+
console.log('[ContribButtons] Article element:', articleElement);
26+
console.log('[ContribButtons] data-source-path attribute:', articleElement.getAttribute('data-source-path'));
27+
console.log('[ContribButtons] dataset.sourcePath:', articleElement.dataset.sourcePath);
28+
29+
if (articleElement.dataset.sourcePath) {
30+
var sourcePath = articleElement.dataset.sourcePath;
31+
console.log('[ContribButtons] ✓ Found source path from data attribute:', sourcePath);
32+
return sourcePath;
33+
}
34+
}
35+
36+
// Fallback: try to parse from URL (less reliable, mainly for debugging)
37+
console.warn('[ContribButtons] ✗ No data-source-path found, attempting to parse from URL');
38+
var pathname = window.location.pathname;
39+
console.log('[ContribButtons] Current pathname:', pathname);
40+
var parts = pathname.split('/').filter(Boolean);
41+
console.log('[ContribButtons] Path parts:', parts);
42+
43+
// Remove baseurl if present
44+
if (docRoot && parts.length > 0 && parts[0] === docRoot.replace(/^\//g, '')) {
45+
console.log('[ContribButtons] Removing docRoot:', parts[0]);
46+
parts.shift();
47+
}
48+
49+
// Remove collections_dir if present
50+
if (collectionsDir && parts.length > 0 && parts[0] === collectionsDir) {
51+
console.log('[ContribButtons] Removing collectionsDir:', parts[0]);
52+
parts.shift();
53+
}
54+
55+
// Join the remaining parts to get the relative path
56+
var relativePath = parts.join('/');
57+
console.log('[ContribButtons] Final relative path from URL parsing:', relativePath);
58+
console.warn('[ContribButtons] Warning: Path parsed from URL may be missing underscores and file extensions');
59+
return relativePath;
60+
}
61+
62+
function updateContributionButtons() {
63+
console.log('[ContribButtons] updateContributionButtons called');
64+
65+
if (!repoUrl) {
66+
console.error('[ContribButtons] No repoUrl configured, aborting');
67+
return;
68+
}
69+
70+
var relativePath = getRelativePathFromUrl();
71+
if (!relativePath) {
72+
console.error('[ContribButtons] Could not determine relative path, aborting');
73+
return;
74+
}
75+
76+
var fullPath = collectionsDir + '/' + relativePath;
77+
console.log('[ContribButtons] Full path for repository:', fullPath);
78+
79+
var viewBtn = document.getElementById('view');
80+
var editBtn = document.getElementById('edit');
81+
var reportBtn = document.getElementById('report');
82+
83+
console.log('[ContribButtons] Buttons found - view:', !!viewBtn, 'edit:', !!editBtn, 'report:', !!reportBtn);
84+
85+
if (viewBtn) {
86+
var viewUrl = repoUrl + '/tree/master/' + fullPath;
87+
console.log('[ContribButtons] View URL:', viewUrl);
88+
viewBtn.onclick = function() {
89+
window.open(viewUrl, '_blank');
90+
};
91+
}
92+
93+
if (editBtn) {
94+
var editUrl = repoUrl + '/edit/master/' + fullPath;
95+
console.log('[ContribButtons] Edit URL:', editUrl);
96+
editBtn.onclick = function() {
97+
window.open(editUrl, '_blank');
98+
};
99+
}
100+
101+
if (reportBtn) {
102+
var pageTitle = (document.querySelector('h1')?.textContent || 'page').trim().replace(/\s+/g, ' ');
103+
var reportUrl = repoUrl + '/issues/new?template=10-doc-issue-bug.md&title=Issue with `' + pageTitle + '` [' + relativePath + ']';
104+
console.log('[ContribButtons] Report URL:', reportUrl);
105+
reportBtn.onclick = function() {
106+
window.open(reportUrl, '_blank');
107+
};
108+
}
109+
110+
console.log('[ContribButtons] Button update complete');
111+
}
112+
17113
function setCookie(name, value, days = 365 * 100) {
18114
var expires = "";
19115
if (days) {

_js/custom/navigation.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ $(function () {
131131

132132
// Function to apply all of our custom modifications on the self loaded pages
133133
function finalizeContent(anchorId) {
134+
console.log('[Navigation] finalizeContent called for URL:', window.location.pathname);
134135
// Sync the sidebar with the current page url, migth be out of sync when the page is loaded initially from an inner url
135136
adjustSidebars();
136137
// There might be nav-links in the loaded new content as well (e.g.Next / Prev buttons
@@ -145,6 +146,9 @@ $(function () {
145146
addCodeBlocksTitle();
146147
// Add content tooltips
147148
addContentTooltips();
149+
// Update contribution buttons to point to the correct source files
150+
if (typeof updateContributionButtons === 'function')
151+
updateContributionButtons();
148152
// Try to scroll to a giben anchor, if any
149153
if (anchorId)
150154
scrollToAnchor(anchorId);

0 commit comments

Comments
 (0)