hdoc-tools 0.39.0 → 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.
Files changed (3) hide show
  1. package/hdoc-validate.js +16 -16
  2. package/hdoc.js +40 -10
  3. 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
- // Use Puppeteer to validate link address works
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
@@ -508,9 +508,6 @@ const e = require("express");
508
508
  errors[htmlFile.relativePath].push(error_message);
509
509
  }
510
510
  } else {
511
- if (links[i] === 'http://www.idontexistasawebsite.co.uk') {
512
- console.log(`Checking for a dummy link: ${links[i]}`);
513
- }
514
511
  messages[htmlFile.relativePath].push(
515
512
  `Link is a properly formatted external URL: ${links[i]}`,
516
513
  );
@@ -588,6 +585,8 @@ const e = require("express");
588
585
  continue;
589
586
  }
590
587
 
588
+ // Use Puppeteer to validate link address works
589
+ const page = await browser.newPage();
591
590
 
592
591
  try {
593
592
  // Set a user-agent to mimic a real browser
@@ -613,12 +612,9 @@ const e = require("express");
613
612
  });
614
613
 
615
614
  // Try loading the URL
616
- response = await page.goto(links[i], { waitUntil: 'networkidle2' }).catch(() => {
617
- // Ignore rendering errors (likely binary files like PDFs)
618
- });
615
+ response = await page.goto(links[i], { waitUntil: 'networkidle2', timeout: 10000 });
619
616
 
620
617
  if (response) {
621
- console.log(response);
622
618
  let status = response.status();
623
619
  const contentType = response.headers()['content-type'];
624
620
 
@@ -630,7 +626,7 @@ const e = require("express");
630
626
  }, links[i]);
631
627
  }
632
628
  if ((status < 200 || status > 299) && status !== 304) {
633
- if (status === 403 && links[i].includes(".hornbill.com")) {
629
+ if (process.env.GITHUB_ACTIONS === 'true' && status === 403 && links[i].includes(".hornbill.com")) {
634
630
  // STEVEG - do nothing here, as it always returns a 403 for Hornbill sites when accessing through GitHub Actions
635
631
  // Works totally fine locally or in hdocpub, still trying to work out what's causing this in GitHub
636
632
  } else {
@@ -652,16 +648,16 @@ const e = require("express");
652
648
  } else {
653
649
  error_message = processErrorMessage(`Issue with external link [${links[i]}]: ${e}`, markdown_paths.relativePath, markdown_content, links[i]);
654
650
  }
655
- if (hdocbook_project.validation.external_link_warnings)
651
+ if (hdocbook_project.validation.external_link_warnings || process.env.GITHUB_ACTIONS === 'true')
656
652
  warnings[htmlFile.relativePath].push(error_message);
657
653
  else
658
654
  errors[htmlFile.relativePath].push(error_message);
659
655
 
660
656
  }
657
+ // Close the headless browser tab
658
+ page.close();
661
659
  }
662
660
  }
663
- // Close the headless browser tab
664
- page.close();
665
661
  };
666
662
 
667
663
  const checkHostExistsInDNS = async (hostname) => {
@@ -1064,7 +1060,8 @@ const e = require("express");
1064
1060
 
1065
1061
 
1066
1062
  const global_links_checked = [];
1067
-
1063
+ const validateBrowser = await puppeteer.launch({ args: ['--no-sandbox'] });
1064
+
1068
1065
  for (const key in html_to_validate) {
1069
1066
  const file = html_to_validate[key];
1070
1067
  // Check for British spellings in static HTML content
@@ -1089,7 +1086,7 @@ const e = require("express");
1089
1086
  messages[file.relativePath].push("No links found in file");
1090
1087
  } else {
1091
1088
  console.log(`\r\nChecking Links in ${file.relativePath}`);
1092
- await checkLinks(source_path, file, links.href, hdocbook_config, hdocbook_project, browser, global_links_checked);
1089
+ await checkLinks(source_path, file, links.href, hdocbook_config, hdocbook_project, validateBrowser, global_links_checked);
1093
1090
  }
1094
1091
  if (links.img.length === 0) {
1095
1092
  messages[file.relativePath].push("No images found in file");
@@ -1100,6 +1097,9 @@ const e = require("express");
1100
1097
  // Check for multiple H1 tags
1101
1098
  await checkTags(file);
1102
1099
  }
1100
+
1101
+ // Close the Chromium browser instance
1102
+ await validateBrowser.close();
1103
1103
 
1104
1104
  if (gen_exclude) console.log(JSON.stringify(excl_output, null, 2));
1105
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
- let console_color = true;
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
- console.error = (arg) => {
45
- if (console_color)
46
- error.call(console, `\x1b[31m${arg}\x1b[0m`);
47
- else error.call(console, arg);
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdoc-tools",
3
- "version": "0.39.0",
3
+ "version": "0.40.0",
4
4
  "description": "Hornbill HDocBook Development Support Tool",
5
5
  "main": "hdoc.js",
6
6
  "bin": {