1
Files
2025/layouts/partials/external-links-report.html
Richard Dern fe8037bb73 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
2026-04-02 22:23:16 +02:00

162 lines
5.2 KiB
HTML

{{- $reportPath := "data/external_links.yaml" -}}
{{- $report := dict -}}
{{- $withLocations := false -}}
{{- if reflect.IsMap . -}}
{{- with index . "ReportPath" -}}
{{- $reportPath = . -}}
{{- end -}}
{{- with index . "Report" -}}
{{- $report = . -}}
{{- end -}}
{{- with index . "WithLocations" -}}
{{- $withLocations = . -}}
{{- end -}}
{{- else if . -}}
{{- $report = . -}}
{{- end -}}
{{- if eq (len $report) 0 -}}
{{- if eq $reportPath "data/external_links.yaml" -}}
{{- with index site.Data "external_links" -}}
{{- $report = . -}}
{{- end -}}
{{- else if fileExists $reportPath -}}
{{- $report = transform.Unmarshal (readFile $reportPath) -}}
{{- end -}}
{{- end -}}
{{- $sourceLinks := slice -}}
{{- with index $report "deadLinks" -}}
{{- $sourceLinks = . -}}
{{- end -}}
{{- if and (eq (len $sourceLinks) 0) (isset $report "links") -}}
{{- $sourceLinks = default (slice) (index $report "links") -}}
{{- end -}}
{{- $entries := default (dict) (index $report "entries") -}}
{{- $deletions := partialCached "deletions-lookup.html" (dict) "default" -}}
{{- $linksByURL := dict -}}
{{- range $sourceLinks -}}
{{- $code := "" -}}
{{- if isset . "code" -}}
{{- $rawCode := index . "code" -}}
{{- if ne $rawCode nil -}}
{{- $code = printf "%v" $rawCode -}}
{{- end -}}
{{- end -}}
{{- $statusText := "" -}}
{{- with index . "statusText" -}}
{{- $statusText = printf "%v" . -}}
{{- end -}}
{{- if eq $statusText "" -}}
{{- with index . "status" -}}
{{- $statusText = printf "%v" . -}}
{{- end -}}
{{- end -}}
{{- $status := "" -}}
{{- with $code -}}
{{- $status = . -}}
{{- end -}}
{{- if and (eq $status "") (ne $statusText "") -}}
{{- $status = $statusText -}}
{{- end -}}
{{- $url := strings.TrimSpace (printf "%v" (default "" (index . "url"))) -}}
{{- $linksByURL = merge $linksByURL (dict $url (dict
"url" $url
"code" $code
"statusText" $statusText
"status" $status
"locations" (default (slice) (index . "locations"))
"errorType" (default "" (index . "errorType"))
"checkedAt" (default "" (index . "checkedAt"))
"manualMarked" false
)) -}}
{{- end -}}
{{- range $url, $_ := $deletions -}}
{{- $existingLink := default (dict) (index $linksByURL $url) -}}
{{- $locations := default (slice) (index $existingLink "locations") -}}
{{- if $withLocations -}}
{{- $locations = slice -}}
{{- end -}}
{{- $linksByURL = merge $linksByURL (dict $url (merge $existingLink (dict
"url" $url
"status" "deleted"
"statusText" "Supprimé"
"locations" $locations
"manualMarked" true
))) -}}
{{- $existingEntry := default (dict) (index $entries $url) -}}
{{- $entries = merge $entries (dict $url (merge $existingEntry (dict
"status" "deleted"
"statusText" "Supprimé"
"manualMarked" true
))) -}}
{{- end -}}
{{- if $withLocations -}}
{{- range where site.RegularPages ".File" "!=" nil -}}
{{- $page := . -}}
{{- $pageLocation := dict
"file" (printf "content/%s" $page.File.Path)
"page" $page.Path
-}}
{{- $rawContent := "" -}}
{{- with $page.RawContent -}}
{{- $rawContent = . -}}
{{- end -}}
{{- if ne $rawContent "" -}}
{{- range $url, $_ := $deletions -}}
{{- if strings.Contains $rawContent $url -}}
{{- $existingLink := default (dict) (index $linksByURL $url) -}}
{{- $locations := default (slice) (index $existingLink "locations") -}}
{{- if eq (len (where $locations "file" $pageLocation.file)) 0 -}}
{{- $locations = $locations | append $pageLocation -}}
{{- $linksByURL = merge $linksByURL (dict $url (merge $existingLink (dict "locations" $locations))) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $dataResources := $page.Resources.Match "data/**/*.yaml" | default (slice) -}}
{{- range ($page.Resources.Match "data/**/*.yml" | default (slice)) -}}
{{- $dataResources = $dataResources | append . -}}
{{- end -}}
{{- range $dataResources -}}
{{- $resourceContent := strings.TrimSpace .Content -}}
{{- if ne $resourceContent "" -}}
{{- $resourceLocation := dict
"file" (printf "content/%s/%s" (path.Dir $page.File.Path) .Name)
"page" $page.Path
-}}
{{- range $url, $_ := $deletions -}}
{{- if strings.Contains $resourceContent $url -}}
{{- $existingLink := default (dict) (index $linksByURL $url) -}}
{{- $locations := default (slice) (index $existingLink "locations") -}}
{{- if eq (len (where $locations "file" $resourceLocation.file)) 0 -}}
{{- $locations = $locations | append $resourceLocation -}}
{{- $linksByURL = merge $linksByURL (dict $url (merge $existingLink (dict "locations" $locations))) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $links := slice -}}
{{- range $url, $link := $linksByURL -}}
{{- $links = $links | append $link -}}
{{- end -}}
{{- $links = sort $links "url" -}}
{{- return (dict
"generatedAt" (default "" (index $report "generatedAt"))
"entries" $entries
"links" $links
"linksMap" $linksByURL
) -}}