hdoc-tools 0.19.7 → 0.20.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.
@@ -1,86 +1,88 @@
1
- (function () {
2
- 'use strict';
3
-
4
- // GS: based on markdown-it-tips extension but customized for our specific needs
5
-
6
- var container = require('markdown-it-container');
7
-
8
- module.exports = function md_tip_plugin(md, options) {
9
- var containerOpenCount = 0;
10
- var links = options ? options.links : true;
11
- init();
12
- return;
13
-
14
- function icon_from_type(name) {
15
- switch (name) {
16
- case "note":
17
- return {
18
- class: "bi bi-exclamation-circle", title: "Note"
19
- };
20
- case "tip":
21
- return {
22
- class: "bi bi-lightbulb", title: "Tip"
23
- };
24
- case "important":
25
- return {
26
- class: "bi bi-megaphone", title: "Important"
27
- };
28
- case "caution":
29
- return {
30
- class: "bi bi-x-circle", title: "Caution"
31
- };
32
- case "warning":
33
- return {
34
- class: "bi bi-exclamation-triangle", title: "Warning"
35
- };
36
- }
37
- }
38
-
39
- function setupContainer(name) {
40
- md.use(container, name, {
41
- render: function (tokens, idx) {
42
- if (tokens[idx].nesting === 1) {
43
- containerOpenCount += 1;
44
- let inf = icon_from_type(name);
45
- return '<div class="hdoc-alert alert-icon-' + name + '">\n<p class="hdoc-alert-title"><span class="' + inf.class + '"></span>' + inf.title + '</p>\n';
46
- } else {
47
- containerOpenCount -= 1;
48
- return '</div>\n';
49
- }
50
- }
51
- });
52
- }
53
-
54
- function isContainerOpen() {
55
- return containerOpenCount > 0;
56
- }
57
-
58
- function selfRender(tokens, idx, options, env, self) {
59
- return self.renderToken(tokens, idx, options);
60
- }
61
-
62
- function setupLinks() {
63
- var defaultRender = md.renderer.rules.link_open || selfRender;
64
-
65
- md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
66
- if (isContainerOpen()) {
67
- tokens[idx].attrPush(['class', 'md-tip-link']);
68
- }
69
-
70
- return defaultRender(tokens, idx, options, env, self);
71
- };
72
- }
73
-
74
- function init() {
75
- setupContainer('note');
76
- setupContainer('tip');
77
- setupContainer('important');
78
- setupContainer('caution');
79
- setupContainer('warning');
80
-
81
- if (links) {
82
- setupLinks();
83
- }
84
- }
85
- };
86
- })();
1
+ (() => {
2
+ // GS: based on markdown-it-tips extension but customized for our specific needs
3
+
4
+ const container = require("markdown-it-container");
5
+
6
+ module.exports = function md_tip_plugin(md, options) {
7
+ let containerOpenCount = 0;
8
+ const links = options ? options.links : true;
9
+ init();
10
+ return;
11
+
12
+ function icon_from_type(name) {
13
+ switch (name) {
14
+ case "note":
15
+ return {
16
+ class: "bi bi-exclamation-circle",
17
+ title: "Note",
18
+ };
19
+ case "tip":
20
+ return {
21
+ class: "bi bi-lightbulb",
22
+ title: "Tip",
23
+ };
24
+ case "important":
25
+ return {
26
+ class: "bi bi-megaphone",
27
+ title: "Important",
28
+ };
29
+ case "caution":
30
+ return {
31
+ class: "bi bi-x-circle",
32
+ title: "Caution",
33
+ };
34
+ case "warning":
35
+ return {
36
+ class: "bi bi-exclamation-triangle",
37
+ title: "Warning",
38
+ };
39
+ }
40
+ }
41
+
42
+ function setupContainer(name) {
43
+ md.use(container, name, {
44
+ render: (tokens, idx) => {
45
+ if (tokens[idx].nesting === 1) {
46
+ containerOpenCount += 1;
47
+ const inf = icon_from_type(name);
48
+ return `<div class="hdoc-alert alert-icon-${name}">\n<p class="hdoc-alert-title"><span class="${inf.class}"></span>${inf.title}</p>\n`;
49
+ }
50
+ containerOpenCount -= 1;
51
+ return "</div>\n";
52
+ },
53
+ });
54
+ }
55
+
56
+ function isContainerOpen() {
57
+ return containerOpenCount > 0;
58
+ }
59
+
60
+ function selfRender(tokens, idx, options, env, self) {
61
+ return self.renderToken(tokens, idx, options);
62
+ }
63
+
64
+ function setupLinks() {
65
+ const defaultRender = md.renderer.rules.link_open || selfRender;
66
+
67
+ md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
68
+ if (isContainerOpen()) {
69
+ tokens[idx].attrPush(["class", "md-tip-link"]);
70
+ }
71
+
72
+ return defaultRender(tokens, idx, options, env, self);
73
+ };
74
+ }
75
+
76
+ function init() {
77
+ setupContainer("note");
78
+ setupContainer("tip");
79
+ setupContainer("important");
80
+ setupContainer("caution");
81
+ setupContainer("warning");
82
+
83
+ if (links) {
84
+ setupLinks();
85
+ }
86
+ }
87
+ };
88
+ })();
package/hdoc-build-db.js CHANGED
@@ -1,198 +1,260 @@
1
- (function () {
2
- 'use strict';
3
-
4
- const path = require('path'),
5
- hdoc_index = require(path.join(__dirname, 'hdoc-db.js')),
6
- database = require('better-sqlite3');
7
-
8
- const db_schema = {
9
- hdoc_index: [
10
- 'resource_url UNINDEXED',
11
- 'book_id',
12
- 'book_audience UNINDEXED',
13
- 'book_tags',
14
- 'doc_title',
15
- 'doc_content',
16
- 'doc_preview UNINDEXED',
17
- 'doc_family_id',
18
- 'doc_md5_hash UNINDEXED',
19
- 'doc_lastmod'
20
- ],
21
- hdoc_meta: [
22
- 'resource_url',
23
- 'book_id',
24
- 'contributor_count INTEGER UNINDEXED',
25
- 'edit_url UNINDEXED',
26
- 'last_commit UNINDEXED',
27
- 'pdf_size INTEGER UNINDEXED'
28
- ],
29
- hdoc_contributors: [
30
- 'resource_url',
31
- 'book_id',
32
- 'login',
33
- 'name',
34
- 'avatar UNINDEXED',
35
- 'url UNINDEXED'
36
- ],
37
- hdoc_redirects: [
38
- 'resource_url',
39
- 'location_url',
40
- 'http_code INTEGER UNINDEXED'
41
- ]
42
- };
43
-
44
- exports.create_table = function (db, table, cols, virtual, fts5) {
45
- const table_created = hdoc_index.create_table(db, table, cols, virtual, fts5);
46
- if (table_created !== null) {
47
- return `\nError creating table: ${table_created}`;
48
- } else {
49
- console.log(`\nTable created: ${table}`);
50
- }
51
- return null;
52
- };
53
-
54
- exports.create_db = function (db_path, doc_id) {
55
- let response = {
56
- error: null,
57
- db: null
58
- };
59
- console.log('\nPerforming SQlite index creation...');
60
- let db_name = path.join(db_path, doc_id, 'index.db');
61
- response.db = new database(db_name);
62
- response.db.pragma('encoding="UTF-8"');
63
- console.log(`\nDatabase created: ${db_name}`);
64
-
65
- // Create tables
66
- for (const key in db_schema) {
67
- if (db_schema.hasOwnProperty(key)) {
68
- const virtual = key === 'hdoc_index' ? true : false;
69
- const fts5 = key === 'hdoc_index' ? true : false;
70
- const table_created_error = this.create_table(response.db, key, db_schema[key], virtual, fts5);
71
- if (table_created_error !== null) {
72
- console.error(table_created_error);
73
- }
74
- }
75
- }
76
- return response;
77
- };
78
-
79
- exports.populate_redirects = function (db, redirect_records, verbose = false) {
80
- let response = {
81
- success: true,
82
- errors: [],
83
- index_success_count: 0
84
- };
85
-
86
- for (let i = 0; i < redirect_records.length; i++) {
87
- const index_vals = [
88
- redirect_records[i].url,
89
- redirect_records[i].location ? redirect_records[i].location : '',
90
- redirect_records[i].code
91
- ];
92
- const index_response = hdoc_index.insert_record(db, 'hdoc_redirects', db_schema.hdoc_redirects, index_vals);
93
- if (!index_response.success) {
94
- response.success = false;
95
- response.errors.push(`Redirect record creation failed - ${redirect_records[i].url}: ${index_response.error}`);
96
- } else {
97
- response.index_success_count++
98
- }
99
- }
100
- console.log(`\nRedirect Index Build Complete: ${response.index_success_count} document records created.`);
101
- return response;
102
- };
103
-
104
- exports.populate_index = async function (db, doc_id, book_config, index_records, verbose = false) {
105
- let response = {
106
- success: false,
107
- index_success_count: 0,
108
- error: ''
109
- };
110
-
111
- if (!book_config.tags) book_config.tags = [];
112
-
113
- let indexPromises = [];
114
- for (let i = 0; i < index_records.length; i++) {
115
- indexPromises.push(index_records[i]);
116
- }
117
- let curr_file = '';
118
- await Promise.all(indexPromises.map(async (file) => {
119
- let index_path_name = file.relative_path.replaceAll('\\', '/');
120
- if (index_path_name.endsWith('/index.md') || index_path_name.endsWith('/index.html') || index_path_name.endsWith('/index.htm')) {
121
- index_path_name = index_path_name.substring(0, index_path_name.lastIndexOf('/'));
122
- }
123
- index_path_name = '/' + index_path_name.replace(path.extname(file.relative_path), '');
124
-
125
- let index_response = {
126
- success: true,
127
- row_id: 0
128
- };
129
- let index_content_path = index_path_name;
130
- if (file.index_html.id !== null) index_content_path += `#${file.index_html.id}`;
131
- if (!file.inline) {
132
- const index_vals = [
133
- index_content_path,
134
- doc_id,
135
- book_config.audience.join(','),
136
- book_config.tags.join(','),
137
- file.index_html.fm_props.title,
138
- file.index_html.text,
139
- file.index_html.preview,
140
- book_config.productFamily,
141
- file.md5,
142
- file.lastmod
143
- ];
144
- index_response = hdoc_index.insert_record(db, 'hdoc_index', db_schema.hdoc_index, index_vals);
145
- }
146
- if (!index_response.success) {
147
- console.error(`Index record creation failed - ${doc_id}/${file.index_html.fm_props.title}: ${index_response.error}`);
148
- } else {
149
- if (curr_file === index_path_name) return;
150
- curr_file = index_path_name;
151
- // Now add metadata
152
- const meta_vals = [
153
- index_path_name,
154
- doc_id,
155
- file.metadata.contributor_count,
156
- file.metadata.edit_url,
157
- file.metadata.last_commit,
158
- file.pdf_size
159
- ];
160
- const meta_response = await hdoc_index.insert_record(db, 'hdoc_meta', db_schema.hdoc_meta, meta_vals);
161
- if (!meta_response.success) {
162
- console.error(`Index metadata record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${meta_response.error}`);
163
- } else {
164
- if (verbose) {
165
- console.log(`Inserted index record ${index_response.row_id}: ${doc_id} - ${file.index_html.fm_props.title}`);
166
- console.log(`Inserted index metadata record for index ID: ${meta_response.row_id}`);
167
- }
168
-
169
- // Now add contributor records
170
- for (let j = 0; j < file.contributors.length; j++) {
171
- const contrib_vals = [
172
- index_path_name,
173
- doc_id,
174
- file.contributors[j].login,
175
- file.contributors[j].name,
176
- file.contributors[j].avatar_url,
177
- file.contributors[j].html_url
178
- ];
179
- const cont_response = await hdoc_index.insert_record(db, 'hdoc_contributors', db_schema.hdoc_contributors, contrib_vals);
180
- if (!cont_response.success) {
181
- console.error(`Index document contributor record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${cont_response.error}`);
182
- continue;
183
- }
184
- if (verbose) {
185
- console.log(`Inserted document contributor record ${cont_response.row_id}`);
186
- }
187
- }
188
- response.index_success_count++;
189
- }
190
- }
191
-
192
- }));
193
-
194
- response.success = true;
195
- console.log(`\nIndex Build Complete: ${response.index_success_count} document index records created.`);
196
- return response;
197
- };
198
- })();
1
+ (() => {
2
+ const path = require("node:path");
3
+ const hdoc_index = require(path.join(__dirname, "hdoc-db.js"));
4
+ const database = require("better-sqlite3");
5
+
6
+ const db_schema = {
7
+ hdoc_index: [
8
+ "resource_url UNINDEXED",
9
+ "book_id",
10
+ "book_audience UNINDEXED",
11
+ "book_tags",
12
+ "doc_title",
13
+ "doc_content",
14
+ "doc_preview UNINDEXED",
15
+ "doc_family_id",
16
+ "doc_md5_hash UNINDEXED",
17
+ "doc_lastmod",
18
+ ],
19
+ hdoc_meta: [
20
+ "resource_url",
21
+ "book_id",
22
+ "contributor_count INTEGER UNINDEXED",
23
+ "edit_url UNINDEXED",
24
+ "last_commit UNINDEXED",
25
+ "pdf_size INTEGER UNINDEXED",
26
+ ],
27
+ hdoc_contributors: [
28
+ "resource_url",
29
+ "book_id",
30
+ "login",
31
+ "name",
32
+ "avatar UNINDEXED",
33
+ "url UNINDEXED",
34
+ ],
35
+ hdoc_redirects: [
36
+ "resource_url",
37
+ "location_url",
38
+ "http_code INTEGER UNINDEXED",
39
+ ],
40
+ };
41
+
42
+ exports.create_table = (db, table, cols, virtual, fts5) => {
43
+ const table_created = hdoc_index.create_table(
44
+ db,
45
+ table,
46
+ cols,
47
+ virtual,
48
+ fts5,
49
+ );
50
+ if (table_created !== null) {
51
+ return `\nError creating table: ${table_created}`;
52
+ }
53
+ console.log(`\nTable created: ${table}`);
54
+ return null;
55
+ };
56
+
57
+ exports.create_db = function (db_path, doc_id) {
58
+ const response = {
59
+ error: null,
60
+ db: null,
61
+ };
62
+ console.log("\nPerforming SQlite index creation...");
63
+ const db_name = path.join(db_path, doc_id, "index.db");
64
+ response.db = new database(db_name);
65
+ response.db.pragma('encoding="UTF-8"');
66
+ console.log(`\nDatabase created: ${db_name}`);
67
+
68
+ // Create tables
69
+ for (const key in db_schema) {
70
+ if (Object.hasOwn(db_schema, key)) {
71
+ const virtual = key === "hdoc_index";
72
+ const fts5 = key === "hdoc_index";
73
+ const table_created_error = this.create_table(
74
+ response.db,
75
+ key,
76
+ db_schema[key],
77
+ virtual,
78
+ fts5,
79
+ );
80
+ if (table_created_error !== null) {
81
+ console.error(table_created_error);
82
+ }
83
+ }
84
+ }
85
+ return response;
86
+ };
87
+
88
+ exports.populate_redirects = (db, redirect_records, verbose = false) => {
89
+ const response = {
90
+ success: true,
91
+ errors: [],
92
+ index_success_count: 0,
93
+ };
94
+
95
+ for (let i = 0; i < redirect_records.length; i++) {
96
+ const index_vals = [
97
+ redirect_records[i].url,
98
+ redirect_records[i].location ? redirect_records[i].location : "",
99
+ redirect_records[i].code,
100
+ ];
101
+ const index_response = hdoc_index.insert_record(
102
+ db,
103
+ "hdoc_redirects",
104
+ db_schema.hdoc_redirects,
105
+ index_vals,
106
+ );
107
+ if (!index_response.success) {
108
+ response.success = false;
109
+ response.errors.push(
110
+ `Redirect record creation failed - ${redirect_records[i].url}: ${index_response.error}`,
111
+ );
112
+ } else {
113
+ response.index_success_count++;
114
+ }
115
+ }
116
+ console.log(
117
+ `\nRedirect Index Build Complete: ${response.index_success_count} document records created.`,
118
+ );
119
+ return response;
120
+ };
121
+
122
+ exports.populate_index = async (
123
+ db,
124
+ doc_id,
125
+ book_config,
126
+ index_records,
127
+ verbose = false,
128
+ ) => {
129
+ const response = {
130
+ success: false,
131
+ index_success_count: 0,
132
+ error: "",
133
+ };
134
+
135
+ if (!book_config.tags) book_config.tags = [];
136
+
137
+ const indexPromises = [];
138
+ for (let i = 0; i < index_records.length; i++) {
139
+ indexPromises.push(index_records[i]);
140
+ }
141
+ let curr_file = "";
142
+ await Promise.all(
143
+ indexPromises.map(async (file) => {
144
+ let index_path_name = file.relative_path.replaceAll("\\", "/");
145
+ if (
146
+ index_path_name.endsWith("/index.md") ||
147
+ index_path_name.endsWith("/index.html") ||
148
+ index_path_name.endsWith("/index.htm")
149
+ ) {
150
+ index_path_name = index_path_name.substring(
151
+ 0,
152
+ index_path_name.lastIndexOf("/"),
153
+ );
154
+ }
155
+ index_path_name = `/${index_path_name.replace(path.extname(file.relative_path), "")}`;
156
+
157
+ let index_response = {
158
+ success: true,
159
+ row_id: 0,
160
+ };
161
+ let index_content_path = index_path_name;
162
+ if (file.index_html.id !== null)
163
+ index_content_path += `#${file.index_html.id}`;
164
+ if (!file.inline) {
165
+ const index_vals = [
166
+ index_content_path,
167
+ doc_id,
168
+ book_config.audience.join(","),
169
+ book_config.tags.join(","),
170
+ file.index_html.fm_props.title,
171
+ file.index_html.text,
172
+ file.index_html.preview,
173
+ book_config.productFamily,
174
+ file.md5,
175
+ file.lastmod,
176
+ ];
177
+ index_response = hdoc_index.insert_record(
178
+ db,
179
+ "hdoc_index",
180
+ db_schema.hdoc_index,
181
+ index_vals,
182
+ );
183
+ }
184
+ if (!index_response.success) {
185
+ console.error(
186
+ `Index record creation failed - ${doc_id}/${file.index_html.fm_props.title}: ${index_response.error}`,
187
+ );
188
+ } else {
189
+ if (curr_file === index_path_name) return;
190
+ curr_file = index_path_name;
191
+ // Now add metadata
192
+ const meta_vals = [
193
+ index_path_name,
194
+ doc_id,
195
+ file.metadata.contributor_count,
196
+ file.metadata.edit_url,
197
+ file.metadata.last_commit,
198
+ file.pdf_size,
199
+ ];
200
+ const meta_response = await hdoc_index.insert_record(
201
+ db,
202
+ "hdoc_meta",
203
+ db_schema.hdoc_meta,
204
+ meta_vals,
205
+ );
206
+ if (!meta_response.success) {
207
+ console.error(
208
+ `Index metadata record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${meta_response.error}`,
209
+ );
210
+ } else {
211
+ if (verbose) {
212
+ console.log(
213
+ `Inserted index record ${index_response.row_id}: ${doc_id} - ${file.index_html.fm_props.title}`,
214
+ );
215
+ console.log(
216
+ `Inserted index metadata record for index ID: ${meta_response.row_id}`,
217
+ );
218
+ }
219
+
220
+ // Now add contributor records
221
+ for (let j = 0; j < file.contributors.length; j++) {
222
+ const contrib_vals = [
223
+ index_path_name,
224
+ doc_id,
225
+ file.contributors[j].login,
226
+ file.contributors[j].name,
227
+ file.contributors[j].avatar_url,
228
+ file.contributors[j].html_url,
229
+ ];
230
+ const cont_response = await hdoc_index.insert_record(
231
+ db,
232
+ "hdoc_contributors",
233
+ db_schema.hdoc_contributors,
234
+ contrib_vals,
235
+ );
236
+ if (!cont_response.success) {
237
+ console.error(
238
+ `Index document contributor record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${cont_response.error}`,
239
+ );
240
+ continue;
241
+ }
242
+ if (verbose) {
243
+ console.log(
244
+ `Inserted document contributor record ${cont_response.row_id}`,
245
+ );
246
+ }
247
+ }
248
+ response.index_success_count++;
249
+ }
250
+ }
251
+ }),
252
+ );
253
+
254
+ response.success = true;
255
+ console.log(
256
+ `\nIndex Build Complete: ${response.index_success_count} document index records created.`,
257
+ );
258
+ return response;
259
+ };
260
+ })();