hdoc-tools 0.62.4 → 0.62.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hdoc-validate-interbook.js +8 -0
- package/hdoc-validate.js +92 -35
- package/package.json +1 -1
|
@@ -290,6 +290,14 @@
|
|
|
290
290
|
const doc_id = segments.shift();
|
|
291
291
|
const article_path = segments.length > 0 ? segments.join("/") : "index";
|
|
292
292
|
|
|
293
|
+
// No book in the path (e.g. a bare "/") - nothing to resolve
|
|
294
|
+
if (!doc_id) {
|
|
295
|
+
return {
|
|
296
|
+
level: "skip",
|
|
297
|
+
message: `Inter-book link has no target book - link not verified: ${link}`,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
293
301
|
// Books not sourced from GitHub — resolved by docId suffix, no repo
|
|
294
302
|
if (has_suffix(doc_id, UNVERIFIABLE_SUFFIXES)) {
|
|
295
303
|
return {
|
package/hdoc-validate.js
CHANGED
|
@@ -498,10 +498,22 @@
|
|
|
498
498
|
return resp.status;
|
|
499
499
|
};
|
|
500
500
|
|
|
501
|
-
//
|
|
502
|
-
//
|
|
503
|
-
|
|
504
|
-
const
|
|
501
|
+
// Links already written to validated-links.txt this run. A link can appear
|
|
502
|
+
// on many pages, but only needs writing to the cache file once.
|
|
503
|
+
const skip_links_written = new Set();
|
|
504
|
+
const appendSkipLink = (link) => {
|
|
505
|
+
if (skip_links_written.has(link)) return;
|
|
506
|
+
skip_links_written.add(link);
|
|
507
|
+
fs.appendFileSync(skip_link_file, `${link}\n`);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// Map a network check result (inter-book or external URL) onto
|
|
511
|
+
// errors/warnings/messages and the validated-links cache. 'ok' and 'skip'
|
|
512
|
+
// outcomes are stable, so they are appended to validated-links.txt like any
|
|
513
|
+
// other passing link. Called once per PAGE carrying the link, so the
|
|
514
|
+
// message is positioned against the page being reported on.
|
|
515
|
+
const emitLinkResult = (result, link, htmlFile, markdown_paths, markdown_content) => {
|
|
516
|
+
if (!result) return;
|
|
505
517
|
if (result.level === "error") {
|
|
506
518
|
errors[htmlFile.relativePath].push(
|
|
507
519
|
processErrorMessage(result.message, markdown_paths.relativePath, markdown_content, link),
|
|
@@ -512,10 +524,25 @@
|
|
|
512
524
|
);
|
|
513
525
|
} else {
|
|
514
526
|
messages[htmlFile.relativePath].push(result.message);
|
|
515
|
-
|
|
527
|
+
appendSkipLink(link);
|
|
516
528
|
}
|
|
517
529
|
};
|
|
518
530
|
|
|
531
|
+
// Run a network check for a link at most once per run, but hand the SAME
|
|
532
|
+
// result back to every page that carries the link. Deduplicating the fetch
|
|
533
|
+
// is a performance concern; deduplicating the reporting is not - a broken
|
|
534
|
+
// link on ten pages has to be flagged on all ten, or fixing the first page
|
|
535
|
+
// just surfaces the second on the next build.
|
|
536
|
+
// global_links_checked: Map link -> Promise<{ level, message } | null>
|
|
537
|
+
const checkLinkOnce = (global_links_checked, link, check) => {
|
|
538
|
+
let pending = global_links_checked.get(link);
|
|
539
|
+
if (!pending) {
|
|
540
|
+
pending = check();
|
|
541
|
+
global_links_checked.set(link, pending);
|
|
542
|
+
}
|
|
543
|
+
return pending;
|
|
544
|
+
};
|
|
545
|
+
|
|
519
546
|
const checkLinks = async (source_path, htmlFile, links, hdocbook_config, hdocbook_project, global_links_checked, output_links) => {
|
|
520
547
|
const markdown_paths = getMDPathFromHtmlPath(htmlFile);
|
|
521
548
|
const markdown_content = fs.readFileSync(markdown_paths.markdownPath, 'utf8');
|
|
@@ -537,11 +564,15 @@
|
|
|
537
564
|
// concurrently rather than one-at-a-time.
|
|
538
565
|
const externalChecks = [];
|
|
539
566
|
|
|
567
|
+
// Same link twice on the same page is reported once. Across pages it is
|
|
568
|
+
// reported every time - see checkLinkOnce.
|
|
569
|
+
const page_links_checked = new Set();
|
|
570
|
+
|
|
540
571
|
for (let i = 0; i < links.length; i++) {
|
|
541
572
|
if (output_links) console.log(` - ${links[i]}`);
|
|
542
573
|
if (exclude_links[links[i]]) continue;
|
|
543
|
-
if (
|
|
544
|
-
|
|
574
|
+
if (page_links_checked.has(links[i])) continue;
|
|
575
|
+
page_links_checked.add(links[i]);
|
|
545
576
|
|
|
546
577
|
const valid_url = hdoc.valid_url(links[i]);
|
|
547
578
|
if (!valid_url) {
|
|
@@ -554,6 +585,13 @@
|
|
|
554
585
|
if (link_segments[0] === "") link_segments.shift();
|
|
555
586
|
const link_root = link_segments[0] === "_books" ? link_segments[1] : link_segments[0];
|
|
556
587
|
|
|
588
|
+
// A bare "/" is the docs site home page - no book, nothing to
|
|
589
|
+
// resolve locally or against GitHub.
|
|
590
|
+
if (link_root === undefined || link_root === "") {
|
|
591
|
+
appendSkipLink(links[i]);
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
|
|
557
595
|
// Check for links with a _books path that have no specific file target
|
|
558
596
|
// We do need to exclude those with an extension though, for pages that link downloadable resources
|
|
559
597
|
if (link_segments[0] === "_books" && path.extname(links[i]) === '') {
|
|
@@ -567,8 +605,10 @@
|
|
|
567
605
|
if (interbook.enabled() && path.extname(links[i].split("#")[0]) === "") {
|
|
568
606
|
const link = links[i];
|
|
569
607
|
externalChecks.push(async () =>
|
|
570
|
-
|
|
571
|
-
await
|
|
608
|
+
emitLinkResult(
|
|
609
|
+
await checkLinkOnce(global_links_checked, link, () =>
|
|
610
|
+
interbook.check_link(link),
|
|
611
|
+
),
|
|
572
612
|
link,
|
|
573
613
|
htmlFile,
|
|
574
614
|
markdown_paths,
|
|
@@ -576,7 +616,7 @@
|
|
|
576
616
|
),
|
|
577
617
|
);
|
|
578
618
|
} else {
|
|
579
|
-
|
|
619
|
+
appendSkipLink(links[i]);
|
|
580
620
|
}
|
|
581
621
|
continue;
|
|
582
622
|
}
|
|
@@ -603,12 +643,12 @@
|
|
|
603
643
|
)
|
|
604
644
|
.edit_path.replace(path.extname(htmlFile.relativePath), ".md")
|
|
605
645
|
) {
|
|
606
|
-
|
|
646
|
+
appendSkipLink(links[i]);
|
|
607
647
|
continue;
|
|
608
648
|
}
|
|
609
649
|
|
|
610
650
|
if (valid_url.protocol === "mailto:") {
|
|
611
|
-
|
|
651
|
+
appendSkipLink(links[i]);
|
|
612
652
|
continue;
|
|
613
653
|
}
|
|
614
654
|
|
|
@@ -653,8 +693,10 @@
|
|
|
653
693
|
const link = links[i];
|
|
654
694
|
const book_link = valid_url.pathname + valid_url.hash;
|
|
655
695
|
externalChecks.push(async () =>
|
|
656
|
-
|
|
657
|
-
await
|
|
696
|
+
emitLinkResult(
|
|
697
|
+
await checkLinkOnce(global_links_checked, link, () =>
|
|
698
|
+
interbook.check_link(book_link),
|
|
699
|
+
),
|
|
658
700
|
link,
|
|
659
701
|
htmlFile,
|
|
660
702
|
markdown_paths,
|
|
@@ -668,20 +710,19 @@
|
|
|
668
710
|
const url = links[i];
|
|
669
711
|
const isInternal = url.toLowerCase().includes("internal.hornbill.com");
|
|
670
712
|
|
|
671
|
-
|
|
713
|
+
// Returns a page-independent { level, message } (or null for "say
|
|
714
|
+
// nothing") so the outcome can be cached per URL and replayed
|
|
715
|
+
// against every page that links to it.
|
|
716
|
+
const checkExternalUrl = async () => {
|
|
672
717
|
// For internal.hornbill.com links, check network reachability first (result cached)
|
|
673
718
|
if (isInternal) {
|
|
674
719
|
const on_int_net = await ensureIntNetCached();
|
|
675
720
|
if (!on_int_net) {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
return;
|
|
721
|
+
return {
|
|
722
|
+
level: "skip",
|
|
723
|
+
message: `Outside of Hornbill network - skipping internal link validation for: ${url}`,
|
|
724
|
+
};
|
|
681
725
|
}
|
|
682
|
-
messages[htmlFile.relativePath].push(
|
|
683
|
-
`Inside of Hornbill network - performing internal link validation for: ${url}`,
|
|
684
|
-
);
|
|
685
726
|
}
|
|
686
727
|
|
|
687
728
|
try {
|
|
@@ -689,25 +730,39 @@
|
|
|
689
730
|
if ((status < 200 || status > 299) && status !== 304) {
|
|
690
731
|
if (process.env.GITHUB_ACTIONS === 'true' && status === 403 && url.includes(".hornbill.com")) {
|
|
691
732
|
// Always returns 403 for Hornbill sites through GitHub Actions — not a real error
|
|
692
|
-
|
|
693
|
-
throw `Unexpected Status Returned: ${status}`;
|
|
733
|
+
return null;
|
|
694
734
|
}
|
|
695
|
-
|
|
696
|
-
fs.appendFileSync(skip_link_file, `${url}\n`);
|
|
735
|
+
throw `Unexpected Status Returned: ${status}`;
|
|
697
736
|
}
|
|
737
|
+
return {
|
|
738
|
+
level: "ok",
|
|
739
|
+
message: `External link is valid: ${url}`,
|
|
740
|
+
};
|
|
698
741
|
} catch (e) {
|
|
699
742
|
let error_message;
|
|
700
743
|
if (e instanceof AggregateError) {
|
|
701
|
-
error_message =
|
|
744
|
+
error_message = `Issue with external link [${url}]: ${e.message} - ${JSON.stringify(e.errors)}`;
|
|
702
745
|
} else {
|
|
703
|
-
error_message =
|
|
746
|
+
error_message = `Issue with external link [${url}]: ${e}`;
|
|
704
747
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
748
|
+
const level =
|
|
749
|
+
hdocbook_project.validation.external_link_warnings ||
|
|
750
|
+
process.env.GITHUB_ACTIONS === 'true'
|
|
751
|
+
? "warning"
|
|
752
|
+
: "error";
|
|
753
|
+
return { level, message: error_message };
|
|
709
754
|
}
|
|
710
|
-
}
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
externalChecks.push(async () =>
|
|
758
|
+
emitLinkResult(
|
|
759
|
+
await checkLinkOnce(global_links_checked, url, checkExternalUrl),
|
|
760
|
+
url,
|
|
761
|
+
htmlFile,
|
|
762
|
+
markdown_paths,
|
|
763
|
+
markdown_content,
|
|
764
|
+
),
|
|
765
|
+
);
|
|
711
766
|
}
|
|
712
767
|
}
|
|
713
768
|
|
|
@@ -1136,7 +1191,9 @@
|
|
|
1136
1191
|
}
|
|
1137
1192
|
|
|
1138
1193
|
|
|
1139
|
-
|
|
1194
|
+
// link -> Promise<{ level, message } | null> for network checks: fetched
|
|
1195
|
+
// once per run, but reported on every page that carries the link.
|
|
1196
|
+
const global_links_checked = new Map();
|
|
1140
1197
|
|
|
1141
1198
|
for (const key in html_to_validate) {
|
|
1142
1199
|
const file = html_to_validate[key];
|