1

Sécuriser et documenter les liens morts

Rend les liens supprimés non cliquables, améliore le rapport des liens morts et accélère la génération du site.

Closes #2
Closes #3
Closes #4
This commit is contained in:
2026-04-02 22:23:16 +02:00
parent 0e4e8cd076
commit fe8037bb73
40 changed files with 423 additions and 112 deletions

View File

@@ -1,7 +1,6 @@
{{- $destination := .Destination -}}
{{- if hasPrefix $destination "~~" -}}
{{- $destination = replaceRE "^~~" "" $destination -}}
{{- $destination = replaceRE "~~([/?#]|$)" "$1" $destination -}}
{{- if strings.Contains $destination "~~" -}}
{{- $destination = replace $destination "~~" "" -}}
{{- end -}}
{{- $isExternal := or (strings.HasPrefix $destination "http://") (strings.HasPrefix $destination "https://") -}}
{{- $host := "" -}}
@@ -17,12 +16,41 @@
{{- end -}}
{{- $page := .Page -}}
{{- $site := $page.Site -}}
{{- $deletions := partialCached "deletions-lookup.html" (dict) "default" -}}
{{- $replacements := default (dict) $site.Data.replacements -}}
{{- $aff := index $site.Data.affiliates.sites $host -}}
{{- $isAffiliated := false -}}
{{- $newURL := $destination -}}
{{- $replacementURL := index $replacements $destination -}}
{{- $hasReplacement := ne $replacementURL nil -}}
{{- $lookupURLs := slice $destination -}}
{{- with $destination -}}
{{- if strings.HasSuffix . "/" -}}
{{- $withoutSlash := strings.TrimSuffix "/" . -}}
{{- if ne $withoutSlash . -}}
{{- $lookupURLs = $lookupURLs | append $withoutSlash -}}
{{- end -}}
{{- else -}}
{{- $lookupURLs = $lookupURLs | append (printf "%s/" .) -}}
{{- end -}}
{{- end -}}
{{- $isDeleted := false -}}
{{- range $lookupURLs -}}
{{- if and (not $isDeleted) (isset $deletions .) -}}
{{- $isDeleted = true -}}
{{- end -}}
{{- end -}}
{{- $replacementURL := "" -}}
{{- $hasReplacement := false -}}
{{- if not $isDeleted -}}
{{- range $lookupURLs -}}
{{- if not $hasReplacement -}}
{{- $candidateReplacementURL := index $replacements . -}}
{{- if ne $candidateReplacementURL nil -}}
{{- $replacementURL = $candidateReplacementURL -}}
{{- $hasReplacement = true -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $customClass := "" -}}
{{- if reflect.IsMap . -}}
@@ -44,6 +72,18 @@
{{- end -}}
{{- end -}}
{{- $newHost := "" -}}
{{- if and $isExternal (gt (len (findRE "^https?://[^[:space:]]+$" $newURL 1)) 0) -}}
{{- $newParsed := urls.Parse $newURL -}}
{{- $newHost = lower $newParsed.Host -}}
{{- end -}}
{{- $isArchiveLink := false -}}
{{- if and $isExternal (ne $newHost "") -}}
{{- if or (eq $newHost "archive.org") (strings.HasSuffix $newHost ".archive.org") -}}
{{- $isArchiveLink = true -}}
{{- end -}}
{{- end -}}
{{- $internalPage := "" -}}
{{- $internalPageFound := false -}}
{{- if not $isExternal -}}
@@ -66,15 +106,22 @@
{{- end -}}
{{- end -}}
{{- $report := partial "external-links-report.html" (dict) -}}
{{- $deadList := default (slice) (index $report "links") -}}
{{- $report := partialCached "external-links-report.html" (dict) "default" -}}
{{- $entriesMap := default (dict) (index $report "entries") -}}
{{- $cacheEntry := index $entriesMap $destination -}}
{{- $deadLinksMap := default (dict) (index $report "linksMap") -}}
{{- $cacheEntry := dict -}}
{{- range $lookupURLs -}}
{{- if and (eq (len $cacheEntry) 0) (isset $entriesMap .) -}}
{{- $cacheEntry = index $entriesMap . -}}
{{- end -}}
{{- end -}}
{{- $deadInfo := dict -}}
{{- $isDeadLink := false -}}
{{- with (first 1 (where $deadList "url" $destination)) -}}
{{- $deadInfo = index . 0 -}}
{{- $isDeadLink = true -}}
{{- range $lookupURLs -}}
{{- if and (not $isDeadLink) (isset $deadLinksMap .) -}}
{{- $deadInfo = index $deadLinksMap . -}}
{{- $isDeadLink = true -}}
{{- end -}}
{{- end -}}
{{- $deadStatus := "" -}}
{{- with (index $deadInfo "status") -}}
@@ -89,6 +136,15 @@
{{- if and $isDeadLink (eq $deadStatus "404") -}}
{{- $isConfirmedDeadLink = true -}}
{{- end -}}
{{- $isMarkedDeadLink := false -}}
{{- with (index $deadInfo "manualMarked") -}}
{{- $isMarkedDeadLink = . -}}
{{- end -}}
{{- if not $isMarkedDeadLink -}}
{{- with (index $cacheEntry "manualMarked") -}}
{{- $isMarkedDeadLink = . -}}
{{- end -}}
{{- end -}}
{{- $titleParts := slice -}}
{{- if $isAffiliated -}}
@@ -96,6 +152,9 @@
{{- else if $isExternal -}}
{{- $titleParts = $titleParts | append "Lien externe" -}}
{{- end -}}
{{- if $isArchiveLink -}}
{{- $titleParts = $titleParts | append "Internet Archive" -}}
{{- end -}}
{{- if and (not $isExternal) $internalPageFound -}}
{{- $internalTitle := $internalPage.LinkTitle | default $internalPage.Title -}}
{{- with $internalPage.Date -}}
@@ -109,6 +168,9 @@
{{- end -}}
{{- if $isDeadLink -}}
{{- $deadDetails := slice -}}
{{- if $isMarkedDeadLink -}}
{{- $deadDetails = $deadDetails | append "supprimé" -}}
{{- end -}}
{{- with (index $cacheEntry "checkedAt") -}}
{{- $deadDetails = $deadDetails | append (printf "inaccessible depuis le %s" (time.Format "02/01/2006" (time .))) -}}
{{- end -}}
@@ -116,7 +178,10 @@
{{- $deadDetails = $deadDetails | append (printf "raison %s" .) -}}
{{- end -}}
{{- with (index $deadInfo "status") -}}
{{- $deadDetails = $deadDetails | append (printf "statut %v" .) -}}
{{- $statusLabel := printf "%v" . -}}
{{- if not (and $isMarkedDeadLink (or (eq $statusLabel "manual") (eq $statusLabel "deleted"))) -}}
{{- $deadDetails = $deadDetails | append (printf "statut %s" $statusLabel) -}}
{{- end -}}
{{- end -}}
{{- if gt (len $deadDetails) 0 -}}
{{- $titleParts = $titleParts | append (printf "(%s)" (delimit $deadDetails " ; ")) -}}
@@ -134,11 +199,17 @@
{{- if $isAffiliated -}}
{{- $linkClasses = $linkClasses | append "link-affiliated" -}}
{{- end -}}
{{- if $isArchiveLink -}}
{{- $linkClasses = $linkClasses | append "link-archive" -}}
{{- end -}}
{{- if $isDeadLink -}}
{{- $linkClasses = $linkClasses | append "link-dead" -}}
{{- if $isConfirmedDeadLink -}}
{{- $linkClasses = $linkClasses | append "link-dead-404" -}}
{{- end -}}
{{- if $isMarkedDeadLink -}}
{{- $linkClasses = $linkClasses | append "link-dead-marked" -}}
{{- end -}}
{{- end -}}
{{- with $customClass -}}
{{- range (split . " ") -}}
@@ -148,4 +219,10 @@
{{- end -}}
{{- end -}}
{{- $classValue := delimit $linkClasses " " -}}
<a href="{{- $newURL -}}"{{ with $classValue }} class="{{- . -}}"{{ end }}{{ with $titleValue }} title="{{- . -}}"{{ end }}{{ if $isExternal }} rel="noreferrer noopener"{{ end }}>{{- strings.TrimSpace .Text | safeHTML -}}</a>{{- "" -}}
{{- if $isMarkedDeadLink -}}
{{- $deadTitleValue := printf "URL d'origine : %s" $destination -}}
{{- $deadAriaLabel := printf "Lien supprimé. URL d'origine : %s" $destination -}}
<del class="link-dead link-dead-marked"><span{{ with $classValue }} class="{{- . -}}"{{ end }} title="{{- $deadTitleValue -}}" aria-label="{{- $deadAriaLabel -}}">{{- strings.TrimSpace .Text | safeHTML -}}</span></del>
{{- else -}}
<a href="{{- $newURL -}}"{{ with $classValue }} class="{{- . -}}"{{ end }}{{ with $titleValue }} title="{{- . -}}"{{ end }}{{ if $isExternal }} rel="noreferrer noopener"{{ end }}>{{- strings.TrimSpace .Text | safeHTML -}}</a>
{{- end -}}{{- "" -}}