@threadlabs/looma 0.50.0 → 0.50.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/docs/catalog.json +1 -1
- package/docs/framework-examples.mjs +26 -22
- package/package.json +1 -1
package/docs/catalog.json
CHANGED
|
@@ -3,6 +3,7 @@ import { camel, componentName, exampleInvocations, nativeBooleans } from './exam
|
|
|
3
3
|
const escapeHtml = value => String(value).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
|
4
4
|
const indent = depth => ' '.repeat(depth);
|
|
5
5
|
const textValue = node => node.preserve ? node.value : node.value.replace(/[\t\n\r\f ]+/g, ' ');
|
|
6
|
+
const formattingSpace = node => node?.kind === 'text' && !node.preserve && /^[\t\n\r\f ]*$/.test(node.value);
|
|
6
7
|
const key = value => /^[a-zA-Z_$][\w$]*$/.test(value) ? value : JSON.stringify(value);
|
|
7
8
|
const kebab = value => value.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`);
|
|
8
9
|
const voidElements = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
@@ -13,17 +14,17 @@ const reserved = new Set('await break case catch class const continue debugger d
|
|
|
13
14
|
|
|
14
15
|
export const frameworkLabels = { 'html-next': 'HTML Next', vue: 'Vue', react: 'React', svelte: 'Svelte' };
|
|
15
16
|
|
|
16
|
-
//
|
|
17
|
-
function layout(nodes
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const multiline = !inline && nodes.some(node => formattingSpace(node) && node.value.includes('\n'))
|
|
17
|
+
// Derive block layout from the tree. Mixed text and inline adjacency stay together.
|
|
18
|
+
function layout(nodes) {
|
|
19
|
+
const structural = nodes.some(node => node.kind === 'slot' || node.kind === 'element' && (node.component || !inlineElements.has(node.tag)));
|
|
20
|
+
const multiline = structural
|
|
21
21
|
&& !nodes.some(node => node.kind === 'text' && !formattingSpace(node));
|
|
22
22
|
return { multiline, nodes: multiline ? nodes.filter(node => !formattingSpace(node)) : nodes };
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
function text(node, target) {
|
|
26
26
|
const value = textValue(node);
|
|
27
|
+
if (target === 'html-next') return escapeHtml(value);
|
|
27
28
|
if (target === 'react') {
|
|
28
29
|
if (node.preserve || /[\n\r\t]/.test(node.value)) {
|
|
29
30
|
return `{${JSON.stringify(value)}}`;
|
|
@@ -37,6 +38,7 @@ function text(node, target) {
|
|
|
37
38
|
function attribute(value, target, component) {
|
|
38
39
|
let name = value.property;
|
|
39
40
|
let data = value.value;
|
|
41
|
+
if (target === 'html-next') return data === true || data === '' ? name : `${name}="${escapeHtml(data)}"`;
|
|
40
42
|
if (target === 'react') {
|
|
41
43
|
name = reactAttributes[name] ?? name;
|
|
42
44
|
if (name === 'style') {
|
|
@@ -88,14 +90,18 @@ function snippetBindings(model) {
|
|
|
88
90
|
function printer(model, target) {
|
|
89
91
|
const bindings = target === 'svelte' ? snippetBindings(model) : new Map();
|
|
90
92
|
|
|
91
|
-
function sequence(nodes, depth, forceLines = false
|
|
92
|
-
const arranged = layout(nodes
|
|
93
|
+
function sequence(nodes, depth, forceLines = false) {
|
|
94
|
+
const arranged = layout(nodes);
|
|
93
95
|
const content = arranged.nodes.map(node => (arranged.multiline ? indent(depth) : '') + render(node, depth)).join(arranged.multiline ? '\n' : '');
|
|
94
96
|
return (forceLines && !arranged.multiline ? indent(depth) : '') + content;
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
function reactValue(nodes, depth) {
|
|
98
|
-
const
|
|
100
|
+
const content = [...nodes];
|
|
101
|
+
const boundaryIndentation = node => formattingSpace(node) && /[\n\r]/.test(node.value);
|
|
102
|
+
while (boundaryIndentation(content[0])) content.shift();
|
|
103
|
+
while (boundaryIndentation(content.at(-1))) content.pop();
|
|
104
|
+
const arranged = layout(content);
|
|
99
105
|
if (!arranged.nodes.length) return 'null';
|
|
100
106
|
if (arranged.nodes.length === 1) {
|
|
101
107
|
const node = arranged.nodes[0];
|
|
@@ -104,8 +110,8 @@ function printer(model, target) {
|
|
|
104
110
|
return render(node, depth);
|
|
105
111
|
}
|
|
106
112
|
return arranged.multiline
|
|
107
|
-
? `<>\n${sequence(
|
|
108
|
-
: `<>${sequence(
|
|
113
|
+
? `<>\n${sequence(content, depth + 1)}\n${indent(depth)}</>`
|
|
114
|
+
: `<>${sequence(content, depth)}</>`;
|
|
109
115
|
}
|
|
110
116
|
|
|
111
117
|
function render(node, depth) {
|
|
@@ -121,14 +127,13 @@ function printer(model, target) {
|
|
|
121
127
|
if (node.slots.length && target === 'react') {
|
|
122
128
|
attrs.push(`slots={{\n${node.slots.map(slot => `${indent(depth + 2)}${key(slot.name)}: ${reactValue(slot.children, depth + 2)},`).join('\n')}\n${indent(depth + 1)}}}`);
|
|
123
129
|
}
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
const start = wrapped ? `<${node.tag}\n${attrs.map(value => indent(depth + 1) + value).join('\n')}\n${indent(depth)}` : opening;
|
|
130
|
+
const wrapped = attrs.length > 0;
|
|
131
|
+
const start = wrapped ? `<${node.tag}\n${attrs.map(value => indent(depth + 1) + value).join('\n')}\n${indent(depth)}` : `<${node.tag}`;
|
|
127
132
|
const children = target === 'vue' ? node.children : node.children.filter(child => child.kind !== 'slot');
|
|
128
|
-
const arranged = layout(children
|
|
133
|
+
const arranged = layout(children);
|
|
129
134
|
if (!arranged.nodes.length && (node.component || target === 'react')) return `${start}${wrapped ? '' : ' '}/>`;
|
|
130
135
|
if (voidElements.has(node.tag)) return `${start}>`;
|
|
131
|
-
const content = sequence(children, depth + 1
|
|
136
|
+
const content = sequence(children, depth + 1);
|
|
132
137
|
return arranged.multiline
|
|
133
138
|
? `${start}>\n${content}\n${indent(depth)}</${node.tag}>`
|
|
134
139
|
: `${start}>${content}</${node.tag}>`;
|
|
@@ -136,8 +141,8 @@ function printer(model, target) {
|
|
|
136
141
|
|
|
137
142
|
function body(depth) {
|
|
138
143
|
const arranged = layout(model.nodes);
|
|
139
|
-
if (target
|
|
140
|
-
return
|
|
144
|
+
if (target === 'react') return reactValue(model.nodes, depth);
|
|
145
|
+
return (arranged.multiline ? '' : indent(depth)) + sequence(model.nodes, depth);
|
|
141
146
|
}
|
|
142
147
|
|
|
143
148
|
return {
|
|
@@ -155,15 +160,14 @@ export function frameworkExamples(example, components) {
|
|
|
155
160
|
const line = `import { ${names.join(', ')} } from ${JSON.stringify(owner)};`;
|
|
156
161
|
return line.length <= 100 ? line : `import {\n${names.map(name => ` ${name},`).join('\n')}\n} from ${JSON.stringify(owner)};`;
|
|
157
162
|
}).join('\n');
|
|
158
|
-
const links = model.components.map(component => `<link
|
|
159
|
-
const
|
|
163
|
+
const links = model.components.map(component => `<link\n rel="component"\n href="${escapeHtml(component.resource ?? `${component.package}/components/${component.tag}/${component.tag}.html`)}"\n>`).join('\n');
|
|
164
|
+
const html = printer(exampleInvocations(example.markup, []), 'html-next').body(0);
|
|
165
|
+
const codes = { 'html-next': { language: 'html', code: [links, html].filter(Boolean).join('\n\n') } };
|
|
160
166
|
for (const target of ['vue', 'react', 'svelte']) {
|
|
161
167
|
const output = printer(model, target);
|
|
162
168
|
const script = imports ? `<script${target === 'vue' ? ' setup' : ''}>\n${imports.split('\n').map(line => ' ' + line).join('\n')}\n</script>\n\n` : '';
|
|
163
|
-
const jsx = target === 'react' ? output.body(2) : '';
|
|
164
|
-
const reactReturn = jsx.includes('\n') || jsx.length + 10 > 100 ? `return (\n ${jsx}\n );` : `return ${jsx};`;
|
|
165
169
|
codes[target] = target === 'react'
|
|
166
|
-
? { language: 'tsx', code: `${imports ? imports + '\n\n' : ''}export default function Example() {\n ${
|
|
170
|
+
? { language: 'tsx', code: `${imports ? imports + '\n\n' : ''}export default function Example() {\n return ${output.body(1)};\n}` }
|
|
167
171
|
: { language: target, code: script + (target === 'vue' ? `<template>\n${output.body(1)}\n</template>` : [output.snippets(), output.body(0)].filter(Boolean).join('\n\n')) };
|
|
168
172
|
}
|
|
169
173
|
return { ...codes, ...example.frameworks };
|