hdoc-tools 0.53.0 → 0.55.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-build.js +17 -3
- package/hdoc-content-routes.js +41 -0
- package/hdoc-edit.js +2 -12
- package/hdoc-serve.js +14 -21
- package/package.json +1 -1
package/hdoc-build.js
CHANGED
|
@@ -97,8 +97,12 @@
|
|
|
97
97
|
|
|
98
98
|
// Mutable references updated immediately before each md.render() call so
|
|
99
99
|
// the highlight and frontmatter plugin callbacks can identify the current
|
|
100
|
-
// file.
|
|
101
|
-
//
|
|
100
|
+
// file. md.render() is synchronous, so these are safe to READ from within
|
|
101
|
+
// the render itself (e.g. the highlight callback reading currentMdFilePath).
|
|
102
|
+
// They are NOT safe to read back AFTER an await: transform_file runs up to 8
|
|
103
|
+
// concurrently, so a concurrent render can clobber these while we're awaiting.
|
|
104
|
+
// Anything needed past the first await must be snapshotted into a local first
|
|
105
|
+
// (see file_frontmatter in transform_file).
|
|
102
106
|
let currentMdFilePath = "";
|
|
103
107
|
let currentFrontmatter = "";
|
|
104
108
|
|
|
@@ -303,6 +307,16 @@
|
|
|
303
307
|
// Render markdown into HTML
|
|
304
308
|
html_txt = md.render(md_txt);
|
|
305
309
|
|
|
310
|
+
// Capture this file's frontmatter into a local NOW, before any await.
|
|
311
|
+
// currentFrontmatter is shared module-level state populated by the mdfm
|
|
312
|
+
// callback during the (synchronous) md.render() above. transform_file runs
|
|
313
|
+
// up to 8-at-a-time (see the chunked Promise.all in run()), so the await
|
|
314
|
+
// below yields the event loop and lets a concurrent transform_file reset
|
|
315
|
+
// and overwrite currentFrontmatter — if we read it back after the await we
|
|
316
|
+
// would parse another file's frontmatter. Snapshot it while it is still
|
|
317
|
+
// guaranteed to be ours.
|
|
318
|
+
const file_frontmatter = currentFrontmatter;
|
|
319
|
+
|
|
306
320
|
// md.render() synchronously queued any Mermaid diagrams in this file.
|
|
307
321
|
// Render them to SVG now (memoized/deduped) so the files exist before
|
|
308
322
|
// PDF generation below reads them, and before validation/zipping.
|
|
@@ -318,7 +332,7 @@
|
|
|
318
332
|
let fm_contains_reading_time = false;
|
|
319
333
|
let fm_contains_description = false;
|
|
320
334
|
|
|
321
|
-
const fm_content =
|
|
335
|
+
const fm_content = file_frontmatter.split(/\r?\n/);
|
|
322
336
|
if (fm_content.length >= 0) {
|
|
323
337
|
for (fm_prop of fm_content) {
|
|
324
338
|
const fm_id = fm_prop.slice(0, fm_prop.indexOf(":"));
|
package/hdoc-content-routes.js
CHANGED
|
@@ -33,6 +33,47 @@ function escape_html(str) {
|
|
|
33
33
|
.replace(/>/g, ">");
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// Recursively walk <source_path>/<docId>/_inline building the nav fragment
|
|
37
|
+
// consumed by the viewer's nav-section-component. Subfolders become
|
|
38
|
+
// expandable container nodes (items, no link); files become leaf links.
|
|
39
|
+
function build_inline_nav_items(dir, docId, rel_prefix) {
|
|
40
|
+
const items = [];
|
|
41
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
42
|
+
if (entry.isDirectory()) {
|
|
43
|
+
items.push({
|
|
44
|
+
text: entry.name,
|
|
45
|
+
expand: false,
|
|
46
|
+
items: build_inline_nav_items(
|
|
47
|
+
path.join(dir, entry.name),
|
|
48
|
+
docId,
|
|
49
|
+
`${rel_prefix}${entry.name}/`,
|
|
50
|
+
),
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
const name = entry.name.replace(path.extname(entry.name), "");
|
|
54
|
+
items.push({
|
|
55
|
+
text: name,
|
|
56
|
+
link: `${docId}/_inline/${rel_prefix}${name}`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return items;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Build the top-level "Inline Help Items" nav fragment for a book, or {}
|
|
64
|
+
// if the book has no _inline folder.
|
|
65
|
+
function build_nav_inline(source_path, docId) {
|
|
66
|
+
const inline_path = path.join(source_path, docId, "_inline");
|
|
67
|
+
if (!fs.existsSync(inline_path)) return {};
|
|
68
|
+
return {
|
|
69
|
+
text: "Inline Help Items",
|
|
70
|
+
expand: true,
|
|
71
|
+
inline: true,
|
|
72
|
+
items: build_inline_nav_items(inline_path, docId, ""),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
exports.build_nav_inline = build_nav_inline;
|
|
76
|
+
|
|
36
77
|
// Build a content handler bound to a single book's context.
|
|
37
78
|
//
|
|
38
79
|
// ctx:
|
package/hdoc-edit.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
const sha1 = (data) =>
|
|
27
27
|
crypto.createHash("sha1").update(Buffer.from(data)).digest("hex");
|
|
28
|
-
const { create_content_handler } = require(
|
|
28
|
+
const { create_content_handler, build_nav_inline } = require(
|
|
29
29
|
path.join(__dirname, "hdoc-content-routes.js"),
|
|
30
30
|
);
|
|
31
31
|
const { TocModel } = require(path.join(__dirname, "hdoc-toc.js"));
|
|
@@ -184,17 +184,7 @@
|
|
|
184
184
|
);
|
|
185
185
|
|
|
186
186
|
// Inline-help nav fragment, surfaced via /_books/library.json.
|
|
187
|
-
|
|
188
|
-
const inline_path = path.join(sp, id, "_inline");
|
|
189
|
-
if (fs.existsSync(inline_path)) {
|
|
190
|
-
inline = { text: "Inline Help Items", expand: true, inline: true, items: [] };
|
|
191
|
-
for (const file of fs.readdirSync(inline_path)) {
|
|
192
|
-
inline.items.push({
|
|
193
|
-
text: file.replace(path.extname(file), ""),
|
|
194
|
-
link: `${id}/_inline/${file.replace(path.extname(file), "")}`,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
187
|
+
const inline = build_nav_inline(sp, id);
|
|
198
188
|
|
|
199
189
|
source_path = sp;
|
|
200
190
|
hdocbook_project = proj;
|
package/hdoc-serve.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
const fs = require("node:fs");
|
|
5
5
|
const path = require("node:path");
|
|
6
6
|
const hdoc = require(path.join(__dirname, "hdoc-module.js"));
|
|
7
|
-
const { create_content_handler } = require(
|
|
7
|
+
const { create_content_handler, build_nav_inline } = require(
|
|
8
8
|
path.join(__dirname, "hdoc-content-routes.js"),
|
|
9
9
|
);
|
|
10
10
|
|
|
@@ -59,23 +59,7 @@
|
|
|
59
59
|
docId = hdocbook_project.docId;
|
|
60
60
|
|
|
61
61
|
// Get inline content for nav
|
|
62
|
-
const
|
|
63
|
-
let nav_inline = {};
|
|
64
|
-
if (fs.existsSync(inline_path)) {
|
|
65
|
-
const inline_files = fs.readdirSync(inline_path);
|
|
66
|
-
nav_inline = {
|
|
67
|
-
text: "Inline Help Items",
|
|
68
|
-
expand: true,
|
|
69
|
-
inline: true,
|
|
70
|
-
items: [],
|
|
71
|
-
};
|
|
72
|
-
for (const file of inline_files) {
|
|
73
|
-
nav_inline.items.push({
|
|
74
|
-
text: file.replace(path.extname(file), ""),
|
|
75
|
-
link: `${docId}/_inline/${file.replace(path.extname(file), "")}`,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
62
|
+
const nav_inline = build_nav_inline(source_path, docId);
|
|
79
63
|
// Get an express server instance
|
|
80
64
|
const app = express();
|
|
81
65
|
|
|
@@ -135,10 +119,10 @@
|
|
|
135
119
|
});
|
|
136
120
|
|
|
137
121
|
const server = app.listen(port, "0.0.0.0", () => {
|
|
138
|
-
const
|
|
139
|
-
|
|
122
|
+
const addr = server.address();
|
|
123
|
+
if (!addr) return;
|
|
140
124
|
|
|
141
|
-
console.log("Server listening at http://127.0.0.1:%s", port);
|
|
125
|
+
console.log("Server listening at http://127.0.0.1:%s", addr.port);
|
|
142
126
|
console.log(`Document source path is: ${source_path}`);
|
|
143
127
|
|
|
144
128
|
const _vars = ["{{DOC_ID}}", "{{BUILD_NUMBER}}", "{{BUILD_DATE}}"];
|
|
@@ -148,6 +132,15 @@
|
|
|
148
132
|
console.log(" ", name, " = ", hdoc.expand_variables(name, docId));
|
|
149
133
|
}
|
|
150
134
|
});
|
|
135
|
+
|
|
136
|
+
server.on("error", (err) => {
|
|
137
|
+
if (err.code === "EADDRINUSE") {
|
|
138
|
+
console.error(`\nPort ${port} is already in use. Pick a different port with -port.\n`);
|
|
139
|
+
} else {
|
|
140
|
+
console.error(`\nServer error: ${err}\n`);
|
|
141
|
+
}
|
|
142
|
+
process.exit(1);
|
|
143
|
+
});
|
|
151
144
|
};
|
|
152
145
|
|
|
153
146
|
})();
|