Ensure relative md links resolve and add nofollow
gitile/pages.scm
27 | 27 | #:use-module (srfi srfi-1); fold | |
28 | 28 | #:use-module (srfi srfi-19); date/time | |
29 | 29 | #:use-module (system foreign) | |
30 | + | #:use-module (web uri) | |
30 | 31 | #:export (not-yet-page | |
31 | 32 | project-file-raw | |
32 | 33 | project-files | |
… | |||
54 | 55 | path)) | |
55 | 56 | ||
56 | 57 | (define (file-content repo path ref repository-name) | |
58 | + | (define (ensure-relative-link link) | |
59 | + | "If link is relative, ensure it resolves relative to `path`" | |
60 | + | (if (string->uri link) | |
61 | + | link | |
62 | + | (string-append "/" repository-name "/tree/" (or ref "-") "/" (dirname path) | |
63 | + | "/" link))) | |
64 | + | ||
65 | + | (define check-links | |
66 | + | (match-lambda | |
67 | + | ((? string? s) s) | |
68 | + | (('a ('@ arg ...) content ...) | |
69 | + | `(a (@ (rel "nofollow") | |
70 | + | ,@(map | |
71 | + | (match-lambda | |
72 | + | (('href link) | |
73 | + | `(href ,(ensure-relative-link link))) | |
74 | + | (('rel _) | |
75 | + | `(rel "nofollow")) | |
76 | + | (arg arg)) | |
77 | + | arg)) | |
78 | + | ,@content)) | |
79 | + | ((tag ('@ arg ...) content ...) | |
80 | + | `(,tag (@ ,@arg) ,@(map check-links content))) | |
81 | + | ((tag content ...) | |
82 | + | `(,tag ,@(map check-links content))))) | |
83 | + | ||
57 | 84 | `((div (@ (class "content")) | |
58 | 85 | (p ,(basename path)) | |
59 | 86 | (p (@ (class "button-row")) | |
… | |||
63 | 90 | ,(let ((content (utf8->string (get-file-content repo path #:ref ref)))) | |
64 | 91 | (if (string-suffix? ".md" path) | |
65 | 92 | `(article (@ (class "file-content")) | |
66 | - | ,(commonmark->sxml content)) | |
93 | + | ,(check-links (commonmark->sxml content))) | |
67 | 94 | `(pre ,content))))) | |
68 | 95 | ||
69 | 96 | (define* (project-files repository-name repo #:key (ref "-") (path '())) |