hdoc-tools 0.27.1 → 0.29.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-db.js +107 -97
- package/hdoc-validate.js +4 -5
- package/package.json +1 -1
package/hdoc-db.js
CHANGED
@@ -1,97 +1,107 @@
|
|
1
|
-
(() => {
|
2
|
-
const html2text = require("html-to-text");
|
3
|
-
const path = require("node:path");
|
4
|
-
const hdoc = require(path.join(__dirname, "hdoc-module.js"));
|
5
|
-
|
6
|
-
exports.create_table = (db, table_name, columns, virtual, fts5) => {
|
7
|
-
const create_sql = ["CREATE"];
|
8
|
-
if (virtual) create_sql.push("VIRTUAL");
|
9
|
-
create_sql.push("TABLE");
|
10
|
-
create_sql.push(table_name);
|
11
|
-
if (fts5) create_sql.push("USING fts5(");
|
12
|
-
else create_sql.push("(");
|
13
|
-
for (let i = 0; i < columns.length; i++) {
|
14
|
-
if (i !== 0) create_sql.push(`,${columns[i]}`);
|
15
|
-
else create_sql.push(columns[i]);
|
16
|
-
}
|
17
|
-
create_sql.push(");");
|
18
|
-
try {
|
19
|
-
db.exec(create_sql.join("\n"));
|
20
|
-
return null;
|
21
|
-
} catch (e) {
|
22
|
-
return e;
|
23
|
-
}
|
24
|
-
};
|
25
|
-
|
26
|
-
exports.insert_record = (db, table, columns, values) => {
|
27
|
-
const response = {
|
28
|
-
success: false,
|
29
|
-
row_id: 0,
|
30
|
-
error: null,
|
31
|
-
};
|
32
|
-
const queryProps = [];
|
33
|
-
queryProps.push(`INSERT INTO ${table}`);
|
34
|
-
let cols = "(";
|
35
|
-
let vals = "VALUES (";
|
36
|
-
for (let i = 0; i < columns.length; i++) {
|
37
|
-
if (i === 0) {
|
38
|
-
cols += `${columns[i].replace("UNINDEXED", "").replace("INTEGER", "").trim()}`;
|
39
|
-
vals += "?";
|
40
|
-
} else {
|
41
|
-
cols += `, ${columns[i].replace("UNINDEXED", "").replace("INTEGER", "").trim()}`;
|
42
|
-
vals += ", ?";
|
43
|
-
}
|
44
|
-
}
|
45
|
-
cols += ")";
|
46
|
-
vals += ")";
|
47
|
-
queryProps.push(cols);
|
48
|
-
queryProps.push(vals);
|
49
|
-
|
50
|
-
try {
|
51
|
-
const stmt = db.prepare(queryProps.join(" "));
|
52
|
-
const info = stmt.run(values);
|
53
|
-
response.row_id = info.lastInsertRowid;
|
54
|
-
response.success = true;
|
55
|
-
} catch (e) {
|
56
|
-
response.error = e;
|
57
|
-
}
|
58
|
-
return response;
|
59
|
-
};
|
60
|
-
|
61
|
-
exports.transform_html_for_index = (html_txt) => {
|
62
|
-
const response = {
|
63
|
-
fm_props: {},
|
64
|
-
sections: [],
|
65
|
-
};
|
66
|
-
|
67
|
-
// Get frontmatter properties
|
68
|
-
const fm_headers = hdoc.getHTMLFrontmatterHeader(html_txt);
|
69
|
-
response.fm_props = fm_headers.fm_properties;
|
70
|
-
|
71
|
-
// Convert HTML into plain text
|
72
|
-
response.text = html2text.convert(html_txt, {
|
73
|
-
ignoreHref: true,
|
74
|
-
ignoreImage: true,
|
75
|
-
uppercaseHeadings: false,
|
76
|
-
wordwrap: null,
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
1
|
+
(() => {
|
2
|
+
const html2text = require("html-to-text");
|
3
|
+
const path = require("node:path");
|
4
|
+
const hdoc = require(path.join(__dirname, "hdoc-module.js"));
|
5
|
+
|
6
|
+
exports.create_table = (db, table_name, columns, virtual, fts5) => {
|
7
|
+
const create_sql = ["CREATE"];
|
8
|
+
if (virtual) create_sql.push("VIRTUAL");
|
9
|
+
create_sql.push("TABLE");
|
10
|
+
create_sql.push(table_name);
|
11
|
+
if (fts5) create_sql.push("USING fts5(");
|
12
|
+
else create_sql.push("(");
|
13
|
+
for (let i = 0; i < columns.length; i++) {
|
14
|
+
if (i !== 0) create_sql.push(`,${columns[i]}`);
|
15
|
+
else create_sql.push(columns[i]);
|
16
|
+
}
|
17
|
+
create_sql.push(");");
|
18
|
+
try {
|
19
|
+
db.exec(create_sql.join("\n"));
|
20
|
+
return null;
|
21
|
+
} catch (e) {
|
22
|
+
return e;
|
23
|
+
}
|
24
|
+
};
|
25
|
+
|
26
|
+
exports.insert_record = (db, table, columns, values) => {
|
27
|
+
const response = {
|
28
|
+
success: false,
|
29
|
+
row_id: 0,
|
30
|
+
error: null,
|
31
|
+
};
|
32
|
+
const queryProps = [];
|
33
|
+
queryProps.push(`INSERT INTO ${table}`);
|
34
|
+
let cols = "(";
|
35
|
+
let vals = "VALUES (";
|
36
|
+
for (let i = 0; i < columns.length; i++) {
|
37
|
+
if (i === 0) {
|
38
|
+
cols += `${columns[i].replace("UNINDEXED", "").replace("INTEGER", "").trim()}`;
|
39
|
+
vals += "?";
|
40
|
+
} else {
|
41
|
+
cols += `, ${columns[i].replace("UNINDEXED", "").replace("INTEGER", "").trim()}`;
|
42
|
+
vals += ", ?";
|
43
|
+
}
|
44
|
+
}
|
45
|
+
cols += ")";
|
46
|
+
vals += ")";
|
47
|
+
queryProps.push(cols);
|
48
|
+
queryProps.push(vals);
|
49
|
+
|
50
|
+
try {
|
51
|
+
const stmt = db.prepare(queryProps.join(" "));
|
52
|
+
const info = stmt.run(values);
|
53
|
+
response.row_id = info.lastInsertRowid;
|
54
|
+
response.success = true;
|
55
|
+
} catch (e) {
|
56
|
+
response.error = e;
|
57
|
+
}
|
58
|
+
return response;
|
59
|
+
};
|
60
|
+
|
61
|
+
exports.transform_html_for_index = (html_txt) => {
|
62
|
+
const response = {
|
63
|
+
fm_props: {},
|
64
|
+
sections: [],
|
65
|
+
};
|
66
|
+
|
67
|
+
// Get frontmatter properties
|
68
|
+
const fm_headers = hdoc.getHTMLFrontmatterHeader(html_txt);
|
69
|
+
response.fm_props = fm_headers.fm_properties;
|
70
|
+
|
71
|
+
// Convert HTML into plain text
|
72
|
+
response.text = html2text.convert(html_txt, {
|
73
|
+
ignoreHref: true,
|
74
|
+
ignoreImage: true,
|
75
|
+
uppercaseHeadings: false,
|
76
|
+
wordwrap: null,
|
77
|
+
selectors: [
|
78
|
+
{ selector: 'h2', format: 'blockString' },
|
79
|
+
{ selector: 'h3', format: 'blockString' },
|
80
|
+
{ selector: 'h4', format: 'blockString' }
|
81
|
+
]
|
82
|
+
});
|
83
|
+
|
84
|
+
// Convert HTML into preview text
|
85
|
+
let preview = html2text.convert(html_txt, {
|
86
|
+
baseElement: "p",
|
87
|
+
ignoreHref: true,
|
88
|
+
ignoreImage: true,
|
89
|
+
uppercaseHeadings: false,
|
90
|
+
wordwrap: null,
|
91
|
+
selectors: [
|
92
|
+
{ selector: 'h2', format: 'blockString' },
|
93
|
+
{ selector: 'h3', format: 'blockString' },
|
94
|
+
{ selector: 'h4', format: 'blockString' }
|
95
|
+
]
|
96
|
+
});
|
97
|
+
preview = hdoc
|
98
|
+
.truncate_string(preview, 200, true)
|
99
|
+
.replace(/(?:\r\n|\r|\n)/g, " ");
|
100
|
+
response.sections.push({
|
101
|
+
text: response.text,
|
102
|
+
preview: preview,
|
103
|
+
});
|
104
|
+
//}
|
105
|
+
return response;
|
106
|
+
};
|
107
|
+
})();
|
package/hdoc-validate.js
CHANGED
@@ -458,15 +458,14 @@ const e = require("express");
|
|
458
458
|
if (!valid_url) {
|
459
459
|
// Could be a relative path, check
|
460
460
|
if (links[i].startsWith("/") && !links[i].startsWith("/#")) {
|
461
|
-
let
|
462
|
-
if (
|
463
|
-
link_root =
|
461
|
+
let link_segments = links[i].split("/");
|
462
|
+
if (link_segments[0] === "") link_segments.shift();
|
463
|
+
const link_root = link_segments[0] === "_books" ? link_segments[1] : link_segments[0];
|
464
464
|
|
465
465
|
// Checking for internal links in other books - can't easily validate those here, returning
|
466
|
-
if (link_root !== hdocbook_config.docId) {
|
466
|
+
if (link_segments.length > 1 && link_root !== hdocbook_config.docId) {
|
467
467
|
continue;
|
468
468
|
}
|
469
|
-
|
470
469
|
isRelativePath(source_path, htmlFile, links[i]);
|
471
470
|
} else if (links[i].startsWith("#") || links[i].startsWith("/#")) {
|
472
471
|
//Flat Anchor - validate we have a same-file hit
|