Skip to content

Commit 64566f1

Browse files
committed
🔤 ⚡ converter - convert also comments
1 parent 2fcec05 commit 64566f1

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

docs/components/converter.html.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ function registerClientPart(page_id){
164164

165165
document.head.append(
166166
el("script", {
167-
src: "https://unpkg.com/@beforesemicolon/html-parser/dist/client.js",
167+
// src: "https://unpkg.com/@beforesemicolon/html-parser/dist/client.js",
168+
src: "https://cdn.jsdelivr.net/npm/@beforesemicolon/html-parser/dist/client.js",
168169
type: "text/javascript",
169170
charset: "utf-8",
170171
defer: true

docs/components/converter.js.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function convertHTMLtoDDE(html, options = {}) {
4848

4949
try {
5050
const parsed = parse(html);
51-
return nodeToDDE(parsed.children[0], options);
51+
const content = parsed.children[0] || parsed.childNodes[0];
52+
return !content ? "" : nodeToDDE(content, options);
5253
} catch (error) {
5354
console.error("Parsing error:", error);
5455
return `// Error parsing HTML: ${error.message}`;
@@ -64,10 +65,14 @@ const NODE_TYPE = {
6465

6566
// Convert a parsed node to dd<el> code
6667
function nodeToDDE(node, options = {}, level = 0) {
68+
const tab= options.indent === "-1" ? "\t" : " ".repeat(options.indent);
69+
const indent = tab.repeat(level);
70+
const nextIndent = tab.repeat(level + 1);
71+
6772
const { nodeType } = node;
6873
// Handle text nodes
6974
if (nodeType === NODE_TYPE.TEXT) {
70-
const text = node.nodeValue;
75+
const text = el("i", { innerText: node.nodeValue }).textContent;
7176
if (!text.trim()) return null;
7277

7378
// Return as plain text or template string for longer text
@@ -78,15 +83,15 @@ function nodeToDDE(node, options = {}, level = 0) {
7883

7984
// Handle comment nodes
8085
if (nodeType === NODE_TYPE.COMMENT) {
81-
return null; // TODO: Skip comments?
86+
const text = node.nodeValue;
87+
if (!text.trim()) return null;
88+
return text.includes("\n")
89+
? [ "/*", ...text.trim().split("\n").map(l=> tab+l), "*/" ]
90+
: [ `// ${text}` ];
8291
}
8392

8493
// For element nodes
8594
if (nodeType === NODE_TYPE.ELEMENT) {
86-
const tab= options.indent === "-1" ? "\t" : " ".repeat(options.indent);
87-
const indent = tab.repeat(level);
88-
const nextIndent = tab.repeat(level + 1);
89-
9095
// Special case for SVG elements
9196
const isNS = node.tagName === "svg";
9297
const elFunction = isNS ? "elNS" : "el";
@@ -171,12 +176,14 @@ function nodeToDDE(node, options = {}, level = 0) {
171176
// Process children
172177
const children = [];
173178
for (const child of node.childNodes) {
174-
const childCode = nodeToDDE(child, options, level + 1);
175-
if (childCode) children.push(childCode);
179+
const childCode = nodeToDDE(child, options, level + 1);
180+
if (!childCode) continue;
181+
182+
children.push(childCode);
176183
}
177-
if(children.length===1 && node.childNodes[0].nodeType===NODE_TYPE.TEXT){
178-
const textContent= children.pop().slice(1, -1);
179-
attrs.unshift(`textContent: "${textContent}"`);
184+
if(node.childNodes.length===1 && node.childNodes[0].nodeType===NODE_TYPE.TEXT){
185+
const textContent= children.pop().slice(1, -1);
186+
attrs.unshift(`textContent: "${textContent}"`);
180187
}
181188

182189
// Build the element creation code
@@ -189,15 +196,16 @@ function nodeToDDE(node, options = {}, level = 0) {
189196
result += `, {\n${nextIndent}${attrs.join(`,\n${nextIndent}`)},\n${indent}}`;
190197
else
191198
result += `, { ${attrs.join(", ")} }`;
192-
} else if (children.length > 0) {
193-
result += ", null";
194199
}
195200

196201
// Add children if any
197202
if (children.length > 0) {
198-
result += `).append(\n${nextIndent}${children.join(`,\n${nextIndent}`)},\n${indent})`;
203+
const chs= children.map(ch=>
204+
Array.isArray(ch) ? ch.map(l=> nextIndent + l).join("\n") :
205+
nextIndent + ch + ",");
206+
result += `).append(\n${chs.join("\n")}\n${indent})`;
199207
} else {
200-
result += ")";
208+
result += ")";
201209
}
202210

203211
return result;

docs/components/ireland.html.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export function ireland({ src, exportName = "default", props = {} }) {
5555
import(path)
5656
.then(module => {
5757
const component = module[exportName];
58-
element.replaceWith(el(component, props, mark(id)));
58+
const content= el(component, props, mark(id));
59+
element.replaceWith(content);
60+
content.querySelectorAll("input, textarea, button")
61+
.forEach(el=> el.disabled= true);
5962
})
6063
.catch(console.error)
6164
);

docs/components/ireland.js.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export function loadIrelands(store) {
55
document.body.querySelectorAll("[data-dde-mark]").forEach(ireland => {
66
const { ddeMark }= ireland.dataset;
77
if(!store.has(ddeMark)) return;
8-
ireland.querySelectorAll("input").forEach(input => input.disabled = true);
98
const { path, exportName, props }= store.get(ddeMark);
109
import("./"+path).then(module => {
1110
ireland.replaceWith(el(module[exportName], props));

0 commit comments

Comments
 (0)