hdoc-tools 0.18.0 → 0.18.1
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-module.js +19 -10
- package/package.json +1 -1
package/hdoc-module.js
CHANGED
@@ -219,10 +219,11 @@
|
|
219
219
|
|
220
220
|
let nodes = Array.from(document.body.childNodes); // Convert NodeList to Array for easier manipulation
|
221
221
|
let newContent = document.createDocumentFragment(); // Create a document fragment to hold the new structure
|
222
|
-
|
222
|
+
|
223
223
|
let currentH2Div = null;
|
224
224
|
let currentH3Div = null;
|
225
|
-
|
225
|
+
let standaloneH3Div = null;
|
226
|
+
|
226
227
|
nodes.forEach(node => {
|
227
228
|
if (node.nodeType === dom.window.Node.ELEMENT_NODE) {
|
228
229
|
if (node.tagName.toLowerCase() === 'h2') {
|
@@ -235,9 +236,13 @@
|
|
235
236
|
currentH2Div.appendChild(node); // Move the <h2> into the new <div>
|
236
237
|
currentH3Div = null; // Reset currentH3Div
|
237
238
|
} else if (node.tagName.toLowerCase() === 'h3') {
|
238
|
-
// When an <h3> is found, close the current <div> (if any) and start a new one
|
239
|
+
// When an <h3> is found, close the current <h3> <div> (if any) and start a new one
|
239
240
|
if (currentH3Div) {
|
240
|
-
currentH2Div
|
241
|
+
if (currentH2Div) {
|
242
|
+
currentH2Div.appendChild(currentH3Div);
|
243
|
+
} else {
|
244
|
+
newContent.appendChild(currentH3Div);
|
245
|
+
}
|
241
246
|
}
|
242
247
|
currentH3Div = document.createElement('div');
|
243
248
|
currentH3Div.id = makeAnchorIdFriendly(node.textContent.trim()); // Set the id to the anchor-friendly text content of the <h3>
|
@@ -263,22 +268,26 @@
|
|
263
268
|
newContent.appendChild(node);
|
264
269
|
}
|
265
270
|
});
|
266
|
-
|
271
|
+
|
267
272
|
// Append the last <h3> <div> if any
|
268
273
|
if (currentH3Div) {
|
269
|
-
currentH2Div
|
274
|
+
if (currentH2Div) {
|
275
|
+
currentH2Div.appendChild(currentH3Div);
|
276
|
+
} else {
|
277
|
+
newContent.appendChild(currentH3Div);
|
278
|
+
}
|
270
279
|
}
|
271
|
-
|
280
|
+
|
272
281
|
// Append the last <h2> <div> if any
|
273
282
|
if (currentH2Div) {
|
274
283
|
newContent.appendChild(currentH2Div);
|
275
284
|
}
|
276
|
-
|
285
|
+
|
277
286
|
// Replace the old body content with the new content
|
278
287
|
document.body.innerHTML = '';
|
279
288
|
document.body.appendChild(newContent);
|
280
|
-
|
281
|
-
// Serialize the document back to HTML and
|
289
|
+
|
290
|
+
// Serialize the document back to HTML and save it to a new file (for example: 'output.html')
|
282
291
|
const outputHtml = dom.serialize();
|
283
292
|
return outputHtml;
|
284
293
|
};
|