|
9 | 9 | const searchFromMastHead = {% if site.masthead != false and site.search_from_masthead == true %} true {% else %} false {% endif %}; |
10 | 10 |
|
11 | 11 | const docRoot = '{{ site.baseurl }}'; |
| 12 | + const repoUrl = '{{ site.github.repository_url }}'; |
| 13 | + const collectionsDir = '{{ site.collections_dir }}'; |
12 | 14 |
|
13 | 15 | function docPrefix() { |
14 | 16 | return (docRoot != '' ? docRoot + '/' : ''); |
15 | 17 | } |
16 | 18 |
|
| 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 | + |
17 | 113 | function setCookie(name, value, days = 365 * 100) { |
18 | 114 | var expires = ""; |
19 | 115 | if (days) { |
|
0 commit comments