hdoc-tools 0.39.1 → 0.40.0
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.js +16 -12
- package/hdoc.js +40 -10
- package/package.json +1 -1
package/hdoc-validate.js
CHANGED
@@ -10,6 +10,7 @@ const e = require("express");
|
|
10
10
|
const hdoc = require(path.join(__dirname, "hdoc-module.js"));
|
11
11
|
const translator = require("american-british-english-translator");
|
12
12
|
const { trueCasePathSync } = require("true-case-path");
|
13
|
+
const puppeteer = require("puppeteer");
|
13
14
|
|
14
15
|
const spellcheck_options = {
|
15
16
|
british: true,
|
@@ -470,15 +471,14 @@ const e = require("express");
|
|
470
471
|
const markdown_paths = getMDPathFromHtmlPath(htmlFile);
|
471
472
|
const markdown_content = fs.readFileSync(markdown_paths.markdownPath, 'utf8');
|
472
473
|
|
473
|
-
|
474
|
-
const page = await browser.newPage();
|
475
|
-
|
474
|
+
|
476
475
|
for (let i = 0; i < links.length; i++) {
|
477
476
|
// Validate that link is a valid URL first
|
478
477
|
if (exclude_links[links[i]]) continue;
|
479
478
|
console.log(` - ${links[i]}`);
|
480
479
|
if (global_links_checked.includes(links[i])) continue;
|
481
480
|
global_links_checked.push(links[i]);
|
481
|
+
|
482
482
|
const valid_url = hdoc.valid_url(links[i]);
|
483
483
|
if (!valid_url) {
|
484
484
|
// Could be a relative path, check
|
@@ -585,6 +585,8 @@ const e = require("express");
|
|
585
585
|
continue;
|
586
586
|
}
|
587
587
|
|
588
|
+
// Use Puppeteer to validate link address works
|
589
|
+
const page = await browser.newPage();
|
588
590
|
|
589
591
|
try {
|
590
592
|
// Set a user-agent to mimic a real browser
|
@@ -610,9 +612,7 @@ const e = require("express");
|
|
610
612
|
});
|
611
613
|
|
612
614
|
// Try loading the URL
|
613
|
-
response = await page.goto(links[i], { waitUntil: 'networkidle2' })
|
614
|
-
// Ignore rendering errors (likely binary files like PDFs)
|
615
|
-
});
|
615
|
+
response = await page.goto(links[i], { waitUntil: 'networkidle2', timeout: 10000 });
|
616
616
|
|
617
617
|
if (response) {
|
618
618
|
let status = response.status();
|
@@ -626,7 +626,7 @@ const e = require("express");
|
|
626
626
|
}, links[i]);
|
627
627
|
}
|
628
628
|
if ((status < 200 || status > 299) && status !== 304) {
|
629
|
-
if (status === 403 && links[i].includes(".hornbill.com")) {
|
629
|
+
if (process.env.GITHUB_ACTIONS === 'true' && status === 403 && links[i].includes(".hornbill.com")) {
|
630
630
|
// STEVEG - do nothing here, as it always returns a 403 for Hornbill sites when accessing through GitHub Actions
|
631
631
|
// Works totally fine locally or in hdocpub, still trying to work out what's causing this in GitHub
|
632
632
|
} else {
|
@@ -648,16 +648,16 @@ const e = require("express");
|
|
648
648
|
} else {
|
649
649
|
error_message = processErrorMessage(`Issue with external link [${links[i]}]: ${e}`, markdown_paths.relativePath, markdown_content, links[i]);
|
650
650
|
}
|
651
|
-
if (hdocbook_project.validation.external_link_warnings)
|
651
|
+
if (hdocbook_project.validation.external_link_warnings || process.env.GITHUB_ACTIONS === 'true')
|
652
652
|
warnings[htmlFile.relativePath].push(error_message);
|
653
653
|
else
|
654
654
|
errors[htmlFile.relativePath].push(error_message);
|
655
655
|
|
656
656
|
}
|
657
|
+
// Close the headless browser tab
|
658
|
+
page.close();
|
657
659
|
}
|
658
660
|
}
|
659
|
-
// Close the headless browser tab
|
660
|
-
page.close();
|
661
661
|
};
|
662
662
|
|
663
663
|
const checkHostExistsInDNS = async (hostname) => {
|
@@ -1060,7 +1060,8 @@ const e = require("express");
|
|
1060
1060
|
|
1061
1061
|
|
1062
1062
|
const global_links_checked = [];
|
1063
|
-
|
1063
|
+
const validateBrowser = await puppeteer.launch({ args: ['--no-sandbox'] });
|
1064
|
+
|
1064
1065
|
for (const key in html_to_validate) {
|
1065
1066
|
const file = html_to_validate[key];
|
1066
1067
|
// Check for British spellings in static HTML content
|
@@ -1085,7 +1086,7 @@ const e = require("express");
|
|
1085
1086
|
messages[file.relativePath].push("No links found in file");
|
1086
1087
|
} else {
|
1087
1088
|
console.log(`\r\nChecking Links in ${file.relativePath}`);
|
1088
|
-
await checkLinks(source_path, file, links.href, hdocbook_config, hdocbook_project,
|
1089
|
+
await checkLinks(source_path, file, links.href, hdocbook_config, hdocbook_project, validateBrowser, global_links_checked);
|
1089
1090
|
}
|
1090
1091
|
if (links.img.length === 0) {
|
1091
1092
|
messages[file.relativePath].push("No images found in file");
|
@@ -1096,6 +1097,9 @@ const e = require("express");
|
|
1096
1097
|
// Check for multiple H1 tags
|
1097
1098
|
await checkTags(file);
|
1098
1099
|
}
|
1100
|
+
|
1101
|
+
// Close the Chromium browser instance
|
1102
|
+
await validateBrowser.close();
|
1099
1103
|
|
1100
1104
|
if (gen_exclude) console.log(JSON.stringify(excl_output, null, 2));
|
1101
1105
|
|
package/hdoc.js
CHANGED
@@ -8,6 +8,10 @@
|
|
8
8
|
const packageFile = path.join(__dirname, "package.json");
|
9
9
|
|
10
10
|
const originalConsoleLog = console.log;
|
11
|
+
const originalConsoleError = console.error;
|
12
|
+
const originalConsoleInfo = console.info;
|
13
|
+
|
14
|
+
let console_color = true;
|
11
15
|
|
12
16
|
console.log = (...args) => {
|
13
17
|
if (process.env.GITHUB_ACTIONS !== 'true') {
|
@@ -33,18 +37,44 @@
|
|
33
37
|
console.log("\nRunning in non-GitHub Actions environment\n");
|
34
38
|
}
|
35
39
|
|
36
|
-
|
40
|
+
console.info = (...args) => {
|
41
|
+
|
42
|
+
if (process.env.GITHUB_ACTIONS !== 'true') {
|
43
|
+
// If not running in GitHub Actions, send args to the original console.log
|
44
|
+
if (console_color) originalConsoleInfo(`\x1b[33m${args}\x1b[0m`);
|
45
|
+
else originalConsoleInfo(...args);
|
46
|
+
} else {
|
47
|
+
// If running in GitHub Actions, escape % and \ characters so printf doesn't throw an error
|
48
|
+
const escapedArgs = args.map(arg => {
|
49
|
+
if (typeof arg === 'string') {
|
50
|
+
return arg.replace(/%/g, '%%').replace(/\\/g, '\\\\');
|
51
|
+
}
|
52
|
+
return arg;
|
53
|
+
});
|
54
|
+
// Use the original console.log with escaped arguments
|
55
|
+
originalConsoleInfo(...escapedArgs);
|
56
|
+
}
|
37
57
|
|
38
|
-
const { info, error } = console;
|
39
|
-
console.info = (arg) => {
|
40
|
-
if (console_color)
|
41
|
-
info.call(console, `\x1b[33m${arg}\x1b[0m`);
|
42
|
-
else info.call(console, arg);
|
43
58
|
};
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
59
|
+
|
60
|
+
console.error = (...args) => {
|
61
|
+
|
62
|
+
if (process.env.GITHUB_ACTIONS !== 'true') {
|
63
|
+
// If not running in GitHub Actions, send args to the original console.log
|
64
|
+
if (console_color) originalConsoleError(`\x1b[33m${args}\x1b[0m`);
|
65
|
+
else originalConsoleError(...args);
|
66
|
+
} else {
|
67
|
+
|
68
|
+
// If running in GitHub Actions, escape % and \ characters so printf doesn't throw an error
|
69
|
+
const escapedArgs = args.map(arg => {
|
70
|
+
if (typeof arg === 'string') {
|
71
|
+
return arg.replace(/%/g, '%%').replace(/\\/g, '\\\\');
|
72
|
+
}
|
73
|
+
return arg;
|
74
|
+
});
|
75
|
+
// Use the original console.log with escaped arguments
|
76
|
+
originalConsoleError(...escapedArgs);
|
77
|
+
}
|
48
78
|
};
|
49
79
|
|
50
80
|
const getHdocPackageVersion = (packagePath) => {
|