hdoc-tools 0.8.34 → 0.8.36

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Hornbill Docs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/hdoc-build-pdf.js CHANGED
@@ -64,17 +64,21 @@
64
64
  }).get();
65
65
  imgs.push(...srcs);
66
66
  for (let i = 0; i < imgs.length; i++) {
67
- if (hdoc.valid_url(imgs[i]).valid) {
67
+ if (hdoc.valid_url(imgs[i])) {
68
68
  // External Link
69
- const file_response = request('GET', imgs[i]);
70
- if (file_response.statusCode === 200) {
71
- const image_buffer = file_response.getBody();
72
- const mime_type = mime.lookup(imgs[i]);
73
- let image_b64 = image_buffer.toString("base64");
74
- image_b64 = `data:${mime_type};base64,${image_b64}`;
75
- html_source = html_source.replace(imgs[i], image_b64);
76
- } else {
77
- throw `Unexpected Status ${file_response.statusCode}`;
69
+ try {
70
+ const file_response = request('GET', imgs[i]);
71
+ if (file_response.statusCode === 200) {
72
+ const image_buffer = file_response.getBody();
73
+ const mime_type = mime.lookup(imgs[i]);
74
+ let image_b64 = image_buffer.toString("base64");
75
+ image_b64 = `data:${mime_type};base64,${image_b64}`;
76
+ html_source = html_source.replace(imgs[i], image_b64);
77
+ } else {
78
+ throw `Unexpected Status ${file_response.statusCode}`;
79
+ }
80
+ } catch (err) {
81
+ console.log(`Error downloading external source [${imgs[i]}] - ${err}`);
78
82
  }
79
83
  } else {
80
84
  // Internal link
@@ -86,7 +90,7 @@
86
90
  image_b64 = `data:${mime_type};base64,${image_b64}`;
87
91
  html_source = html_source.replace(imgs[i], image_b64);
88
92
  } catch (err) {
89
- console.log('Error reading image from HTML source [', image_path, '] - ', err);
93
+ console.log('Error reading image from HTML source [', image_path, '] -', err);
90
94
  return null;
91
95
  }
92
96
  }
package/hdoc-build.js CHANGED
@@ -307,22 +307,24 @@
307
307
 
308
308
  if (fm_content.length >= 0) {
309
309
  fm_content.forEach(function (fm_prop) {
310
- const fm_property = fm_prop.split(':');
311
- if (fm_property[0] && fm_property[0].trim().length > 0 && fm_property[1] && fm_property[1].trim().length > 0) {
310
+ const fm_id = fm_prop.slice(0, fm_prop.indexOf(':'));
311
+ const fm_val = fm_prop.slice(fm_prop.indexOf(':') + 1);
312
+
313
+ if (fm_id && fm_id.trim().length > 0 && fm_val && fm_val.trim().length > 0) {
312
314
  fm_headers.push({
313
- id: fm_property[0].trim(),
314
- value: fm_property[1].trim()
315
+ id: fm_id.trim(),
316
+ value: fm_val.trim()
315
317
  });
316
318
 
317
- if (fm_property[0].trim() === 'title') {
319
+ if (fm_id.trim() === 'title') {
318
320
  fm_contains_title = true;
319
- doc_title = fm_property[1].trim();
321
+ doc_title = fm_val.trim();
320
322
  }
321
- if (fm_property[0].trim() === 'type') {
322
- doc_type = fm_property[1].trim();
323
+ if (fm_id.trim() === 'type') {
324
+ doc_type = fm_val.trim();
323
325
  }
324
- if (fm_property[0].trim() === 'reading-time') {
325
- book_read_time += parseInt(fm_property[1].trim(), 10);
326
+ if (fm_id.trim() === 'reading-time') {
327
+ book_read_time += parseInt(fm_val.trim(), 10);
326
328
  fm_contains_reading_time = true;
327
329
  }
328
330
  }
@@ -650,15 +652,12 @@
650
652
  });
651
653
  }
652
654
  }
653
-
654
655
  }
655
656
  }
656
657
  }
657
658
  }
658
659
  }
659
-
660
660
  }
661
- //console.log(JSON.stringify(bc, null, 2));
662
661
  };
663
662
 
664
663
  exports.run = async function (source_path, verbose_output, github_api_token) {
@@ -763,7 +762,7 @@
763
762
  console.log(` Static HTML Files Found: ${static_html_files.length}\n`);
764
763
 
765
764
  // Validate content
766
- const validation_success = validate.run(work_path, doc_id, verbose);
765
+ const validation_success = await validate.run(work_path, doc_id, verbose, hdocbook_config, hdocbook_project);
767
766
  if (!validation_success) {
768
767
  process.exit(1);
769
768
  }
package/hdoc-module.js CHANGED
@@ -2,8 +2,8 @@
2
2
  'use strict';
3
3
 
4
4
  const cheerio = require('cheerio'),
5
- request = require('sync-request'),
6
5
  html2text = require('html-to-text'),
6
+ request = require('sync-request'),
7
7
  wordsCount = require('words-count').default;
8
8
 
9
9
  let includesCache = {};
@@ -54,18 +54,17 @@
54
54
  }
55
55
  };
56
56
 
57
- exports.valid_url = function (s) {
58
- let response = {
59
- valid: false,
60
- urlProps: {}
61
- };
62
- try {
63
- response.urlProps = new URL(s);
64
- response.valid = true;
65
- } catch (err) {
66
- response.valid = false;
67
- }
68
- return response;
57
+ exports.valid_url = function (url) {
58
+
59
+ const stringIsAValidUrl = (s) => {
60
+ try {
61
+ new URL(s);
62
+ return true;
63
+ } catch (err) {
64
+ return false;
65
+ }
66
+ };
67
+ return stringIsAValidUrl(url);
69
68
  };
70
69
 
71
70
  exports.expand_variables = function (text, docId = '') {
package/hdoc-validate.js CHANGED
@@ -1,34 +1,79 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- const cheerio = require('cheerio'),
4
+ const axios = require('axios'),
5
+ cheerio = require('cheerio'),
5
6
  dree = require('dree'),
6
7
  fs = require('fs'),
8
+ https = require('https'),
7
9
  path = require('path'),
8
- hdoc = require(path.join(__dirname, 'hdoc-module.js')),
9
- URL = require("url").URL;
10
+ hdoc = require(path.join(__dirname, 'hdoc-module.js'));
11
+
12
+ const agent = new https.Agent({
13
+ rejectUnauthorized: false
14
+ });
10
15
 
11
16
  let errors = {},
12
17
  messages = {},
13
18
  warnings = {},
14
19
  errorcount = 0,
15
- filecount = 0,
16
- htmlFiles = [];
20
+ html_files = [],
21
+ exclude_links = {};
17
22
 
18
23
 
19
- const checkLinks = function (source_path, htmlFile, links) {
24
+ const checkLinks = async function (source_path, htmlFile, links, hdocbook_config) {
20
25
  for (let i = 0; i < links.length; i++) {
21
26
 
22
27
  // Validate that link is a valid URL first
23
- if (!hdoc.valid_url(links[i]).valid) {
24
-
28
+ if (!hdoc.valid_url(links[i])) {
25
29
  // Could be a relative path, check
26
- const fileExists = doesFileExist(source_path, htmlFile, links[i]);
27
- if (!fileExists) {
28
- errorcount++;
30
+ isRelativePath(source_path, htmlFile, links[i]);
31
+ } else {
32
+ messages[htmlFile.relativePath].push(`Link is a properly formatted external URL: ${links[i]}`);
33
+
34
+ // Skip if it's the auto-generated edit url, as these could be part of a private repo which would return a 404
35
+ if (links[i] === hdoc.get_github_api_path(hdocbook_config.publicSource, htmlFile.relativePath).edit_path.replace(path.extname(htmlFile.relativePath), '.md')) {
36
+ continue;
37
+ }
38
+
39
+ // Skip if the link is excluded in the project config
40
+ if (exclude_links[links[i]]) {
41
+ continue;
29
42
  }
43
+
44
+ // Skip mailto links
45
+ if (links[i].startsWith('mailto:')) {
46
+ continue;
47
+ }
48
+
49
+ try {
50
+ await axios.get(links[i], { httpsAgent: agent });
51
+ messages[htmlFile.relativePath].push(`Link is a valid external URL: ${links[i]}`);
52
+ } catch (e) {
53
+ // Handle errors
54
+ errors[htmlFile.relativePath].push(`Link is not responding: ${links[i]} - [${e.message}]`);
55
+ }
56
+ }
57
+ }
58
+ };
59
+
60
+ const checkImages = async function (source_path, htmlFile, links) {
61
+ for (let i = 0; i < links.length; i++) {
62
+
63
+ // Validate that image is a valid URL first
64
+ if (!hdoc.valid_url(links[i])) {
65
+ // Could be a relative path, check image exists
66
+ doesFileExist(source_path, htmlFile, links[i]);
30
67
  } else {
31
- messages[htmlFile.relativePath].push(`Link is valid External URL: ${links[i]}`);
68
+ messages[htmlFile.relativePath].push(`Image link is a properly formatted external URL: ${links[i]}`);
69
+ // Do a Get to the URL to see if it exists
70
+ try {
71
+ const res = await axios.get(links[i]);
72
+ messages[htmlFile.relativePath].push(`Image link is a valid external URL: ${links[i]}`);
73
+ } catch (e) {
74
+ // Handle errors
75
+ errors[htmlFile.relativePath].push(`Unexpected Error from external image link: ${links[i]} - ${e.message}`);
76
+ }
32
77
  }
33
78
  }
34
79
  };
@@ -47,16 +92,57 @@
47
92
 
48
93
  // File callbacks for html file scan
49
94
  const fileCallback = function (element) {
50
- filecount++;
51
- htmlFiles.push(element);
95
+ html_files.push(element);
52
96
  };
53
97
 
54
- const doesFileExist = function (source_path, html_path, relative_path) {
55
- // Remove explicit anchor links
56
- let file_path = path.join(source_path, relative_path.split('#')[0]);
98
+ const isRelativePath = function (source_path, html_path, relative_path) {
99
+ const rel_path_ext = path.extname(relative_path);
100
+ let response = {
101
+ is_rel_path: false,
102
+ has_md_extension: rel_path_ext === '.md'
103
+ };
104
+
105
+ const supported_relpaths = [
106
+ path.sep + 'index.htm',
107
+ path.sep + 'index.html',
108
+ '.htm',
109
+ '.html',
110
+ '.md'
111
+ ];
57
112
 
58
- // Replace /_books/ with /
59
- file_path = file_path.replace(path.sep + '_books' + path.sep, path.sep);
113
+ // Remove explicit anchor links and _books prefix
114
+ relative_path = relative_path.split('#')[0].replace('_books/', '');
115
+
116
+ // Make full file path
117
+ const file_path = path.join(source_path, relative_path);
118
+
119
+ // Does path exist?
120
+ if (fs.existsSync(file_path)) {
121
+ response.is_rel_path = true;
122
+ } else {
123
+ // Path
124
+ for (let i = 0; i < supported_relpaths.length; i++) {
125
+ if (fs.existsSync(`${file_path}${supported_relpaths[i]}`)) {
126
+ response.is_rel_path = true;
127
+ break;
128
+ }
129
+ }
130
+ }
131
+ if (response.has_md_extension) {
132
+ errors[html_path.relativePath].push(`Relative link contains MD extension, but should not: ${relative_path}`);
133
+ } else {
134
+ if (response.is_rel_path) {
135
+ messages[html_path.relativePath].push(`Relative path exists: ${relative_path}`);
136
+ } else {
137
+ errors[html_path.relativePath].push(`Link path does not exist: ${relative_path}`);
138
+ }
139
+ }
140
+ }
141
+
142
+ const doesFileExist = function (source_path, html_path, relative_path) {
143
+ // Remove explicit anchor links and _books prefix
144
+ relative_path = relative_path.split('#')[0].replace('_books/', '');
145
+ const file_path = path.join(source_path, relative_path);
60
146
  if (!fs.existsSync(file_path) && !fs.existsSync(file_path + path.sep + 'index.htm') && !fs.existsSync(file_path + 'index.html') && !fs.existsSync(file_path + '.htm') && !fs.existsSync(file_path + '.html')) {
61
147
  errors[html_path.relativePath].push(`Book resource does not exist: ${relative_path}`);
62
148
  return false;
@@ -70,7 +156,10 @@
70
156
  const getLinks = function (file) {
71
157
  messages[file.relativePath].push('Parsing HTML file');
72
158
  const htmlBody = fs.readFileSync(file.path, 'utf8');
73
- let links = [];
159
+ let links = {
160
+ href: [],
161
+ img: []
162
+ };
74
163
  const $ = cheerio.load(htmlBody);
75
164
  const hrefs = $('a').map(function (i) {
76
165
  return $(this).attr('href');
@@ -78,35 +167,46 @@
78
167
  const srcs = $('img').map(function (i) {
79
168
  return $(this).attr('src');
80
169
  }).get();
81
- links.push(...hrefs);
82
- links.push(...srcs);
170
+ links.href.push(...hrefs);
171
+ links.img.push(...srcs);
83
172
  return links;
84
173
  };
85
174
 
86
- exports.run = function (source_path, doc_id, verbose) {
175
+ exports.run = async function (source_path, doc_id, verbose, hdocbook_config, hdocbook_project) {
87
176
  // Get a list of HTML files in source_path
88
177
  dree.scan(source_path, dreeOptions, fileCallback);
89
178
 
179
+ if (hdocbook_project.linkValidation && hdocbook_project.linkValidation.exclude_links && hdocbook_project.linkValidation.exclude_links instanceof Array) {
180
+ hdocbook_project.linkValidation.exclude_links.forEach(function(excl_link) {
181
+ exclude_links[excl_link] = true;
182
+ });
183
+ }
184
+
90
185
  console.log(`Performing Validation and Building SEO Link List...`);
91
186
 
92
187
  let listContent = '';
93
- for (let i = 0; i < htmlFiles.length; i++) {
188
+ for (let i = 0; i < html_files.length; i++) {
94
189
 
95
190
  // Initiate maps for errors and verbose messages for HTML file
96
- errors[htmlFiles[i].relativePath] = [];
97
- messages[htmlFiles[i].relativePath] = [];
98
- warnings[htmlFiles[i].relativePath] = [];
191
+ errors[html_files[i].relativePath] = [];
192
+ messages[html_files[i].relativePath] = [];
193
+ warnings[html_files[i].relativePath] = [];
99
194
 
100
- const links = getLinks(htmlFiles[i]);
101
- if (links.length === 0) {
102
- messages[htmlFiles[i].relativePath].push('No links found in file');
195
+ const links = getLinks(html_files[i]);
196
+ if (links.href.length === 0) {
197
+ messages[html_files[i].relativePath].push('No links found in file');
198
+ } else {
199
+ await checkLinks(source_path, html_files[i], links.href, hdocbook_config);
200
+ }
201
+ if (links.img.length === 0) {
202
+ messages[html_files[i].relativePath].push('No images found in file');
103
203
  } else {
104
- checkLinks(source_path, htmlFiles[i], links);
204
+ await checkImages(source_path, html_files[i], links.img);
105
205
  }
106
206
 
107
207
  // Build list content for Google
108
- listContent += `/${htmlFiles[i].relativePath.replace(path.extname(htmlFiles[i].relativePath), '')}`;
109
- if (i < htmlFiles.length - 1) {
208
+ listContent += `/${html_files[i].relativePath.replace(path.extname(html_files[i].relativePath), '')}`;
209
+ if (i < html_files.length - 1) {
110
210
  listContent += '\r\n';
111
211
  }
112
212
  }
@@ -118,24 +218,6 @@
118
218
  } catch (err) {
119
219
  console.error(err);
120
220
  }
121
- if (errorcount === 0) {
122
- console.log('\r\n---------------');
123
- console.log(' No Errors ');
124
- console.log('---------------\r\n');
125
- } else {
126
- console.log('\r\n------------');
127
- console.log(' Errors ');
128
- console.log('------------');
129
-
130
- for (const key in errors) {
131
- if (errors.hasOwnProperty(key) && errors[key].length > 0) {
132
- console.log(`\r\n${errors[key].length} error(s) in ${key}`);
133
- for (let i = 0; i < errors[key].length; i++) {
134
- console.log(` - ${errors[key][i]}`);
135
- }
136
- }
137
- }
138
- }
139
221
 
140
222
  if (verbose) {
141
223
  console.log('\r\n-------------');
@@ -150,7 +232,26 @@
150
232
  }
151
233
  }
152
234
  }
153
- console.log(`\r\nValidation Errors Found: ${errorcount}\r\n`);
154
- return errorcount === 0 ? true : false;
235
+
236
+ console.log('\r\n-----------------------');
237
+ console.log(' Validation Output ');
238
+ console.log('-----------------------');
239
+
240
+ for (const key in errors) {
241
+ if (errors.hasOwnProperty(key) && errors[key].length > 0) {
242
+ console.log(`\r\n${errors[key].length} error(s) in ${key}`);
243
+ for (let i = 0; i < errors[key].length; i++) {
244
+ console.log(` - ${errors[key][i]}`);
245
+ errorcount++;
246
+ }
247
+ }
248
+ }
249
+
250
+ if (errorcount > 0) {
251
+ console.log(`\r\n${errorcount} Validation Errors Found\r\n`);
252
+ return false;
253
+ }
254
+ console.log(`\r\nNo Validation Errors Found!\r\n`);
255
+ return true;
155
256
  };
156
257
  })();
package/hdoc.js CHANGED
@@ -1,5 +1,4 @@
1
- #!/usr/bin/env node
2
-
1
+ #!/usr/bin/env node
3
2
  (async function () {
4
3
  'use strict';
5
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdoc-tools",
3
- "version": "0.8.34",
3
+ "version": "0.8.36",
4
4
  "description": "Hornbill HDocBook Development Support Tool",
5
5
  "main": "hdoc.js",
6
6
  "bin": {
@@ -30,6 +30,7 @@
30
30
  "author": "Hornbill Technologies Ltd",
31
31
  "license": "ISC",
32
32
  "dependencies": {
33
+ "axios": "^1.3.2",
33
34
  "better-sqlite3": "^8.0.1",
34
35
  "body-parser": "^1.20.1",
35
36
  "cheerio": "^1.0.0-rc.12",
@@ -40,7 +41,6 @@
40
41
  "highlight.js": "^11.6.0",
41
42
  "html-to-text": "^8.2.1",
42
43
  "js-yaml": "^4.1.0",
43
- "jspdf": "^2.5.1",
44
44
  "markdown-it": "^13.0.1",
45
45
  "markdown-it-container": "^3.0.0",
46
46
  "markdown-it-front-matter": "^0.2.3",