Commit 7e1761b

bryfry <bryon@fryer.io>
2026-01-28 14:31:41
Add raw file links to single file headers (#23)
1 parent 83be9dd
pkg/templates/header.gohtml
@@ -24,6 +24,23 @@
                     {{ end }}
                 </nav>
             {{ end }}
+            {{ if .RawHref }}
+                <div class="raw-actions">
+                    <a class="raw-btn" href="{{.RawHref}}">Raw</a>
+                    <button class="raw-btn has-tooltip" onclick="copyRaw(this, '{{.RawHref}}')">
+                        <svg aria-hidden="true" focusable="false" width="16" height="16">
+                            <use xlink:href="#copy"></use>
+                        </svg>
+                        <span class="tooltip">Copy raw content</span>
+                    </button>
+                    <a class="raw-btn has-tooltip" href="{{.RawHref}}" download>
+                        <svg aria-hidden="true" focusable="false" width="16" height="16">
+                            <use xlink:href="#download"></use>
+                        </svg>
+                        <span class="tooltip">Download raw file</span>
+                    </a>
+                </div>
+            {{ end }}
             <button class="goto-top" onclick="window.scrollTo({top: 0, behavior: 'smooth'})">
                 <svg aria-hidden="true" focusable="false" width="16" height="16">
                     <use xlink:href="#arrow-top"></use>
pkg/templates/layout.gohtml
@@ -265,6 +265,58 @@
         .goto-top:hover {
           background: var(--c-bg-elv);
         }
+
+        .raw-actions {
+          display: flex;
+          margin-left: auto;
+          gap: 2px;
+          align-items: center;
+        }
+
+        .raw-btn {
+          display: flex;
+          padding: 6px 10px;
+          background: none;
+          border: none;
+          border-radius: 6px;
+          align-items: center;
+          color: var(--c-text-1);
+          cursor: pointer;
+          font-size: 14px;
+          font-family: inherit;
+          text-decoration: none;
+        }
+
+        .raw-btn:hover {
+          background: var(--c-bg-elv);
+          color: var(--c-text-1);
+          text-decoration: none;
+        }
+
+        .has-tooltip {
+          position: relative;
+        }
+
+        .tooltip {
+          display: none;
+          position: absolute;
+          bottom: calc(100% + 6px);
+          left: 50%;
+          transform: translateX(-50%);
+          padding: 4px 8px;
+          background: var(--c-bg-elv);
+          border: 1px solid var(--c-border);
+          border-radius: 4px;
+          font-size: 12px;
+          white-space: nowrap;
+          color: var(--c-text-1);
+          pointer-events: none;
+        }
+
+        .has-tooltip:hover .tooltip,
+        .has-tooltip.copied .tooltip {
+          display: block;
+        }
     </style>
     {{ template "head" . }}
 </head>
@@ -315,5 +367,23 @@
 <footer>
     Generated by <a href="https://github.com/antonmedv/gitmal">Gitmal</a>
 </footer>
+<script>
+function copyRaw(btn, url) {
+    var tip = btn.querySelector('.tooltip');
+    var original = tip.textContent;
+    fetch(url)
+        .then(function(r) { return r.text(); })
+        .then(function(text) { return navigator.clipboard.writeText(text); })
+        .then(function() {
+            tip.textContent = 'Copied!';
+            btn.classList.add('copied');
+            setTimeout(function() {
+                btn.classList.remove('copied');
+                tip.textContent = original;
+            }, 2000);
+        })
+        .catch(function(err) { console.error('Copy failed:', err); });
+}
+</script>
 </body>
 </html>
pkg/templates/svg.gohtml
@@ -46,5 +46,13 @@
             <path fill="currentColor"
                   d="m11.28 3.22 4.25 4.25a.75.75 0 0 1 0 1.06l-4.25 4.25a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L13.94 8l-3.72-3.72a.749.749 0 0 1 .326-1.275.749.749 0 0 1 .734.215Zm-6.56 0a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042L2.06 8l3.72 3.72a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215L.47 8.53a.75.75 0 0 1 0-1.06Z"></path>
         </symbol>
+        <symbol id="copy" viewBox="0 0 16 16">
+            <path fill="currentColor"
+                  d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25ZM5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>
+        </symbol>
+        <symbol id="download" viewBox="0 0 16 16">
+            <path fill="currentColor"
+                  d="M2.75 14A1.75 1.75 0 0 1 1 12.25v-2.5a.75.75 0 0 1 1.5 0v2.5c0 .138.112.25.25.25h10.5a.25.25 0 0 0 .25-.25v-2.5a.75.75 0 0 1 1.5 0v2.5A1.75 1.75 0 0 1 13.25 14ZM7.25 7.689V2a.75.75 0 0 1 1.5 0v5.689l1.97-1.969a.749.749 0 1 1 1.06 1.06l-3.25 3.25a.749.749 0 0 1-1.06 0L4.22 6.78a.749.749 0 1 1 1.06-1.06Z"></path>
+        </symbol>
     </svg>
 {{ end }}
pkg/templates/templates.go
@@ -79,6 +79,7 @@ type HeaderParams struct {
 	Ref         git.Ref
 	Header      string
 	Breadcrumbs []Breadcrumb
+	RawHref     string
 }
 
 type Breadcrumb struct {
blob.go
@@ -98,6 +98,21 @@ func generateBlobs(files []git.Blob, params Params) error {
 						content = string(data)
 					}
 
+					// Write raw file for every blob
+					rawPath := filepath.Join(params.OutputDir, "raw", params.Ref.DirName(), blob.Path)
+					if err := os.MkdirAll(filepath.Dir(rawPath), 0o755); check(err) {
+						return
+					}
+					rf, err := os.Create(rawPath)
+					if check(err) {
+						return
+					}
+					if _, err := rf.Write(data); check(err) {
+						_ = rf.Close()
+						return
+					}
+					_ = rf.Close()
+
 					outPath := filepath.Join(params.OutputDir, "blob", params.Ref.DirName(), blob.Path) + ".html"
 					if err := os.MkdirAll(filepath.Dir(outPath), 0o755); check(err) {
 						return
@@ -116,6 +131,7 @@ func generateBlobs(files []git.Blob, params Params) error {
 						depth = len(strings.Split(blob.Path, "/")) - 1
 					}
 					rootHref := strings.Repeat("../", depth+2)
+					rawHref := filepath.Join(rootHref, "raw", params.Ref.DirName(), blob.Path)
 
 					if isMarkdown(blob.Path) {
 						var b bytes.Buffer
@@ -145,6 +161,7 @@ func generateBlobs(files []git.Blob, params Params) error {
 							HeaderParams: templates.HeaderParams{
 								Ref:         params.Ref,
 								Breadcrumbs: breadcrumbs(params.Name, blob.Path, true),
+								RawHref:     rawHref,
 							},
 							Blob:    blob,
 							Content: template.HTML(contentHTML),
@@ -170,23 +187,6 @@ func generateBlobs(files []git.Blob, params Params) error {
 
 						} else if isImg {
 
-							rawPath := filepath.Join(params.OutputDir, "raw", params.Ref.DirName(), blob.Path)
-							if err := os.MkdirAll(filepath.Dir(rawPath), 0o755); check(err) {
-								return
-							}
-
-							rf, err := os.Create(rawPath)
-							if check(err) {
-								return
-							}
-							defer func() {
-								_ = rf.Close()
-							}()
-
-							if _, err := rf.Write(data); check(err) {
-								return
-							}
-
 							relativeRawPath := filepath.Join(rootHref, "raw", params.Ref.DirName(), blob.Path)
 							contentHTML = template.HTML(fmt.Sprintf(`<img src="%s" alt="%s" />`, relativeRawPath, blob.FileName))
 						}
@@ -203,6 +203,7 @@ func generateBlobs(files []git.Blob, params Params) error {
 							HeaderParams: templates.HeaderParams{
 								Ref:         params.Ref,
 								Breadcrumbs: breadcrumbs(params.Name, blob.Path, true),
+								RawHref:     rawHref,
 							},
 							CSS:      template.CSS(css.String()),
 							Blob:     blob,