Gestion et affichage des liens morts
This commit is contained in:
@@ -19,6 +19,10 @@ main:
|
||||
title: Un texte d’intention sur la démarche du site
|
||||
pageRef: /manifeste/
|
||||
parent: Accueil
|
||||
- name: Liens morts
|
||||
title: Rapport sur les liens morts du site
|
||||
pageRef: /liens-morts/
|
||||
parent: Accueil
|
||||
- name: Revue de presse
|
||||
title: "Mes apparitions sur le web"
|
||||
pageRef: /revue-de-presse/
|
||||
|
||||
99
content/liens-morts/index.md
Normal file
99
content/liens-morts/index.md
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Liens morts
|
||||
---
|
||||
|
||||
Il est inévitable, surtout quand on crée beaucoup de liens, de se retrouver confronter à des liens morts.
|
||||
C'est ce que l'on appelle le [_link-rot_](https://en.wikipedia.org/wiki/Link_rot).
|
||||
|
||||
Par soucis de transparence, pour faciliter le suivi des liens morts et pour inciter mes éventuels lecteurs vers lesquels j'ai créé un lien devenu mort à [m'indiquer](/contact/) comment le corriger, je présente ici une page générée automatiquement, contenant le rapport des liens morts détectés sur mon site.
|
||||
|
||||
Je m'efforce d'automatiser le processus de détection de ces liens morts, autant pour les liens internes à mon site que pour les liens externes.
|
||||
S'il est parfaitement légitime de me tenir pour responsable de la vivacité de mes propres liens internes, **personne ne peut me rendre responsable des liens externes**.
|
||||
Ce n'est pas mon travail.
|
||||
Je n'ai aucune obligation de maintenir un outil de vérification et la transparence des résultats.
|
||||
Je le fais par plaisir du travail bien fait et par respect pour mes visiteurs, mais je n'ai aucune emprise sur les nombreux facteurs externes déterminant si un lien est accessible ou non par mon outil.
|
||||
|
||||
## Méthodologie
|
||||
|
||||
J'ai créé un script exploitant [cURL](https://curl.se/docs/) avec les paramètres suivants :
|
||||
|
||||
```javascript
|
||||
const args = [
|
||||
"--silent",
|
||||
"--location",
|
||||
"--fail",
|
||||
"--max-time",
|
||||
`${REQUEST_TIMEOUT_SECONDS}`,
|
||||
"--output",
|
||||
"/dev/null",
|
||||
"--write-out",
|
||||
"%{http_code}",
|
||||
"--user-agent",
|
||||
DEFAULT_USER_AGENT,
|
||||
"--request",
|
||||
method,
|
||||
url,
|
||||
];
|
||||
```
|
||||
|
||||
`DEFAULT_USER_AGENT` est un UA valide et régulièrement mis à jour.
|
||||
Je fais une première requête avec la méthode [`HEAD`](https://developer.mozilla.org/fr/docs/Web/HTTP/Reference/Methods/HEAD), et si cette requête échoue, j'en envoie une autre avec la méthode `GET`, après un délais de 5s.
|
||||
|
||||
Trois cas de figure se présentent à ce stade.
|
||||
|
||||
### Code HTTP entre 200 et 400
|
||||
|
||||
Mon outil considère systématiquement qu'un code HTTP supérieur à 200 et strictement inférieur à 400 est une page accessible.
|
||||
|
||||
Cela peut générer des faux positifs (des pages considérées comme accessibles, mais qui ne le sont pas), notamment dans les cas suivants :
|
||||
|
||||
- Si le site affiche une page d'erreur sans relayer le code HTTP correspondant à l'erreur
|
||||
- L'URL est conservée pour un contenu totalement différent de la page originale
|
||||
|
||||
Lorsque je constate qu'un URL retourne un code strictement inférieur à 400, il n'est pas re-testé avant 1 mois.
|
||||
|
||||
### Code HTTP entre 400 et 499
|
||||
|
||||
Toute réponse avec un code HTTP compris entre 400 et 499 est considérée comme une erreur, dans le respect de la [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231).
|
||||
|
||||
Cela génère de nombreux faux négatifs (des pages considérées comme inaccessibles alors qu'elles le sont), symptomatiques d'une volonté de blocage des techniques de navigation automatisée, ou d'un problème de paramétrage de mon outil.
|
||||
|
||||
Par construction, par honnêteté intellectuelle et par bienveillance, mon outil est développé de manière à ne pas être intrusif.
|
||||
Son "paramétrage" permettrait en théorie d'exploiter des techniques plus agressives afin de limiter ces faux négatifs.
|
||||
J'ai fait le choix délibéré de ne pas rendre mon outil plus agressif, et de marquer tout lien retournant un code supérieur ou égal à 400 comme étant inaccessible, peu importe la raison réelle.
|
||||
|
||||
Je considère que ne pas respecter la RFC 7231 est une pratique destructive.
|
||||
Donc les serveurs qui répondent avec un code inapproprié doivent être marqués comme étant inaccessibles.
|
||||
|
||||
Le problème ici est que, si l'on retourne une erreur 403 pour un contenu qui existe réellement, sous prétexte que la navigation ne s'est pas faite avec un navigateur "traditionnel", il n'est pas possible pour moi de savoir si la page a été déplacée, si j'ai commis une erreur dans le copier-coller de l'URL, ou si j'ai accédé à un URL protégé par un mot de passe (un exemple de motif légitime d'utilisation de l'erreur 403).
|
||||
|
||||
Il existe trop de ces cas de figure pour que j'accepte de prendre le temps de les identifier manuellement.
|
||||
|
||||
Les requêtes ayant abouti à un code HTTP compris entre 400 et 499 ne sont pas réitérées avant 1 semaine.
|
||||
|
||||
### Code HTTP supérieur ou égal à 500
|
||||
|
||||
Les requêtes ayant abouti à un code HTTP supérieur ou égal à 500 ne sont pas réitérées avant 1 jour : ces erreurs sont censées être légitimes, transitoires et promptement corrigées.
|
||||
|
||||
J'ai néanmoins identifié que certains serveurs répondent à un navigateur automatisé avec une erreur 500.
|
||||
Je refuse de constituer et de maintenir une liste de ces serveurs.
|
||||
|
||||
### Timeout
|
||||
|
||||
De nombreux sites ont fait le choix de punir la navigation automatisée en ne répondant tout simplement pas à la requête, en laissant le client "tourner dans le vide".
|
||||
Il n'est donc pas possible, pour un script bienveillant, de savoir si le serveur distant bloque la requête ou s'il s'agit d'un problème transitoire.
|
||||
|
||||
On pourrait ergoter longtemps sur le bienfondé (ou pas) de cette technique.
|
||||
Pour ma part, je considère qu'elle est destructive.
|
||||
Donc les serveurs qui ne répondent jamais doivent être marqués comme étant inaccessibles, parce que certains d'entre eux peuvent réellement être temporairement inaccessibles.
|
||||
|
||||
Les requêtes ayant abouti à un _timeout_ ne sont pas renouvelées avant 1 semaine.
|
||||
|
||||
### Autres cas
|
||||
|
||||
Il arrive que cURL me renvoie une erreur HTTP 0 (qui n'existe pas réellement).
|
||||
L'examen des journaux détaillés de ces requêtes m'apprend qu'en général (mais pas toujours), le problème est essentiellement lié aux certificats du serveur (obsolescence, nom de domaine qui ne correspond pas, etc.).
|
||||
|
||||
Les requêtes aboutissant à un code HTTP 0 ne sont pas renouvelées avant 1 semaine.
|
||||
|
||||
## Rapport
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"scripts": {
|
||||
"links:refresh": "node tools/run_link_checks.js"
|
||||
"links:refresh": "node tools/check_external_links.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"postcss-import": "^16.1.0",
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
a {
|
||||
color: var(--color-link);
|
||||
text-decoration: underline;
|
||||
display: inline-block;
|
||||
max-width: 100%;
|
||||
overflow-wrap: anywhere;
|
||||
word-break: break-word;
|
||||
white-space: normal;
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
|
||||
@@ -44,3 +44,8 @@ tfoot td {
|
||||
padding: var(--padding-half) var(--padding);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.table-wrapper {
|
||||
width: 100%;
|
||||
overflow-x: auto;
|
||||
}
|
||||
@@ -7,22 +7,16 @@
|
||||
{{- $site := $page.Site -}}
|
||||
{{- $aff := index $site.Data.affiliates.sites $host -}}
|
||||
{{- $isAffiliated := false -}}
|
||||
{{- $scratch := $page.Scratch -}}
|
||||
{{- $externalCache := $scratch.Get "externalLinksCache" -}}
|
||||
{{- if not $externalCache -}}
|
||||
{{- $externalCache = dict -}}
|
||||
{{- if fileExists "tools/cache/external_links.yaml" -}}
|
||||
{{- with readFile "tools/cache/external_links.yaml" -}}
|
||||
{{- $parsedCache := transform.Unmarshal . -}}
|
||||
{{- if $parsedCache -}}
|
||||
{{- $externalCache = $parsedCache -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $scratch.Set "externalLinksCache" $externalCache -}}
|
||||
{{- $report := default (dict) (transform.Unmarshal (readFile "tools/cache/external_links.yaml")) -}}
|
||||
{{- $deadList := default (slice) (index $report "links") -}}
|
||||
{{- $entriesMap := default (dict) (index $report "entries") -}}
|
||||
{{- $cacheEntry := index $entriesMap .Destination -}}
|
||||
{{- $deadInfo := dict -}}
|
||||
{{- $isDeadLink := false -}}
|
||||
{{- with (first 1 (where $deadList "url" .Destination)) -}}
|
||||
{{- $deadInfo = index . 0 -}}
|
||||
{{- $isDeadLink = true -}}
|
||||
{{- end -}}
|
||||
{{- $cacheEntry := index $externalCache .Destination -}}
|
||||
{{- $isDeadLink := and $cacheEntry (eq (index $cacheEntry "manually_killed") true) -}}
|
||||
{{- $newURL := .Destination -}}
|
||||
{{- if and $isExternal $aff -}}
|
||||
{{- $param := $aff.param -}}
|
||||
@@ -34,12 +28,7 @@
|
||||
{{- $newURL = printf "%s://%s%s?%s=%s" $parsed.Scheme $host $path $param $value -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $titlePrefix := "" -}}
|
||||
{{- if $isAffiliated -}}
|
||||
{{- $titlePrefix = "Lien affilié" -}}
|
||||
{{- else if $isExternal -}}
|
||||
{{- $titlePrefix = "Lien externe" -}}
|
||||
{{- end -}}
|
||||
{{- $titlePrefix := cond $isAffiliated "Lien affilié" (cond $isExternal "Lien externe" "") -}}
|
||||
{{- $classes := slice -}}
|
||||
{{- if $isExternal -}}
|
||||
{{- $classes = $classes | append "external" -}}
|
||||
@@ -50,7 +39,30 @@
|
||||
{{- if $isDeadLink -}}
|
||||
{{- $classes = $classes | append "dead" -}}
|
||||
{{- end -}}
|
||||
<a href="{{ $newURL }}" title="{{ $titlePrefix }}{{ .Title }}"
|
||||
{{- $titleParts := slice -}}
|
||||
{{- with $titlePrefix -}}
|
||||
{{- $titleParts = $titleParts | append . -}}
|
||||
{{- end -}}
|
||||
{{- with .Title -}}
|
||||
{{- $titleParts = $titleParts | append . -}}
|
||||
{{- end -}}
|
||||
{{- if $isDeadLink -}}
|
||||
{{- $deadDetails := slice -}}
|
||||
{{- with (index $cacheEntry "checkedAt") -}}
|
||||
{{- $deadDetails = $deadDetails | append (printf "inaccessible depuis le %s" (time.Format "02/01/2006" (time .))) -}}
|
||||
{{- end -}}
|
||||
{{- with (index $cacheEntry "errorType") -}}
|
||||
{{- $deadDetails = $deadDetails | append (printf "raison %s" .) -}}
|
||||
{{- end -}}
|
||||
{{- with (index $deadInfo "status") -}}
|
||||
{{- $deadDetails = $deadDetails | append (printf "statut %v" .) -}}
|
||||
{{- end -}}
|
||||
{{- with $deadDetails -}}
|
||||
{{- $titleParts = $titleParts | append (printf "(%s)" (delimit . " ; ")) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $titleValue := delimit $titleParts " - " -}}
|
||||
<a href="{{ $newURL }}" title="{{ $titleValue }}"
|
||||
{{- if $isExternal -}} rel="noreferrer"{{- end -}}
|
||||
{{- if gt (len $classes) 0 -}} class="{{ delimit $classes ` ` }}"{{- end -}}>
|
||||
{{- .Text | safeHTML -}}
|
||||
|
||||
82
themes/42/layouts/_partials/liens-morts/report.html
Normal file
82
themes/42/layouts/_partials/liens-morts/report.html
Normal file
@@ -0,0 +1,82 @@
|
||||
{{- $defaultReportPath := "tools/cache/external_links.yaml" -}}
|
||||
{{- $reportPath := default $defaultReportPath .ReportPath -}}
|
||||
{{- $report := default (dict) .Report -}}
|
||||
{{- if or (eq (len $report) 0) (not (isset $report "links")) -}}
|
||||
{{- if fileExists $reportPath -}}
|
||||
{{- with readFile $reportPath -}}
|
||||
{{- $report = . | unmarshal -}}
|
||||
{{- end -}}
|
||||
{{- else -}}
|
||||
{{- warnf "Rapport des liens morts introuvable (%s)" $reportPath -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $allPages := where site.Pages ".File" "!=" nil -}}
|
||||
{{- $links := default (slice) $report.links -}}
|
||||
{{- $linkCount := len $links -}}
|
||||
{{- $generatedLabel := "" -}}
|
||||
{{- with $report.generatedAt -}}
|
||||
{{- $ts := time . -}}
|
||||
{{- $generatedLabel = $ts.Format "02/01/2006" -}}
|
||||
{{- end -}}
|
||||
|
||||
<div aria-label="Statistiques" class="stats">
|
||||
{{ partial "stat.html" (dict "title" "Dernière mise à jour" "value" $generatedLabel)}}
|
||||
{{ partial "stat.html" (dict "title" "Liens morts détectés" "value" $linkCount)}}
|
||||
</div>
|
||||
|
||||
<div class="table-wrapper">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>URL</th>
|
||||
<th>Emplacements</th>
|
||||
<th>Statut</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- range $links }}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ .url }}" rel="noreferrer noopener" target="_blank">{{ .url }}</a>
|
||||
</td>
|
||||
<td>
|
||||
{{- $locations := default (slice) .locations -}}
|
||||
{{- if gt (len $locations) 0 -}}
|
||||
{{- range $locations }}
|
||||
{{- $file := .file -}}
|
||||
{{- $line := .line -}}
|
||||
{{- $pagePath := .page -}}
|
||||
{{- $matchedPage := false -}}
|
||||
{{- if $pagePath -}}
|
||||
{{- $candidate := site.GetPage $pagePath -}}
|
||||
{{- if $candidate -}}
|
||||
{{- $matchedPage = $candidate -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if and (not $matchedPage) $file -}}
|
||||
{{- $normalized := replaceRE "^content/" "" $file -}}
|
||||
{{- $candidates := where $allPages "File.Path" $normalized -}}
|
||||
{{- if gt (len $candidates) 0 -}}
|
||||
{{- $matchedPage = index $candidates 0 -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $matchedPage -}}
|
||||
<a href="{{ $matchedPage.RelPermalink }}">{{ $matchedPage.Title }}</a>
|
||||
{{- else if $file -}}
|
||||
<code>{{ $file }}{{ if $line }}:{{ $line }}{{ end }}</code>
|
||||
{{- else -}}
|
||||
<em>Emplacement inconnu</em>
|
||||
{{- end -}}
|
||||
<br />
|
||||
{{- end }}
|
||||
{{- else -}}
|
||||
<em>Emplacements inconnus</em>
|
||||
{{- end -}}
|
||||
</td>
|
||||
<td><code>{{ .status }}</code></td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
34
themes/42/layouts/liens-morts/single.html
Normal file
34
themes/42/layouts/liens-morts/single.html
Normal file
@@ -0,0 +1,34 @@
|
||||
{{ define "main" }}
|
||||
{{ partial "hero-page.html" . }}
|
||||
<main>
|
||||
<article class="article-body">
|
||||
|
||||
{{- if eq .Page.Parent.RelPermalink "/interets/liens-interessants/" -}}
|
||||
{{- else -}}
|
||||
{{- if .Params.cover -}}
|
||||
{{- partial "media/render-image.html" (dict
|
||||
"Page" .Page
|
||||
"Destination" .Params.cover
|
||||
) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{ with .TableOfContents }}
|
||||
{{ if gt (len (plainify .)) 0 }}
|
||||
<div class="stat">
|
||||
<strong>
|
||||
Sommaire
|
||||
</strong>
|
||||
{{ . | safeHTML }}
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ .Content }}
|
||||
|
||||
{{- partial "liens-morts/report.html" (dict "Page" .) -}}
|
||||
|
||||
{{- partial "asides/keywords.html" . }}
|
||||
</article>
|
||||
</main>
|
||||
{{ end }}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,102 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const http = require("http");
|
||||
const readline = require("readline");
|
||||
|
||||
const BASE_URL = "http://127.0.0.1:1313";
|
||||
const CONTENT_DIR = path.join(__dirname, "..", "content");
|
||||
const SITE_ROOT = path.resolve(__dirname, "..");
|
||||
const BAD_LINKS = [];
|
||||
|
||||
function isInternalLink(link) {
|
||||
return !link.includes("://") && !link.startsWith("mailto:") && !link.startsWith("tel:");
|
||||
}
|
||||
|
||||
function extractLinksFromLine(line) {
|
||||
const regex = /\]\(([^)"]+)\)/g;
|
||||
let match;
|
||||
const links = [];
|
||||
while ((match = regex.exec(line)) !== null) {
|
||||
links.push(match[1]);
|
||||
}
|
||||
return links;
|
||||
}
|
||||
|
||||
function getBundleRelativeUrl(mdPath, link) {
|
||||
const bundleRoot = path.dirname(mdPath);
|
||||
let urlPath;
|
||||
|
||||
if (link.startsWith("/")) {
|
||||
urlPath = link;
|
||||
} else {
|
||||
const fullPath = path.resolve(bundleRoot, link);
|
||||
const relative = path.relative(CONTENT_DIR, fullPath);
|
||||
urlPath = "/" + relative.replace(/\\/g, "/");
|
||||
}
|
||||
|
||||
return urlPath;
|
||||
}
|
||||
|
||||
async function checkLink(file, lineNumber, link) {
|
||||
const relativeUrl = getBundleRelativeUrl(file, link);
|
||||
const fullUrl = `${BASE_URL}${relativeUrl}`;
|
||||
return new Promise((resolve) => {
|
||||
http.get(fullUrl, (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
BAD_LINKS.push([path.relative(SITE_ROOT, file), link, lineNumber]);
|
||||
}
|
||||
res.resume();
|
||||
resolve();
|
||||
}).on("error", () => {
|
||||
BAD_LINKS.push([path.relative(SITE_ROOT, file), link, lineNumber]);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function processFile(filePath) {
|
||||
const fileStream = fs.createReadStream(filePath);
|
||||
const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });
|
||||
let lineNumber = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
lineNumber++;
|
||||
const links = extractLinksFromLine(line);
|
||||
for (const link of links) {
|
||||
if (isInternalLink(link)) {
|
||||
process.stdout.write(".");
|
||||
await checkLink(filePath, lineNumber, link);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function walk(dir) {
|
||||
let results = [];
|
||||
const list = fs.readdirSync(dir);
|
||||
list.forEach((file) => {
|
||||
file = path.resolve(dir, file);
|
||||
const stat = fs.statSync(file);
|
||||
if (stat && stat.isDirectory()) {
|
||||
results = results.concat(walk(file));
|
||||
} else if (file.endsWith(".md")) {
|
||||
results.push(file);
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const files = walk(CONTENT_DIR);
|
||||
console.log(`Analyzing ${files.length} Markdown files...`);
|
||||
for (const file of files) {
|
||||
await processFile(file);
|
||||
}
|
||||
|
||||
console.log("\n\n=== Broken Internal Links Report ===");
|
||||
if (BAD_LINKS.length === 0) {
|
||||
console.log("✅ No broken internal links found.");
|
||||
} else {
|
||||
console.table(BAD_LINKS.map(([f, u, l]) => ({ File: f + '#' + l, URL: u })));
|
||||
}
|
||||
})();
|
||||
@@ -1,450 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const yaml = require("js-yaml");
|
||||
|
||||
const SITE_ROOT = path.resolve(__dirname, "..");
|
||||
const CONFIG_PATH = path.join(__dirname, "config.json");
|
||||
|
||||
function loadConfig() {
|
||||
if (!fs.existsSync(CONFIG_PATH)) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Impossible de parser ${path.relative(SITE_ROOT, CONFIG_PATH)} (${error.message}).`
|
||||
);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
const config = loadConfig();
|
||||
const externalConfig = {
|
||||
cacheDir: path.join(__dirname, "cache"),
|
||||
cacheFile: "external_links.yaml",
|
||||
...(config.externalLinks || {}),
|
||||
};
|
||||
|
||||
const CACHE_DIR = path.isAbsolute(externalConfig.cacheDir)
|
||||
? externalConfig.cacheDir
|
||||
: path.resolve(SITE_ROOT, externalConfig.cacheDir);
|
||||
const CACHE_PATH = path.isAbsolute(externalConfig.cacheFile)
|
||||
? externalConfig.cacheFile
|
||||
: path.join(CACHE_DIR, externalConfig.cacheFile);
|
||||
|
||||
function loadCache(cachePath) {
|
||||
if (!fs.existsSync(cachePath)) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return yaml.load(fs.readFileSync(cachePath, "utf8")) || {};
|
||||
} catch (error) {
|
||||
console.error(`Erreur lors de la lecture du cache YAML (${error.message}).`);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function getCheckedDate(info) {
|
||||
if (info && typeof info.checked === "string") {
|
||||
const parsed = new Date(info.checked);
|
||||
if (!Number.isNaN(parsed.valueOf())) {
|
||||
return parsed.toISOString();
|
||||
}
|
||||
}
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function getStatusCode(info) {
|
||||
if (info && typeof info.status === "number") {
|
||||
return info.status;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const frenchDateFormatter = new Intl.DateTimeFormat("fr-FR", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
function formatDisplayDate(isoString) {
|
||||
if (typeof isoString === "string") {
|
||||
const parsed = new Date(isoString);
|
||||
if (!Number.isNaN(parsed.valueOf())) {
|
||||
return frenchDateFormatter.format(parsed);
|
||||
}
|
||||
}
|
||||
return frenchDateFormatter.format(new Date());
|
||||
}
|
||||
|
||||
function getFilesForUrl(info) {
|
||||
if (!info) return [];
|
||||
if (Array.isArray(info.files) && info.files.length > 0) {
|
||||
return info.files;
|
||||
}
|
||||
if (Array.isArray(info.locations) && info.locations.length > 0) {
|
||||
return Array.from(new Set(info.locations.map((entry) => String(entry).split(":")[0])));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function splitFrontmatter(content) {
|
||||
if (!content.startsWith("---")) {
|
||||
return null;
|
||||
}
|
||||
const match = content.match(/^---\n([\s\S]*?)\n---\n?/);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const frontmatterText = match[1];
|
||||
let frontmatter = {};
|
||||
try {
|
||||
frontmatter = yaml.load(frontmatterText) || {};
|
||||
} catch (error) {
|
||||
console.error(`Frontmatter YAML invalide (${error.message}).`);
|
||||
return null;
|
||||
}
|
||||
const block = match[0];
|
||||
const body = content.slice(block.length);
|
||||
return { frontmatter, block, body };
|
||||
}
|
||||
|
||||
function escapeRegExp(value) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
function ensureTrailingNewline(value) {
|
||||
if (!value.endsWith("\n")) {
|
||||
return `${value}\n`;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function ensureBlankLineBeforeAppend(body) {
|
||||
if (body.endsWith("\n\n")) {
|
||||
return body;
|
||||
}
|
||||
if (body.endsWith("\n")) {
|
||||
return `${body}\n`;
|
||||
}
|
||||
return `${body}\n\n`;
|
||||
}
|
||||
|
||||
function markInterestingLink(filePath, url, info) {
|
||||
const original = fs.readFileSync(filePath, "utf8");
|
||||
const parsed = splitFrontmatter(original);
|
||||
if (!parsed) {
|
||||
console.warn(`Frontmatter introuvable pour ${path.relative(SITE_ROOT, filePath)}, ignoré.`);
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
const { frontmatter } = parsed;
|
||||
let body = parsed.body;
|
||||
const checkedDate = getCheckedDate(info);
|
||||
const displayDate = formatDisplayDate(checkedDate);
|
||||
const httpCode = getStatusCode(info);
|
||||
let changed = false;
|
||||
|
||||
if (typeof frontmatter.title === "string" && !frontmatter.title.startsWith("[Lien mort]")) {
|
||||
frontmatter.title = `[Lien mort] ${frontmatter.title}`;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
let statusEntries = [];
|
||||
if (Array.isArray(frontmatter.status)) {
|
||||
statusEntries = [...frontmatter.status];
|
||||
}
|
||||
|
||||
let statusEntry = statusEntries.find(
|
||||
(entry) => entry && typeof entry === "object" && entry.date === checkedDate
|
||||
);
|
||||
if (!statusEntry) {
|
||||
statusEntry = { date: checkedDate, http_code: httpCode };
|
||||
statusEntries.push(statusEntry);
|
||||
changed = true;
|
||||
} else if (statusEntry.http_code !== httpCode) {
|
||||
statusEntry.http_code = httpCode;
|
||||
changed = true;
|
||||
}
|
||||
frontmatter.status = statusEntries;
|
||||
|
||||
const noteLine = `> Lien inaccessible depuis le ${displayDate}`;
|
||||
const noteRegex = /(>\s*Lien inaccessible depuis le\s+)([^\n]+)/;
|
||||
const existing = body.match(noteRegex);
|
||||
if (existing) {
|
||||
const current = existing[2].trim();
|
||||
if (current !== displayDate) {
|
||||
body = body.replace(noteRegex, `> Lien inaccessible depuis le ${displayDate}`);
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
body = ensureBlankLineBeforeAppend(body);
|
||||
body += `${noteLine}\n`;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
const newFrontmatter = yaml.dump(frontmatter);
|
||||
const updatedContent = `---\n${newFrontmatter}---\n${body}`;
|
||||
if (updatedContent === original) {
|
||||
return { changed: false };
|
||||
}
|
||||
fs.writeFileSync(filePath, updatedContent, "utf8");
|
||||
return { changed: true };
|
||||
}
|
||||
|
||||
function collectDeadlinkMaxId(body) {
|
||||
let maxId = 0;
|
||||
const regex = /\[\^deadlink-(\d+)\]/g;
|
||||
let match;
|
||||
while ((match = regex.exec(body)) !== null) {
|
||||
const value = parseInt(match[1], 10);
|
||||
if (Number.isInteger(value) && value > maxId) {
|
||||
maxId = value;
|
||||
}
|
||||
}
|
||||
return maxId;
|
||||
}
|
||||
|
||||
function findExistingDeadlinkReference(line, url) {
|
||||
if (!line.includes(url)) return null;
|
||||
const escapedUrl = escapeRegExp(url);
|
||||
const markdownRegex = new RegExp(`\\[[^\\]]*\\]\\(${escapedUrl}\\)`);
|
||||
const angleRegex = new RegExp(`<${escapedUrl}>`);
|
||||
|
||||
let referenceId = null;
|
||||
|
||||
const searchers = [
|
||||
{ regex: markdownRegex },
|
||||
{ regex: angleRegex },
|
||||
];
|
||||
|
||||
for (const { regex } of searchers) {
|
||||
const match = regex.exec(line);
|
||||
if (!match) continue;
|
||||
const start = match.index;
|
||||
const end = start + match[0].length;
|
||||
const tail = line.slice(end);
|
||||
const footnoteMatch = tail.match(/^([\s)*_~`]*?)\[\^deadlink-(\d+)\]/);
|
||||
if (footnoteMatch) {
|
||||
referenceId = `deadlink-${footnoteMatch[2]}`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return referenceId;
|
||||
}
|
||||
|
||||
function insertDeadlinkReference(line, url, nextId) {
|
||||
const escapedUrl = escapeRegExp(url);
|
||||
const markdownRegex = new RegExp(`\\[[^\\]]*\\]\\(${escapedUrl}\\)`);
|
||||
const angleRegex = new RegExp(`<${escapedUrl}>`);
|
||||
|
||||
const footnoteRef = `[^deadlink-${nextId}]`;
|
||||
|
||||
const markdownMatch = markdownRegex.exec(line);
|
||||
if (markdownMatch) {
|
||||
const end = markdownMatch.index + markdownMatch[0].length;
|
||||
let insertPos = end;
|
||||
while (insertPos < line.length && /[*_]/.test(line[insertPos])) {
|
||||
insertPos += 1;
|
||||
}
|
||||
return line.slice(0, insertPos) + ' ' + footnoteRef + line.slice(insertPos);
|
||||
}
|
||||
|
||||
const angleMatch = angleRegex.exec(line);
|
||||
if (angleMatch) {
|
||||
const end = angleMatch.index + angleMatch[0].length;
|
||||
return line.slice(0, end) + footnoteRef + line.slice(end);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function upsertFootnoteDefinition(body, footnoteId, isoDate) {
|
||||
const displayDate = formatDisplayDate(isoDate);
|
||||
const desired = `Lien inaccessible depuis le ${displayDate}`;
|
||||
const definitionRegex = new RegExp(`^\\[\\^${footnoteId}\\]:\\s*(.+)$`, "m");
|
||||
const match = definitionRegex.exec(body);
|
||||
if (match) {
|
||||
if (match[1].trim() !== desired) {
|
||||
return {
|
||||
body: body.replace(definitionRegex, `[^${footnoteId}]: ${desired}`),
|
||||
changed: true,
|
||||
};
|
||||
}
|
||||
return { body, changed: false };
|
||||
}
|
||||
let updated = ensureTrailingNewline(body);
|
||||
updated = ensureBlankLineBeforeAppend(updated);
|
||||
updated += `[^${footnoteId}]: ${desired}\n`;
|
||||
return { body: updated, changed: true };
|
||||
}
|
||||
|
||||
function markMarkdownLink(filePath, url, info) {
|
||||
const original = fs.readFileSync(filePath, "utf8");
|
||||
const parsed = splitFrontmatter(original);
|
||||
const hasFrontmatter = Boolean(parsed);
|
||||
const block = parsed?.block ?? "";
|
||||
const bodyOriginal = parsed ? parsed.body : original;
|
||||
|
||||
const lines = bodyOriginal.split("\n");
|
||||
let inFence = false;
|
||||
let fenceChar = null;
|
||||
let referenceId = null;
|
||||
let changed = false;
|
||||
let maxId = collectDeadlinkMaxId(bodyOriginal);
|
||||
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
const line = lines[i];
|
||||
|
||||
const trimmed = line.trimStart();
|
||||
const fenceMatch = trimmed.match(/^([`~]{3,})/);
|
||||
if (fenceMatch) {
|
||||
const currentFenceChar = fenceMatch[1][0];
|
||||
if (!inFence) {
|
||||
inFence = true;
|
||||
fenceChar = currentFenceChar;
|
||||
continue;
|
||||
}
|
||||
if (fenceChar === currentFenceChar) {
|
||||
inFence = false;
|
||||
fenceChar = null;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (inFence) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!line.includes(url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const existingRef = findExistingDeadlinkReference(line, url);
|
||||
if (existingRef) {
|
||||
referenceId = existingRef;
|
||||
break;
|
||||
}
|
||||
|
||||
const nextId = maxId + 1;
|
||||
const updatedLine = insertDeadlinkReference(line, url, nextId);
|
||||
if (updatedLine) {
|
||||
lines[i] = updatedLine;
|
||||
referenceId = `deadlink-${nextId}`;
|
||||
maxId = nextId;
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!referenceId) {
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
let body = lines.join("\n");
|
||||
const { body: updatedBody, changed: definitionChanged } = upsertFootnoteDefinition(
|
||||
body,
|
||||
referenceId,
|
||||
getCheckedDate(info)
|
||||
);
|
||||
|
||||
body = updatedBody;
|
||||
if (definitionChanged) {
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
const updatedContent = hasFrontmatter ? `${block}${body}` : body;
|
||||
if (updatedContent === original) {
|
||||
return { changed: false };
|
||||
}
|
||||
fs.writeFileSync(filePath, updatedContent, "utf8");
|
||||
return { changed: true };
|
||||
}
|
||||
|
||||
function processFile(absolutePath, url, info) {
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
console.warn(`Fichier introuvable: ${absolutePath}`);
|
||||
return { changed: false };
|
||||
}
|
||||
const relative = path.relative(SITE_ROOT, absolutePath);
|
||||
if (relative.startsWith("content/interets/liens-interessants/")) {
|
||||
return markInterestingLink(absolutePath, url, info);
|
||||
}
|
||||
if (path.extname(relative).toLowerCase() === ".md") {
|
||||
return markMarkdownLink(absolutePath, url, info);
|
||||
}
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
function main() {
|
||||
if (!fs.existsSync(CACHE_PATH)) {
|
||||
console.error("Cache introuvable. Exécutez d'abord tools/check_external_links.js.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cache = loadCache(CACHE_PATH);
|
||||
const entries = Object.entries(cache).filter(
|
||||
([, info]) => info && info.manually_killed === true
|
||||
);
|
||||
|
||||
if (entries.length === 0) {
|
||||
console.log("Aucun lien marqué comme mort manuellement dans le cache.");
|
||||
return;
|
||||
}
|
||||
|
||||
let updates = 0;
|
||||
let warnings = 0;
|
||||
|
||||
for (const [url, info] of entries) {
|
||||
const files = getFilesForUrl(info);
|
||||
if (files.length === 0) {
|
||||
console.warn(`Aucun fichier associé à ${url}.`);
|
||||
warnings += 1;
|
||||
continue;
|
||||
}
|
||||
for (const relativePath of files) {
|
||||
const absolutePath = path.isAbsolute(relativePath)
|
||||
? relativePath
|
||||
: path.resolve(SITE_ROOT, relativePath);
|
||||
try {
|
||||
const { changed } = processFile(absolutePath, url, info);
|
||||
if (changed) {
|
||||
updates += 1;
|
||||
console.log(
|
||||
`✅ ${path.relative(SITE_ROOT, absolutePath)} mis à jour pour ${url}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
warnings += 1;
|
||||
console.error(
|
||||
`Erreur lors du traitement de ${path.relative(SITE_ROOT, absolutePath)} (${error.message}).`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (updates === 0) {
|
||||
console.log("Aucune modification nécessaire.");
|
||||
} else {
|
||||
console.log(`${updates} fichier(s) mis à jour.`);
|
||||
}
|
||||
|
||||
if (warnings > 0) {
|
||||
console.warn(`${warnings} fichier(s) n'ont pas pu être traités complètement.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const path = require("path");
|
||||
const { spawn } = require("child_process");
|
||||
|
||||
const SITE_ROOT = path.resolve(__dirname, "..");
|
||||
|
||||
const steps = [
|
||||
{ label: "check_internal_links", script: path.join(__dirname, "check_internal_links.js") },
|
||||
{ label: "check_external_links", script: path.join(__dirname, "check_external_links.js") },
|
||||
{ label: "update_external_links", script: path.join(__dirname, "update_external_links.js") },
|
||||
{ label: "mark_dead_links", script: path.join(__dirname, "mark_dead_links.js") },
|
||||
];
|
||||
|
||||
function runStep({ label, script }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn("node", [script], {
|
||||
cwd: SITE_ROOT,
|
||||
stdio: "inherit",
|
||||
});
|
||||
|
||||
child.on("exit", (code, signal) => {
|
||||
if (typeof code === "number" && code === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const reason =
|
||||
typeof code === "number"
|
||||
? `code ${code}`
|
||||
: signal
|
||||
? `signal ${signal}`
|
||||
: "unknown reason";
|
||||
reject(new Error(`Étape "${label}" terminée avec ${reason}`));
|
||||
});
|
||||
|
||||
child.on("error", (error) => {
|
||||
reject(new Error(`Impossible d'exécuter "${label}": ${error.message}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
for (const step of steps) {
|
||||
const relative = path.relative(SITE_ROOT, step.script);
|
||||
console.log(`\n➡️ Exécution de ${relative}...`);
|
||||
await runStep(step);
|
||||
}
|
||||
console.log("\n✅ Workflow des liens terminé.");
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(`\n❌ Échec du workflow: ${error.message}`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -1,254 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const util = require("util");
|
||||
const yaml = require("js-yaml");
|
||||
const readline = require("readline");
|
||||
const { execFile } = require("child_process");
|
||||
|
||||
const execFileAsync = util.promisify(execFile);
|
||||
|
||||
const SITE_ROOT = path.resolve(__dirname, "..");
|
||||
const CONFIG_PATH = path.join(__dirname, "config.json");
|
||||
|
||||
let config = {};
|
||||
if (fs.existsSync(CONFIG_PATH)) {
|
||||
try {
|
||||
config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Impossible de parser ${path.relative(
|
||||
SITE_ROOT,
|
||||
CONFIG_PATH
|
||||
)}. Valeurs par défaut utilisées. (${error.message})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const externalConfig = {
|
||||
cacheDir: path.join(__dirname, "cache"),
|
||||
cacheFile: "external_links.yaml",
|
||||
...(config.externalLinks || {}),
|
||||
};
|
||||
|
||||
const CACHE_DIR = path.isAbsolute(externalConfig.cacheDir)
|
||||
? externalConfig.cacheDir
|
||||
: path.resolve(SITE_ROOT, externalConfig.cacheDir);
|
||||
const CACHE_PATH = path.isAbsolute(externalConfig.cacheFile)
|
||||
? externalConfig.cacheFile
|
||||
: path.join(CACHE_DIR, externalConfig.cacheFile);
|
||||
|
||||
function ensureDirectoryExists(targetFile) {
|
||||
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
|
||||
}
|
||||
|
||||
function loadCache() {
|
||||
if (!fs.existsSync(CACHE_PATH)) return {};
|
||||
try {
|
||||
return yaml.load(fs.readFileSync(CACHE_PATH, "utf8")) || {};
|
||||
} catch (e) {
|
||||
console.error("Erreur de lecture du cache YAML:", e.message);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function saveCache(cache) {
|
||||
ensureDirectoryExists(CACHE_PATH);
|
||||
fs.writeFileSync(CACHE_PATH, yaml.dump(cache), "utf8");
|
||||
}
|
||||
|
||||
function promptFactory() {
|
||||
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
||||
const question = (q) =>
|
||||
new Promise((resolve) => rl.question(q, (ans) => resolve(ans.trim())));
|
||||
return {
|
||||
async ask(q) {
|
||||
return await question(q);
|
||||
},
|
||||
close() {
|
||||
rl.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function ensureCheckRanIfNeeded() {
|
||||
if (fs.existsSync(CACHE_PATH)) return;
|
||||
console.log(
|
||||
"Cache introuvable. Exécution préalable de tools/check_external_links.js..."
|
||||
);
|
||||
await execFileAsync("node", [path.join(__dirname, "check_external_links.js")], {
|
||||
cwd: SITE_ROOT,
|
||||
env: process.env,
|
||||
});
|
||||
}
|
||||
|
||||
function listBrokenUrls(cache) {
|
||||
const result = [];
|
||||
for (const [url, info] of Object.entries(cache)) {
|
||||
const status = info && typeof info.status === "number" ? info.status : null;
|
||||
const killed = info && info.manually_killed === true;
|
||||
const validated = info && info.manually_validated === true;
|
||||
if (killed) continue; // on ne traite plus ces URL
|
||||
if (validated) continue; // déjà validé manuellement
|
||||
if (status !== null && (status >= 400 || status === 0)) {
|
||||
result.push({ url, info });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFilesForUrl(info) {
|
||||
let files = [];
|
||||
if (Array.isArray(info?.files) && info.files.length > 0) {
|
||||
files = info.files;
|
||||
} else if (Array.isArray(info?.locations) && info.locations.length > 0) {
|
||||
files = Array.from(new Set(info.locations.map((s) => String(s).split(":")[0])));
|
||||
}
|
||||
return files.map((p) => path.resolve(SITE_ROOT, p));
|
||||
}
|
||||
|
||||
function replaceInFile(filePath, from, to) {
|
||||
if (!fs.existsSync(filePath)) return { changed: false };
|
||||
const original = fs.readFileSync(filePath, "utf8");
|
||||
if (!original.includes(from)) return { changed: false };
|
||||
const updated = original.split(from).join(to);
|
||||
if (updated !== original) {
|
||||
fs.writeFileSync(filePath, updated, "utf8");
|
||||
return { changed: true };
|
||||
}
|
||||
return { changed: false };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await ensureCheckRanIfNeeded();
|
||||
let cache = loadCache();
|
||||
|
||||
const broken = listBrokenUrls(cache);
|
||||
if (broken.length === 0) {
|
||||
console.log("Aucun lien en erreur (>= 400) à traiter.");
|
||||
return;
|
||||
}
|
||||
|
||||
const p = promptFactory();
|
||||
try {
|
||||
for (const { url, info } of broken) {
|
||||
const statusLabel = typeof info.status === "number" ? String(info.status) : "inconnu";
|
||||
const locations = Array.isArray(info.locations) ? info.locations : [];
|
||||
const files = Array.isArray(info.files) ? info.files : Array.from(new Set(locations.map((s) => String(s).split(":")[0])));
|
||||
console.log("\nURL: ", url);
|
||||
console.log("Statut: ", statusLabel);
|
||||
if (locations.length > 0) {
|
||||
console.log("Emplacements:");
|
||||
for (const loc of locations) console.log(" - ", loc);
|
||||
} else if (files.length > 0) {
|
||||
console.log("Emplacements:");
|
||||
for (const f of files) console.log(" - ", `${f}:?`);
|
||||
} else {
|
||||
console.log("Fichiers: (aucun chemin enregistré)");
|
||||
}
|
||||
|
||||
const choice = (
|
||||
await p.ask(
|
||||
"Action ? [i]gnorer, [c]onfirmer, [r]emplacer, [m]ort, [q]uitter (défaut: i) : "
|
||||
)
|
||||
).toLowerCase() || "i";
|
||||
|
||||
if (choice === "q") {
|
||||
console.log("Arrêt demandé.");
|
||||
break;
|
||||
}
|
||||
|
||||
if (choice === "i") {
|
||||
// Ignorer
|
||||
continue;
|
||||
}
|
||||
|
||||
if (choice === "c") {
|
||||
const nowIso = new Date().toISOString();
|
||||
cache[url] = {
|
||||
...(cache[url] || {}),
|
||||
manually_validated: true,
|
||||
manually_killed: cache[url]?.manually_killed === true,
|
||||
status: 200,
|
||||
errorType: null,
|
||||
method: "MANUAL",
|
||||
checked: nowIso,
|
||||
};
|
||||
saveCache(cache);
|
||||
console.log("Marqué comme validé manuellement.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (choice === "m") {
|
||||
cache[url] = {
|
||||
...(cache[url] || {}),
|
||||
manually_killed: true,
|
||||
manually_validated: cache[url]?.manually_validated === true,
|
||||
status: cache[url]?.status ?? null,
|
||||
errorType: cache[url]?.errorType ?? null,
|
||||
method: cache[url]?.method ?? null,
|
||||
};
|
||||
saveCache(cache);
|
||||
console.log("Marqué comme mort (plus jamais retesté).");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (choice === "r") {
|
||||
if (!(Array.isArray(files) && files.length > 0)) {
|
||||
console.log(
|
||||
"Impossible de remplacer: aucun fichier enregistré pour cet URL. Relancez d'abord tools/check_external_links.js."
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const newUrl = await p.ask("Nouvel URL: ");
|
||||
if (!newUrl || !newUrl.includes("://")) {
|
||||
console.log("URL invalide, opération annulée.");
|
||||
continue;
|
||||
}
|
||||
// Remplacements dans les fichiers listés
|
||||
let changedFiles = 0;
|
||||
for (const rel of files) {
|
||||
const abs = path.resolve(SITE_ROOT, rel);
|
||||
const { changed } = replaceInFile(abs, url, newUrl);
|
||||
if (changed) changedFiles++;
|
||||
}
|
||||
console.log(`Remplacements effectués dans ${changedFiles} fichier(s).`);
|
||||
|
||||
// Mettre à jour la base: déplacer l'entrée vers la nouvelle clé
|
||||
const oldEntry = cache[url] || {};
|
||||
const newEntryExisting = cache[newUrl] || {};
|
||||
cache[newUrl] = {
|
||||
...newEntryExisting,
|
||||
files: Array.isArray(oldEntry.files) ? [...oldEntry.files] : files,
|
||||
locations: Array.isArray(oldEntry.locations)
|
||||
? [...oldEntry.locations]
|
||||
: Array.isArray(oldEntry.files)
|
||||
? oldEntry.files.map((f) => `${f}:?`)
|
||||
: Array.isArray(locations)
|
||||
? [...locations]
|
||||
: [],
|
||||
manually_validated: false,
|
||||
manually_killed: false,
|
||||
status: null,
|
||||
errorType: null,
|
||||
method: newEntryExisting.method || null,
|
||||
checked: null,
|
||||
};
|
||||
delete cache[url];
|
||||
saveCache(cache);
|
||||
console.log("Base mise à jour pour le nouvel URL.");
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log("Choix non reconnu. Ignoré.");
|
||||
}
|
||||
} finally {
|
||||
p.close();
|
||||
}
|
||||
|
||||
console.log("\nTerminé. Vous pouvez relancer 'node tools/check_external_links.js' pour mettre à jour les statuts.");
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("Erreur:", err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user