verso-cms 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +288 -2
- package/bin/verso.js +16 -0
- package/editor/door.js +139 -0
- package/editor/editor.js +1880 -0
- package/editor/elevate.css +2158 -0
- package/editor/ext-attrs.js +87 -0
- package/editor/ext-blocks.js +422 -0
- package/editor/ext-builder.js +819 -0
- package/editor/ext-channels.js +574 -0
- package/editor/ext-components.js +160 -0
- package/editor/ext-history.js +242 -0
- package/editor/ext-palette.js +307 -0
- package/editor/ext-people.js +345 -0
- package/editor/ext-repeaters.js +143 -0
- package/editor/ext-screens.js +94 -0
- package/editor/ext-site-settings.js +318 -0
- package/editor/ext-site-view.js +632 -0
- package/editor/ext-tokens.js +1166 -0
- package/editor/new-page.js +252 -0
- package/editor/rich.js +23 -0
- package/package.json +69 -4
- package/src/blocks.js +269 -0
- package/src/box.js +339 -0
- package/src/build.js +210 -0
- package/src/cli.js +161 -0
- package/src/color.js +137 -0
- package/src/components.js +223 -0
- package/src/elevate.js +74 -0
- package/src/errors.js +46 -0
- package/src/extensions/README.md +70 -0
- package/src/extensions/blocks.js +294 -0
- package/src/extensions/builder.js +734 -0
- package/src/extensions/channels.js +341 -0
- package/src/extensions/components.js +141 -0
- package/src/extensions/history.js +169 -0
- package/src/extensions/palette.js +31 -0
- package/src/extensions/people.js +113 -0
- package/src/extensions/site-settings.js +251 -0
- package/src/extensions/site-view.js +381 -0
- package/src/extensions/tags.js +15 -0
- package/src/extensions/tokens.js +587 -0
- package/src/fonts.js +139 -0
- package/src/git.js +419 -0
- package/src/html-edit.js +942 -0
- package/src/images.js +116 -0
- package/src/people.js +286 -0
- package/src/rich.js +124 -0
- package/src/scaffold.js +185 -0
- package/src/screens.js +65 -0
- package/src/server.js +693 -0
- package/src/site.js +618 -0
- package/src/template.js +915 -0
- package/src/video.js +29 -0
- package/starter/Dockerfile +20 -0
- package/starter/README.md +49 -0
- package/starter/blocks/call-to-action.html +6 -0
- package/starter/blocks/features.html +15 -0
- package/starter/blocks/hero.html +8 -0
- package/starter/blocks/image-and-text.html +8 -0
- package/starter/blocks/latest-posts.html +20 -0
- package/starter/blocks/post-index.html +30 -0
- package/starter/blocks/quote.html +9 -0
- package/starter/blocks/site-footer.html +8 -0
- package/starter/blocks/site-nav.html +8 -0
- package/starter/blocks/tag-list.html +10 -0
- package/starter/blocks/tagged-posts.html +19 -0
- package/starter/blocks/team.html +15 -0
- package/starter/blocks/text.html +4 -0
- package/starter/content/404/page.json +21 -0
- package/starter/content/about/page.json +65 -0
- package/starter/content/journal/designing-for-the-morning-shift/body.html +22 -0
- package/starter/content/journal/designing-for-the-morning-shift/checklist.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/cover.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/page.json +17 -0
- package/starter/content/journal/page.json +19 -0
- package/starter/content/journal/the-first-week/body.html +17 -0
- package/starter/content/journal/the-first-week/cover.jpg +0 -0
- package/starter/content/journal/the-first-week/page.json +17 -0
- package/starter/content/journal/the-first-week/wall-of-cards.jpg +0 -0
- package/starter/content/page.json +94 -0
- package/starter/content/site.json +88 -0
- package/starter/content/studio-table.jpg +0 -0
- package/starter/content/topics/page.json +27 -0
- package/starter/dockerignore +5 -0
- package/starter/elevate/config/design.ts +30 -0
- package/starter/elevate/config/elevate.ts +16 -0
- package/starter/elevate/config/syntax.ts +6 -0
- package/starter/elevate/design/site.ts +9 -0
- package/starter/elevate/design/tokens.json +20 -0
- package/starter/elevate/extend/fonts.css +1 -0
- package/starter/gitignore +6 -0
- package/starter/netlify.toml +5 -0
- package/starter/parts/.gitkeep +0 -0
- package/starter/public/favicon.svg +1 -0
- package/starter/templates/page.html +20 -0
- package/starter/templates/page.layout.json +23 -0
- package/starter/templates/post.html +65 -0
- package/starter/verso.defaults.json +33 -0
- package/starter-blank/blocks/site-footer.html +3 -0
- package/starter-blank/blocks/site-nav.html +8 -0
- package/starter-blank/blocks/text.html +4 -0
- package/starter-blank/content/404/page.json +18 -0
- package/starter-blank/content/page.json +18 -0
- package/starter-blank/content/site.json +36 -0
- package/starter-blank/elevate/design/tokens.json +10 -0
- package/starter-blank/templates/page.html +17 -0
- package/starter-blank/verso.defaults.json +26 -0
package/src/html-edit.js
ADDED
|
@@ -0,0 +1,942 @@
|
|
|
1
|
+
// Surgical edits to hand-written HTML. Verso only changes what it
|
|
2
|
+
// understands (elements, class strings, fields, slots), at exact source
|
|
3
|
+
// offsets, so everything else in a file stays byte for byte as it was.
|
|
4
|
+
|
|
5
|
+
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
|
|
6
|
+
const RAW = new Set(['script', 'style', 'textarea', 'title']);
|
|
7
|
+
|
|
8
|
+
// ════ Parse ════
|
|
9
|
+
// Elements with their offsets. Paths are child indexes from the top:
|
|
10
|
+
// "0" is the first top-level element, "0.2" its third child element.
|
|
11
|
+
export function parseFragment(source) {
|
|
12
|
+
const top = { children: [], path: '', depth: -1 };
|
|
13
|
+
const stack = [top];
|
|
14
|
+
let pos = 0;
|
|
15
|
+
while (pos < source.length) {
|
|
16
|
+
const lt = source.indexOf('<', pos);
|
|
17
|
+
if (lt === -1) break;
|
|
18
|
+
if (source.startsWith('<!--', lt)) {
|
|
19
|
+
const close = source.indexOf('-->', lt + 4);
|
|
20
|
+
pos = close === -1 ? source.length : close + 3;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const head = /^<(\/?)([a-zA-Z][\w:-]*)/.exec(source.slice(lt, lt + 80));
|
|
24
|
+
if (!head) { pos = lt + 1; continue; }
|
|
25
|
+
let j = lt + head[0].length;
|
|
26
|
+
let quote = null;
|
|
27
|
+
let braces = 0;
|
|
28
|
+
for (; j < source.length; j++) {
|
|
29
|
+
const c = source[j];
|
|
30
|
+
if (quote) { if (c === quote) quote = null; continue; }
|
|
31
|
+
if (source.startsWith('{{', j)) { braces++; j++; continue; }
|
|
32
|
+
if (braces && source.startsWith('}}', j)) { braces--; j++; continue; }
|
|
33
|
+
if (braces) continue;
|
|
34
|
+
if (c === '"' || c === "'") quote = c;
|
|
35
|
+
else if (c === '>') break;
|
|
36
|
+
}
|
|
37
|
+
const openEnd = j; // index of '>'
|
|
38
|
+
const name = head[2].toLowerCase();
|
|
39
|
+
if (head[1] === '/') {
|
|
40
|
+
for (let k = stack.length - 1; k > 0; k--) {
|
|
41
|
+
if (stack[k].tag === name) {
|
|
42
|
+
const el = stack[k];
|
|
43
|
+
el.closeStart = lt;
|
|
44
|
+
el.end = openEnd + 1;
|
|
45
|
+
stack.length = k;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
pos = openEnd + 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const parent = stack[stack.length - 1];
|
|
53
|
+
const selfClosing = source[openEnd - 1] === '/';
|
|
54
|
+
const el = {
|
|
55
|
+
tag: name,
|
|
56
|
+
start: lt,
|
|
57
|
+
openEnd,
|
|
58
|
+
selfClosing,
|
|
59
|
+
parent,
|
|
60
|
+
children: [],
|
|
61
|
+
path: parent.path === '' ? String(parent.children.length) : `${parent.path}.${parent.children.length}`,
|
|
62
|
+
attr: readClassAttr(source, lt, openEnd),
|
|
63
|
+
};
|
|
64
|
+
parent.children.push(el);
|
|
65
|
+
if (VOID.has(name) || selfClosing) {
|
|
66
|
+
el.end = openEnd + 1;
|
|
67
|
+
el.void = true;
|
|
68
|
+
} else if (RAW.has(name)) {
|
|
69
|
+
const close = source.toLowerCase().indexOf(`</${name}`, openEnd);
|
|
70
|
+
const closeEnd = close === -1 ? source.length : source.indexOf('>', close);
|
|
71
|
+
el.closeStart = close === -1 ? source.length : close;
|
|
72
|
+
el.end = closeEnd === -1 ? source.length : closeEnd + 1;
|
|
73
|
+
el.raw = true;
|
|
74
|
+
pos = el.end;
|
|
75
|
+
continue;
|
|
76
|
+
} else {
|
|
77
|
+
stack.push(el);
|
|
78
|
+
}
|
|
79
|
+
pos = openEnd + 1;
|
|
80
|
+
}
|
|
81
|
+
// Unclosed elements end where the file ends.
|
|
82
|
+
stack.slice(1).forEach((el) => {
|
|
83
|
+
el.closeStart = source.length;
|
|
84
|
+
el.end = source.length;
|
|
85
|
+
});
|
|
86
|
+
return top;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The class attribute inside an open tag: value range and quote style.
|
|
90
|
+
function readClassAttr(source, start, openEnd) {
|
|
91
|
+
const tag = source.slice(start, openEnd);
|
|
92
|
+
const re = /\sclass\s*=\s*(["'])([\s\S]*?)\1/g;
|
|
93
|
+
const m = re.exec(tag);
|
|
94
|
+
if (m) {
|
|
95
|
+
const valueStart = start + m.index + m[0].indexOf(m[1]) + 1;
|
|
96
|
+
return { valueStart, valueEnd: valueStart + m[2].length, value: m[2], quote: m[1] };
|
|
97
|
+
}
|
|
98
|
+
const bare = /\sclass\s*=\s*([^\s"'>]+)/.exec(tag);
|
|
99
|
+
if (bare) {
|
|
100
|
+
const valueStart = start + bare.index + bare[0].length - bare[1].length;
|
|
101
|
+
return { valueStart, valueEnd: valueStart + bare[1].length, value: bare[1], quote: '' };
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function findElement(root, path) {
|
|
107
|
+
const parts = String(path).split('.').map(Number);
|
|
108
|
+
let node = root;
|
|
109
|
+
for (const i of parts) {
|
|
110
|
+
node = node.children[i];
|
|
111
|
+
if (!node) return null;
|
|
112
|
+
}
|
|
113
|
+
return node;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function allElements(root) {
|
|
117
|
+
const out = [];
|
|
118
|
+
const walk = (n) => n.children.forEach((c) => { out.push(c); walk(c); });
|
|
119
|
+
walk(root);
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ════ Edits ════
|
|
124
|
+
// Each returns the new source. Nothing outside the edited range changes.
|
|
125
|
+
|
|
126
|
+
const fail = (message) => { const e = new Error(message); e.plain = true; throw e; };
|
|
127
|
+
const need = (root, path) => findElement(root, path) || fail('That element is no longer in the file. Reload the builder.');
|
|
128
|
+
const splice = (source, start, end, text) => source.slice(0, start) + text + source.slice(end);
|
|
129
|
+
|
|
130
|
+
export function setClass(source, path, value) {
|
|
131
|
+
const el = need(parseFragment(source), path);
|
|
132
|
+
if (el.attr) {
|
|
133
|
+
const q = el.attr.quote || (/[\s"'=<>`]/.test(value) ? '"' : '');
|
|
134
|
+
if (!el.attr.quote && q) return splice(source, el.attr.valueStart, el.attr.valueEnd, `"${value}"`);
|
|
135
|
+
if (q && value.includes(q)) fail('A class string can\'t contain quotes.');
|
|
136
|
+
return splice(source, el.attr.valueStart, el.attr.valueEnd, value);
|
|
137
|
+
}
|
|
138
|
+
if (!value) return source;
|
|
139
|
+
// Before the closing > (or before the space and / of a self-closing tag).
|
|
140
|
+
let at = el.selfClosing ? el.openEnd - 1 : el.openEnd;
|
|
141
|
+
while (el.selfClosing && source[at - 1] === ' ') at -= 1;
|
|
142
|
+
return splice(source, at, at, ` class="${value}"`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const lineStart = (source, i) => source.lastIndexOf('\n', i - 1) + 1;
|
|
146
|
+
const indentAt = (source, i) => /^[ \t]*/.exec(source.slice(lineStart(source, i)))[0];
|
|
147
|
+
const onlySpaceBefore = (source, i) => /^[ \t]*$/.test(source.slice(lineStart(source, i), i));
|
|
148
|
+
|
|
149
|
+
// Inserts html as a child of the element at path, before child `index`
|
|
150
|
+
// (or at the end). Indentation follows the file's own.
|
|
151
|
+
export function insertChild(source, path, html, index = null) {
|
|
152
|
+
const root = parseFragment(source);
|
|
153
|
+
const el = path === '' ? null : need(root, path);
|
|
154
|
+
if (el && (el.void || el.raw)) fail(`A <${el.tag}> can't hold other elements.`);
|
|
155
|
+
const siblings = el ? el.children : root.children;
|
|
156
|
+
// A piece of several lines keeps its shape at the depth it goes in.
|
|
157
|
+
const at = (indent) => html.replace(/\n/g, `\n${indent}`);
|
|
158
|
+
if (index != null && index < siblings.length) {
|
|
159
|
+
const before = siblings[index];
|
|
160
|
+
if (onlySpaceBefore(source, before.start)) {
|
|
161
|
+
const indent = indentAt(source, before.start);
|
|
162
|
+
return splice(source, before.start, before.start, `${at(indent)}\n${indent}`);
|
|
163
|
+
}
|
|
164
|
+
return splice(source, before.start, before.start, html);
|
|
165
|
+
}
|
|
166
|
+
if (!el) return `${source.replace(/\s*$/, '')}\n${html}\n`;
|
|
167
|
+
const close = el.closeStart;
|
|
168
|
+
const parentIndent = indentAt(source, el.start);
|
|
169
|
+
const childIndent = siblings.length ? indentAt(source, siblings[siblings.length - 1].start) : `${parentIndent} `;
|
|
170
|
+
if (onlySpaceBefore(source, close)) {
|
|
171
|
+
const ls = lineStart(source, close);
|
|
172
|
+
return splice(source, ls, ls, `${childIndent}${at(childIndent)}\n`);
|
|
173
|
+
}
|
|
174
|
+
if (!source.slice(el.openEnd + 1, close).trim()) {
|
|
175
|
+
return splice(source, el.openEnd + 1, close, `\n${childIndent}${at(childIndent)}\n${parentIndent}`);
|
|
176
|
+
}
|
|
177
|
+
return splice(source, close, close, html);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// The range an element occupies, including its whole line when it's alone on it.
|
|
181
|
+
function lineRange(source, el) {
|
|
182
|
+
let { start, end } = el;
|
|
183
|
+
if (onlySpaceBefore(source, start) && /^[ \t]*(\r?\n|$)/.test(source.slice(end))) {
|
|
184
|
+
start = lineStart(source, start);
|
|
185
|
+
const nl = source.indexOf('\n', end);
|
|
186
|
+
end = nl === -1 ? source.length : nl + 1;
|
|
187
|
+
}
|
|
188
|
+
return { start, end };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function removeElement(source, path) {
|
|
192
|
+
const el = need(parseFragment(source), path);
|
|
193
|
+
const { start, end } = lineRange(source, el);
|
|
194
|
+
return splice(source, start, end, '');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Swaps an element with its previous (-1) or next (+1) sibling element. The
|
|
198
|
+
// whitespace and anything between them stays where it was.
|
|
199
|
+
// An element with the conditions or lists wrapped tightly around it, like
|
|
200
|
+
// {{ if:photo }}<img …>{{ /if }}: those move with it, so a move never leaves
|
|
201
|
+
// a condition guarding a different element.
|
|
202
|
+
function unitSpan(source, el) {
|
|
203
|
+
let start = el.start;
|
|
204
|
+
let end = el.end;
|
|
205
|
+
for (;;) {
|
|
206
|
+
const before = /\{\{\s*(if|each|list)\b[^{}]*\}\}\s*$/.exec(source.slice(0, start));
|
|
207
|
+
const after = before && new RegExp(`^\\s*\\{\\{\\s*\\/${before[1]}\\s*\\}\\}`).exec(source.slice(end));
|
|
208
|
+
if (!before || !after) break;
|
|
209
|
+
start = before.index;
|
|
210
|
+
end += after[0].length;
|
|
211
|
+
}
|
|
212
|
+
return { start, end };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function moveElement(source, path, dir) {
|
|
216
|
+
const root = parseFragment(source);
|
|
217
|
+
const el = need(root, path);
|
|
218
|
+
const siblings = el.parent.children;
|
|
219
|
+
const i = siblings.indexOf(el);
|
|
220
|
+
const other = siblings[i + dir];
|
|
221
|
+
if (!other) return source;
|
|
222
|
+
const [a, b] = (dir < 0 ? [other, el] : [el, other]).map((x) => unitSpan(source, x));
|
|
223
|
+
if (a.end > b.start) return source; // the two share a wrapper; nothing safe to swap
|
|
224
|
+
const textA = source.slice(a.start, a.end);
|
|
225
|
+
const textB = source.slice(b.start, b.end);
|
|
226
|
+
return source.slice(0, a.start) + textB + source.slice(a.end, b.start) + textA + source.slice(b.end);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function wrapElement(source, path, tag = 'div') {
|
|
230
|
+
if (!/^[a-z][a-z0-9]*$/.test(tag) || VOID.has(tag)) fail(`<${tag}> can't wrap other elements.`);
|
|
231
|
+
const el = need(parseFragment(source), path);
|
|
232
|
+
return splice(source, el.start, el.end, `<${tag}>${source.slice(el.start, el.end)}</${tag}>`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function unwrapElement(source, path) {
|
|
236
|
+
const el = need(parseFragment(source), path);
|
|
237
|
+
if (el.void || el.raw) fail(`A <${el.tag}> has nothing inside to keep.`);
|
|
238
|
+
if (!el.parent.tag && el.parent.children.length === 1 && el.children.length !== 1) fail('A block needs one element around everything. Unwrap this one after adding another around it.');
|
|
239
|
+
return splice(source, el.start, el.end, source.slice(el.openEnd + 1, el.closeStart));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ════ Palette ════
|
|
243
|
+
// The pieces the builder can add. Field names are made unique in the file.
|
|
244
|
+
|
|
245
|
+
export function uniqueName(source, base) {
|
|
246
|
+
const used = new Set([...source.matchAll(/\{\{\s*([a-zA-Z][\w-]*)\s*:/g)].map((m) => m[1]));
|
|
247
|
+
if (!used.has(base)) return base;
|
|
248
|
+
for (let n = 2; ; n++) if (!used.has(`${base}-${n}`)) return `${base}-${n}`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Site field names are their own set: {{ site.note:text }}.
|
|
252
|
+
function uniqueSiteName(source, base) {
|
|
253
|
+
const used = new Set([...source.matchAll(/\{\{\s*site\.([\w-]+)/g)].map((m) => m[1]));
|
|
254
|
+
if (!used.has(base)) return `site.${base}`;
|
|
255
|
+
for (let n = 2; ; n++) if (!used.has(`${base}-${n}`)) return `site.${base}-${n}`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Lists of pages and repeaters get a name of their own too.
|
|
259
|
+
function uniqueListName(source, base) {
|
|
260
|
+
const used = new Set([...source.matchAll(/\{\{\s*list:([\w-]+)/g)].map((m) => m[1]));
|
|
261
|
+
if (!used.has(base)) return base;
|
|
262
|
+
for (let n = 2; ; n++) if (!used.has(`${base}-${n}`)) return `${base}-${n}`;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// A card for one page in a list: title linked to the page, date, description.
|
|
266
|
+
const pageCard = () => `<article class="col gap:d2">
|
|
267
|
+
<h3><a href="{{ item.url }}">{{ item.label }}</a></h3>
|
|
268
|
+
<time datetime="{{ item.date-iso }}">{{ item.date:short }}</time>
|
|
269
|
+
<p>{{ item.description:words-25 }}</p>
|
|
270
|
+
</article>`;
|
|
271
|
+
|
|
272
|
+
// A tag as a small chip: role="list" drops the bullets, and the link inside
|
|
273
|
+
// keeps the text colour (Elevate's reset styles links without a class).
|
|
274
|
+
const TAG_CHIP = '<li class="pd:d1:d3 bd:round-full bd:width-px:solid font:small"><a href="{{ item.url }}">{{ item.title }}</a></li>';
|
|
275
|
+
|
|
276
|
+
// `where` says when the builder offers a piece: 'any' (outside lists of
|
|
277
|
+
// pages), 'item' (inside a list of pages, about each page), 'paged' (when
|
|
278
|
+
// the block has a list split into pages), 'always'.
|
|
279
|
+
export const PALETTE = {
|
|
280
|
+
box: { label: 'Box', group: 'Layout', where: 'always', html: () => '<div class="pd:d4"></div>' },
|
|
281
|
+
row: { label: 'Row', group: 'Layout', where: 'always', html: () => '<div class="row gap:d4"></div>' },
|
|
282
|
+
column: { label: 'Column', group: 'Layout', where: 'always', html: () => '<div class="col gap:d4"></div>' },
|
|
283
|
+
heading: { label: 'Heading', group: 'Words and pictures', where: 'any', html: (s) => `<h2>{{ ${uniqueName(s, 'heading')}:text }}</h2>` },
|
|
284
|
+
subheading: { label: 'Small heading', group: 'Words and pictures', where: 'any', html: (s) => `<h3>{{ ${uniqueName(s, 'subheading')}:text }}</h3>` },
|
|
285
|
+
paragraph: { label: 'Text', group: 'Words and pictures', where: 'any', html: (s) => `<p>{{ ${uniqueName(s, 'text')}:text }}</p>` },
|
|
286
|
+
rich: { label: 'Rich text', group: 'Words and pictures', where: 'any', html: (s) => `<div>{{ ${uniqueName(s, 'body')}:rich:headings:lists:links }}</div>` },
|
|
287
|
+
image: { label: 'Image', group: 'Words and pictures', where: 'any', html: (s) => `<img src="{{ ${uniqueName(s, 'image')}:image }}" alt="">` },
|
|
288
|
+
link: { label: 'Link', group: 'Words and pictures', where: 'any', html: (s) => { const n = uniqueName(s, 'link'); return `<a href="{{ ${n}-url:text }}">{{ ${n}-label:text }}</a>`; } },
|
|
289
|
+
button: { label: 'Button', group: 'Words and pictures', where: 'any', html: (s) => { const n = uniqueName(s, 'button'); return `<a href="{{ ${n}-url:text }}" class="row:x-center:y-center pd:d5:d3 bd:round-d2 bd:width-px:solid font:semibold w:fit-content">{{ ${n}-label:text }}</a>`; } },
|
|
290
|
+
quote: { label: 'Quote', group: 'Words and pictures', where: 'any', html: (s) => `<blockquote><p>{{ ${uniqueName(s, 'quote')}:text }}</p></blockquote>` },
|
|
291
|
+
words: { label: 'Fixed words', group: 'Words and pictures', where: 'always', html: () => '<p>Words that are the same everywhere this block is used</p>' },
|
|
292
|
+
slot: { label: 'Block area', group: 'Lists', where: 'any', html: (s) => `<div class="col gap:d4">{{ ${uniqueName(s, 'items')}:blocks }}</div>` },
|
|
293
|
+
'page-list': { label: 'List of pages', group: 'Lists', where: 'any', html: (s, o = {}) => `<div class="col gap:d6">
|
|
294
|
+
{{ each:${o.folder || 'children'}:newest:3 }}
|
|
295
|
+
${pageCard()}
|
|
296
|
+
{{ else }}
|
|
297
|
+
<p>Nothing here yet.</p>
|
|
298
|
+
{{ /each }}
|
|
299
|
+
</div>` },
|
|
300
|
+
repeater: { label: 'List of items', group: 'Lists', where: 'any', html: (s) => {
|
|
301
|
+
const n = uniqueListName(s, 'items');
|
|
302
|
+
return `<ul class="col gap:d3">
|
|
303
|
+
{{ list:${n} }}
|
|
304
|
+
<li class="col gap:d1">
|
|
305
|
+
<h3>{{ name:text }}</h3>
|
|
306
|
+
<p>{{ detail:text }}</p>
|
|
307
|
+
</li>
|
|
308
|
+
{{ /list }}
|
|
309
|
+
</ul>`;
|
|
310
|
+
} },
|
|
311
|
+
count: { label: 'Count of pages', group: 'Lists', where: 'any', html: (s, o = {}) => `<p><span>{{ count:${o.folder || 'children'} }}</span> pages</p>` },
|
|
312
|
+
pager: { label: 'Previous and next pages', group: 'Lists', where: 'paged', html: () => `<nav class="row:y-center:x-between gap:d4">
|
|
313
|
+
{{ if:paging.previous }}<a href="{{ paging.previous }}">Newer</a>{{ /if }}
|
|
314
|
+
<span>Page {{ paging.number }} of {{ paging.total }}</span>
|
|
315
|
+
{{ if:paging.next }}<a href="{{ paging.next }}">Older</a>{{ /if }}
|
|
316
|
+
</nav>` },
|
|
317
|
+
'page-title': { label: 'Title', group: 'This page', where: 'any', html: () => '<h1>{{ page.title }}</h1>' },
|
|
318
|
+
'page-date': { label: 'Date', group: 'This page', where: 'any', html: () => '<time datetime="{{ page.date-iso }}">{{ page.date }}</time>' },
|
|
319
|
+
'page-description': { label: 'Description', group: 'This page', where: 'any', html: () => '<p>{{ page.description }}</p>' },
|
|
320
|
+
'page-tags': { label: 'Tags', group: 'This page', where: 'any', html: () => `<ul role="list" class="row:wrap gap:d2">{{ each:tags }}${TAG_CHIP}{{ /each }}</ul>` },
|
|
321
|
+
'site-text': { label: 'Text on every page', group: 'Every page', where: 'any', html: (s) => `<p>{{ ${uniqueSiteName(s, 'note')}:text }}</p>` },
|
|
322
|
+
'item-title': { label: 'Its title', group: 'Each page in the list', where: 'item', html: () => '<h3><a href="{{ item.url }}">{{ item.label }}</a></h3>' },
|
|
323
|
+
'item-date': { label: 'Its date', group: 'Each page in the list', where: 'item', html: () => '<time datetime="{{ item.date-iso }}">{{ item.date:short }}</time>' },
|
|
324
|
+
'item-description': { label: 'Its description', group: 'Each page in the list', where: 'item', html: () => '<p>{{ item.description:words-25 }}</p>' },
|
|
325
|
+
'item-image': { label: 'Its image', group: 'Each page in the list', where: 'item', html: (s, o = {}) => `<img src="{{ item.${o.field || 'image'} }}" class="block w:full h:auto aspect:landscape fit:cover">` },
|
|
326
|
+
'item-text': { label: 'One of its fields', group: 'Each page in the list', where: 'item', html: (s, o = {}) => `<p>{{ item.${o.field || 'summary'} }}</p>` },
|
|
327
|
+
'item-link': { label: 'Link to it', group: 'Each page in the list', where: 'item', html: () => '<a href="{{ item.url }}">Read more</a>' },
|
|
328
|
+
'item-tags': { label: 'Its tags', group: 'Each page in the list', where: 'item', html: () => `<ul role="list" class="row:wrap gap:d2">{{ each:item.tags }}${TAG_CHIP}{{ /each }}</ul>` },
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
export function insertPalette(source, path, item, index = null, options = {}) {
|
|
332
|
+
const piece = PALETTE[item];
|
|
333
|
+
if (!piece) fail(`"${item}" isn't something the builder can add.`);
|
|
334
|
+
if (options.folder != null && !/^[a-zA-Z0-9][a-zA-Z0-9_/-]*$|^(children|siblings)$/.test(options.folder)) fail('Pick a folder of pages.');
|
|
335
|
+
if (options.field != null && !/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(options.field)) fail('Pick one of the pages\' fields.');
|
|
336
|
+
return insertChild(source, path, withStyles(piece.html(source, options), options.styles), index);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Puts a piece of HTML right after an element, at its indent, and says
|
|
340
|
+
// where it landed. An element with a condition wrapped tightly around it
|
|
341
|
+
// keeps it to itself; a list's card has the piece go after the whole list.
|
|
342
|
+
export function insertAfter(source, path, html, reindent = true) {
|
|
343
|
+
const el = need(parseFragment(source), path);
|
|
344
|
+
if (!el.parent.tag) fail('Nothing goes beside the outer element of a block. Add it inside instead.');
|
|
345
|
+
let { start, end } = unitSpan(source, el);
|
|
346
|
+
constructs(source).forEach((c) => {
|
|
347
|
+
if ((c.kind === 'each' || c.kind === 'list') && c.open.end <= start && end <= c.close.start && c.open.start > el.parent.openEnd && c.close.end <= el.parent.closeStart) {
|
|
348
|
+
start = Math.min(start, c.open.start);
|
|
349
|
+
end = Math.max(end, c.close.end);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
const alone = onlySpaceBefore(source, start);
|
|
353
|
+
const indent = alone ? indentAt(source, start) : '';
|
|
354
|
+
const lead = alone ? `\n${indent}` : '';
|
|
355
|
+
const out = splice(source, end, end, `${lead}${reindent ? html.replace(/\n/g, `\n${indent}`) : html}`);
|
|
356
|
+
const at = end + lead.length;
|
|
357
|
+
const added = allElements(parseFragment(out)).find((e) => e.start === at);
|
|
358
|
+
return { source: out, path: added ? added.path : null };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// A copy of an element, right after it.
|
|
362
|
+
export function duplicateElement(source, path) {
|
|
363
|
+
const el = need(parseFragment(source), path);
|
|
364
|
+
if (!el.parent.tag) fail('A block has one outer element. Copy something inside it instead.');
|
|
365
|
+
return insertAfter(source, path, source.slice(el.start, el.end), false);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Gives unstyled headings, paragraphs, links, lists and quotes the site's
|
|
369
|
+
// article styles from verso.json's "rich" map, the way rich text looks.
|
|
370
|
+
// Elements that already have classes keep them.
|
|
371
|
+
const STYLED = ['h1', 'h2', 'h3', 'h4', 'p', 'a', 'ul', 'ol', 'li', 'blockquote', 'time', 'img'];
|
|
372
|
+
export function withStyles(html, styles) {
|
|
373
|
+
if (!styles || typeof styles !== 'object') return html;
|
|
374
|
+
return html.replace(/<([a-z][a-z0-9]*)(\s[^>]*)?>/g, (whole, tag, attrs = '') => {
|
|
375
|
+
const classes = styles[tag];
|
|
376
|
+
if (!STYLED.includes(tag) || typeof classes !== 'string' || !classes.trim() || /\sclass\s*=/.test(attrs)) return whole;
|
|
377
|
+
return `<${tag} class="${classes.trim().replace(/"/g, '"')}"${attrs}>`;
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ════ Template tags ════
|
|
382
|
+
// Lists, conditions and repeaters are tags around elements:
|
|
383
|
+
// {{ each:journal }}<article>…</article>{{ else }}…{{ /each }}. The builder
|
|
384
|
+
// finds them by position, shows them as "constructs" around the elements
|
|
385
|
+
// they hold, and edits only the tag text.
|
|
386
|
+
|
|
387
|
+
export function scanTags(source) {
|
|
388
|
+
const out = [];
|
|
389
|
+
const re = /\{\{([\s\S]*?)\}\}/g;
|
|
390
|
+
let m;
|
|
391
|
+
while ((m = re.exec(source))) {
|
|
392
|
+
const raw = m[1].trim();
|
|
393
|
+
let kind = 'other';
|
|
394
|
+
if (/^each(:|$)/.test(raw)) kind = 'each';
|
|
395
|
+
else if (/^if(:|$)/.test(raw)) kind = 'if';
|
|
396
|
+
else if (/^list(:|$)/.test(raw)) kind = 'list';
|
|
397
|
+
else if (raw === 'else') kind = 'else';
|
|
398
|
+
else if (/^\/(each|if|list)$/.test(raw)) kind = 'close';
|
|
399
|
+
out.push({ start: m.index, end: m.index + m[0].length, raw, kind });
|
|
400
|
+
}
|
|
401
|
+
return out;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Every list, condition and repeater, in source order. Ids are their order,
|
|
405
|
+
// so they're good for the source they came from.
|
|
406
|
+
export function constructs(source) {
|
|
407
|
+
const list = [];
|
|
408
|
+
const stack = [];
|
|
409
|
+
scanTags(source).forEach((t) => {
|
|
410
|
+
if (t.kind === 'each' || t.kind === 'if' || t.kind === 'list') {
|
|
411
|
+
const c = { id: list.length, kind: t.kind, open: t, parent: stack[stack.length - 1]?.id ?? null };
|
|
412
|
+
list.push(c);
|
|
413
|
+
stack.push(c);
|
|
414
|
+
} else if (t.kind === 'else' && stack.length) {
|
|
415
|
+
stack[stack.length - 1].alt = t;
|
|
416
|
+
} else if (t.kind === 'close' && stack.length) {
|
|
417
|
+
const c = stack.pop();
|
|
418
|
+
c.close = t;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return list.filter((c) => c.close).map((c) => ({ ...c, info: c.kind === 'each' ? readList(c.open.raw) : c.kind === 'if' ? readCondition(c.open.raw) : { name: c.open.raw.slice(5).trim() } }));
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const SORTS = ['newest', 'oldest', 'a-z', 'z-a', 'order', 'most'];
|
|
425
|
+
|
|
426
|
+
// {{ each:journal:oldest:3:skip-1:paged:tagged-design:has-cover:kind=essay }}
|
|
427
|
+
export function readList(raw) {
|
|
428
|
+
const [, section = '', ...mods] = raw.split(':').map((p) => p.trim());
|
|
429
|
+
const info = { section, sort: null, limit: null, skip: 0, paged: false, others: false, filters: [] };
|
|
430
|
+
mods.forEach((m) => {
|
|
431
|
+
let f;
|
|
432
|
+
if (SORTS.includes(m)) info.sort = m;
|
|
433
|
+
else if (/^\d+$/.test(m)) info.limit = Number(m);
|
|
434
|
+
else if (/^skip-\d+$/.test(m)) info.skip = Number(m.slice(5));
|
|
435
|
+
else if (m === 'paged') info.paged = true;
|
|
436
|
+
else if (m === 'others') info.others = true;
|
|
437
|
+
else if ((f = /^tagged-(.+)$/.exec(m))) info.filters.push({ type: 'tagged', value: f[1] });
|
|
438
|
+
else if ((f = /^(has|no)-(.+)$/.exec(m))) info.filters.push({ type: f[1], field: f[2] });
|
|
439
|
+
else if ((f = /^([a-zA-Z][\w-]*)(!?=)(.*)$/.exec(m))) info.filters.push({ type: f[2] === '=' ? 'is' : 'not', field: f[1], value: f[3].replace(/^"(.*)"$/, '$1') });
|
|
440
|
+
});
|
|
441
|
+
return info;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const NAME = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
445
|
+
const SECTION = /^(children|siblings|tagged|tags|all-tags|item|item\.tags|[a-zA-Z0-9][a-zA-Z0-9_/-]*)$/;
|
|
446
|
+
const VALUE = /^[^:{}"<>]*$/;
|
|
447
|
+
|
|
448
|
+
export function listTag(info) {
|
|
449
|
+
if (!SECTION.test(info.section || '')) fail('Pick a folder of pages for the list.');
|
|
450
|
+
const parts = ['each', info.section];
|
|
451
|
+
if (info.sort && info.sort !== 'newest') {
|
|
452
|
+
if (!SORTS.includes(info.sort)) fail(`"${info.sort}" isn't a way to sort.`);
|
|
453
|
+
parts.push(info.sort);
|
|
454
|
+
}
|
|
455
|
+
if (info.limit) parts.push(String(Math.max(1, Math.floor(Number(info.limit)) || 1)));
|
|
456
|
+
if (info.skip) parts.push(`skip-${Math.max(0, Math.floor(Number(info.skip)) || 0)}`);
|
|
457
|
+
if (info.paged) {
|
|
458
|
+
if (!info.limit) fail('Say how many go on each page before splitting the list into pages.');
|
|
459
|
+
parts.push('paged');
|
|
460
|
+
}
|
|
461
|
+
if (info.others) parts.push('others');
|
|
462
|
+
(info.filters || []).forEach((f) => {
|
|
463
|
+
if (f.type === 'tagged') {
|
|
464
|
+
const slug = String(f.value || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
465
|
+
if (!slug) fail('Pick a tag to filter by.');
|
|
466
|
+
parts.push(`tagged-${slug}`);
|
|
467
|
+
} else if (f.type === 'has' || f.type === 'no') {
|
|
468
|
+
if (!NAME.test(f.field || '')) fail('Pick a field to filter by.');
|
|
469
|
+
parts.push(`${f.type}-${f.field}`);
|
|
470
|
+
} else if (f.type === 'is' || f.type === 'not') {
|
|
471
|
+
if (!NAME.test(f.field || '')) fail('Pick a field to filter by.');
|
|
472
|
+
const v = String(f.value ?? '').trim();
|
|
473
|
+
if (!v || !VALUE.test(v) || /\s/.test(v)) fail('Filter values are one word, without : or quotes.');
|
|
474
|
+
parts.push(`${f.field}${f.type === 'is' ? '=' : '!='}${v}`);
|
|
475
|
+
} else fail(`"${f.type}" isn't a filter.`);
|
|
476
|
+
});
|
|
477
|
+
return `{{ ${parts.join(':')} }}`;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// {{ if:cover }}, {{ if:!cover }}, {{ if:kind=essay }}, {{ if:item.cover }}
|
|
481
|
+
export function readCondition(raw) {
|
|
482
|
+
const m = /^if:\s*(!)?([\w.-]+?)\s*(?:(!?=)\s*(.*))?$/.exec(raw);
|
|
483
|
+
if (!m) return { target: '', op: 'filled', value: '' };
|
|
484
|
+
return { target: m[2], op: m[1] ? 'empty' : m[3] === '=' ? 'is' : m[3] === '!=' ? 'not' : 'filled', value: (m[4] || '').replace(/^"(.*)"$/, '$1') };
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function conditionTag(info) {
|
|
488
|
+
const target = String(info.target || '');
|
|
489
|
+
if (!/^((item|page|parent|paging|site)\.)?[a-zA-Z][a-zA-Z0-9_-]*$/.test(target)) fail('Pick what to check.');
|
|
490
|
+
const op = info.op || 'filled';
|
|
491
|
+
if (op === 'filled') return `{{ if:${target} }}`;
|
|
492
|
+
if (op === 'empty') return `{{ if:!${target} }}`;
|
|
493
|
+
const v = String(info.value ?? '').trim();
|
|
494
|
+
if (!v || !VALUE.test(v)) fail('Say what value to compare with, without : or quotes.');
|
|
495
|
+
const quoted = /\s/.test(v) ? `"${v}"` : v;
|
|
496
|
+
if (op === 'is') return `{{ if:${target}=${quoted} }}`;
|
|
497
|
+
if (op === 'not') return `{{ if:${target}!=${quoted} }}`;
|
|
498
|
+
return fail(`"${op}" isn't a way to compare.`);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const needConstruct = (source, id) => constructs(source).find((c) => c.id === Number(id)) || fail('That list or condition is no longer in the file. Reload the builder.');
|
|
502
|
+
|
|
503
|
+
// Rewrites a list's or condition's opening tag.
|
|
504
|
+
export function setConstructTag(source, id, info) {
|
|
505
|
+
const c = needConstruct(source, id);
|
|
506
|
+
const tag = c.kind === 'each' ? listTag({ ...c.info, ...info }) : c.kind === 'if' ? conditionTag({ ...c.info, ...info }) : fail('A list of items has nothing to change here.');
|
|
507
|
+
return splice(source, c.open.start, c.open.end, tag);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const escapeText = (t) => String(t).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
511
|
+
|
|
512
|
+
// What shows when a list is empty, or a condition isn't met: a line of
|
|
513
|
+
// words, or nothing. text null takes the {{ else }} away.
|
|
514
|
+
export function setOtherwise(source, id, text) {
|
|
515
|
+
const c = needConstruct(source, id);
|
|
516
|
+
if (c.kind === 'list') fail('A list of items has no "otherwise".');
|
|
517
|
+
const html = text == null ? '' : `{{ else }}<p>${escapeText(String(text).trim() || 'Nothing here yet.')}</p>`;
|
|
518
|
+
if (c.alt) {
|
|
519
|
+
if (text == null) {
|
|
520
|
+
const from = onlySpaceBefore(source, c.alt.start) ? lineStart(source, c.alt.start) : c.alt.start;
|
|
521
|
+
const to = onlySpaceBefore(source, c.close.start) ? lineStart(source, c.close.start) : c.close.start;
|
|
522
|
+
return splice(source, from, to, '');
|
|
523
|
+
}
|
|
524
|
+
const tail = /\s*$/.exec(source.slice(c.alt.start, c.close.start))[0];
|
|
525
|
+
return splice(source, c.alt.start, c.close.start - tail.length, html);
|
|
526
|
+
}
|
|
527
|
+
if (text == null) return source;
|
|
528
|
+
if (onlySpaceBefore(source, c.close.start)) {
|
|
529
|
+
const indent = indentAt(source, c.close.start);
|
|
530
|
+
const ls = lineStart(source, c.close.start);
|
|
531
|
+
return splice(source, ls, ls, `${indent}${html}\n`);
|
|
532
|
+
}
|
|
533
|
+
return splice(source, c.close.start, c.close.start, html);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// The words shown when a list is empty or a condition isn't met.
|
|
537
|
+
export function otherwiseText(source, c) {
|
|
538
|
+
if (!c.alt) return null;
|
|
539
|
+
const inner = source.slice(c.alt.end, c.close.start).trim();
|
|
540
|
+
const m = /^<p>([^<{}]*)<\/p>$/.exec(inner);
|
|
541
|
+
return m ? m[1].replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&') : inner ? '' : null;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// Wraps an element in a condition: it shows only when the check passes.
|
|
545
|
+
export function wrapCondition(source, path, info) {
|
|
546
|
+
const el = need(parseFragment(source), path);
|
|
547
|
+
if (!el.parent.tag) fail('The outer element of a block always shows. Put a condition on something inside it.');
|
|
548
|
+
return splice(source, el.start, el.end, `${conditionTag(info)}${source.slice(el.start, el.end)}{{ /if }}`);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// What a condition shows when it isn't met: nothing, a line of words, or an
|
|
552
|
+
// element of the block.
|
|
553
|
+
export function otherwiseKind(source, c) {
|
|
554
|
+
if (!c.alt) return 'nothing';
|
|
555
|
+
const inner = source.slice(c.alt.end, c.close.start).trim();
|
|
556
|
+
if (!inner) return 'nothing';
|
|
557
|
+
return /^<p>[^<{}]*<\/p>$/.test(inner) ? 'words' : 'element';
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// "Otherwise show the element after it": the element right after a
|
|
561
|
+
// condition moves into its {{ else }}, so one of the two always shows (a
|
|
562
|
+
// highlighted card when Badge is filled, the plain one otherwise).
|
|
563
|
+
export function otherwiseNext(source, id) {
|
|
564
|
+
const c = needConstruct(source, id);
|
|
565
|
+
if (c.kind !== 'if') fail('Only a condition can show something else instead.');
|
|
566
|
+
const next = allElements(parseFragment(source)).filter((e) => e.start >= c.close.end).sort((a, b) => a.start - b.start)[0];
|
|
567
|
+
if (!next || source.slice(c.close.end, next.start).trim()) fail('There\'s no element right after it to show instead. Put one there first.');
|
|
568
|
+
const text = source.slice(next.start, next.end);
|
|
569
|
+
let out = splice(source, c.close.end, next.end, '');
|
|
570
|
+
if (c.alt) {
|
|
571
|
+
const from = onlySpaceBefore(out, c.alt.start) ? lineStart(out, c.alt.start) : c.alt.start;
|
|
572
|
+
out = splice(out, from, onlySpaceBefore(out, c.close.start) ? lineStart(out, c.close.start) : c.close.start, '');
|
|
573
|
+
}
|
|
574
|
+
const fresh = needConstruct(out, id);
|
|
575
|
+
const b = (() => {
|
|
576
|
+
if (onlySpaceBefore(out, fresh.close.start)) {
|
|
577
|
+
const indent = indentAt(out, fresh.close.start);
|
|
578
|
+
return { at: lineStart(out, fresh.close.start), text: `${indent}{{ else }}\n${indent}${text}\n` };
|
|
579
|
+
}
|
|
580
|
+
return { at: fresh.close.start, text: `{{ else }}${text}` };
|
|
581
|
+
})();
|
|
582
|
+
return splice(out, b.at, b.at, b.text);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// The reverse: the element in a condition's {{ else }} goes back to after
|
|
586
|
+
// the condition, and the condition shows nothing otherwise.
|
|
587
|
+
export function otherwiseOut(source, id) {
|
|
588
|
+
const c = needConstruct(source, id);
|
|
589
|
+
if (!c.alt) return source;
|
|
590
|
+
const inner = source.slice(c.alt.end, c.close.start).trim();
|
|
591
|
+
const indent = onlySpaceBefore(source, c.open.start) ? indentAt(source, c.open.start) : '';
|
|
592
|
+
let out = splice(source, c.close.end, c.close.end, inner ? (indent || onlySpaceBefore(source, c.close.start) ? `\n${indent}${inner}` : inner) : '');
|
|
593
|
+
const from = onlySpaceBefore(out, c.alt.start) ? lineStart(out, c.alt.start) : c.alt.start;
|
|
594
|
+
const to = onlySpaceBefore(out, c.close.start) ? lineStart(out, c.close.start) : c.close.start;
|
|
595
|
+
out = splice(out, from, to, '');
|
|
596
|
+
return out;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// Takes a list's or condition's tags away and keeps what it held (without
|
|
600
|
+
// its "otherwise").
|
|
601
|
+
export function unwrapConstruct(source, id) {
|
|
602
|
+
const c = needConstruct(source, id);
|
|
603
|
+
if (c.kind === 'list') fail('Remove the whole list of items instead: select its outer element and press Remove.');
|
|
604
|
+
const closeRange = (() => {
|
|
605
|
+
if (onlySpaceBefore(source, c.close.start) && /^[ \t]*(\r?\n|$)/.test(source.slice(c.close.end))) {
|
|
606
|
+
const nl = source.indexOf('\n', c.close.end);
|
|
607
|
+
return { start: lineStart(source, c.close.start), end: nl === -1 ? source.length : nl + 1 };
|
|
608
|
+
}
|
|
609
|
+
return { start: c.close.start, end: c.close.end };
|
|
610
|
+
})();
|
|
611
|
+
let out = splice(source, c.alt ? (onlySpaceBefore(source, c.alt.start) ? lineStart(source, c.alt.start) : c.alt.start) : closeRange.start, closeRange.end, '');
|
|
612
|
+
const openRange = onlySpaceBefore(out, c.open.start) && /^[ \t]*(\r?\n|$)/.test(out.slice(c.open.end))
|
|
613
|
+
? { start: lineStart(out, c.open.start), end: out.indexOf('\n', c.open.end) + 1 || out.length }
|
|
614
|
+
: { start: c.open.start, end: c.open.end };
|
|
615
|
+
out = splice(out, openRange.start, openRange.end, '');
|
|
616
|
+
return out;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// The plain words inside an element (no elements or tags in it), or null.
|
|
620
|
+
export function plainText(source, el) {
|
|
621
|
+
if (el.void || el.raw || el.children.length) return null;
|
|
622
|
+
const inner = source.slice(el.openEnd + 1, el.closeStart);
|
|
623
|
+
if (/\{\{/.test(inner)) return null;
|
|
624
|
+
return inner.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').replace(/\s+/g, ' ').trim();
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
export function setText(source, path, text) {
|
|
628
|
+
const el = need(parseFragment(source), path);
|
|
629
|
+
if (plainText(source, el) == null) fail('Only plain words can be changed here. Fields are filled in on the page.');
|
|
630
|
+
const words = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
631
|
+
if (!words) fail('Write something, or remove the element.');
|
|
632
|
+
return splice(source, el.openEnd + 1, el.closeStart, escapeText(words));
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// References in an element's own text, like {{ item.date:short }}.
|
|
636
|
+
export function refsIn(source, el) {
|
|
637
|
+
const from = el.openEnd + 1;
|
|
638
|
+
const to = el.children[0] ? el.children[0].start : el.closeStart;
|
|
639
|
+
if (el.void || el.raw || to <= from) return [];
|
|
640
|
+
return [...source.slice(from, to).matchAll(/\{\{\s*((?:item|page|parent)\.([\w-]+))(?::([a-z0-9-]+))?\s*\}\}/g)]
|
|
641
|
+
.map((m) => ({ ref: m[1], key: m[2], format: m[3] || null, start: from + m.index, end: from + m.index + m[0].length }));
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
const FORMATS = /^(long|short|numeric|month|year|iso|words-\d+|chars-\d+)$/;
|
|
645
|
+
export function setFormat(source, path, ref, format) {
|
|
646
|
+
const el = need(parseFragment(source), path);
|
|
647
|
+
const found = refsIn(source, el).find((r) => r.ref === ref) || fail('That reference is no longer there.');
|
|
648
|
+
if (format && !FORMATS.test(format)) fail(`"${format}" isn't a format.`);
|
|
649
|
+
return splice(source, found.start, found.end, `{{ ${ref}${format ? `:${format}` : ''} }}`);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// A rich text field's tools: {{ body:rich:headings:lists }}.
|
|
653
|
+
const RICH_TOOLS = ['headings', 'lists', 'links', 'images', 'tables', 'quotes', 'callouts', 'embeds', 'code'];
|
|
654
|
+
export function setRichTools(source, path, tools) {
|
|
655
|
+
const el = need(parseFragment(source), path);
|
|
656
|
+
const inner = source.slice(el.openEnd + 1, el.closeStart);
|
|
657
|
+
const m = /\{\{\s*([\w.-]+):rich((?::[\w-]+)*)\s*\}\}/.exec(inner);
|
|
658
|
+
if (!m) fail('That element has no rich text in it.');
|
|
659
|
+
const bad = (tools || []).find((t) => !RICH_TOOLS.includes(t));
|
|
660
|
+
if (bad) fail(`"${bad}" isn't a rich text tool.`);
|
|
661
|
+
const keep = m[2].split(':').filter((t) => t === 'required');
|
|
662
|
+
const at = el.openEnd + 1 + m.index;
|
|
663
|
+
return splice(source, at, at + m[0].length, `{{ ${[m[1], 'rich', ...keep, ...RICH_TOOLS.filter((t) => (tools || []).includes(t))].join(':')} }}`);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// A colour picked on the page, from the site's colours, for an element's
|
|
667
|
+
// background or text: style="background-color: {{ background:color }}".
|
|
668
|
+
export function setColorField(source, path, prop, on) {
|
|
669
|
+
const el = need(parseFragment(source), path);
|
|
670
|
+
const css = { background: 'background-color', text: 'color' }[prop] || fail('Pick background or text.');
|
|
671
|
+
const tag = source.slice(el.start, el.openEnd);
|
|
672
|
+
const style = /\sstyle\s*=\s*"([^"]*)"/.exec(tag);
|
|
673
|
+
const decl = new RegExp(`(^|;)\\s*${css}\\s*:[^;]*`);
|
|
674
|
+
if (on) {
|
|
675
|
+
const name = uniqueName(source, prop === 'background' ? 'background' : 'text-colour');
|
|
676
|
+
const add = `${css}: {{ ${name}:color }}`;
|
|
677
|
+
if (!style) {
|
|
678
|
+
const at = el.selfClosing ? el.openEnd - 1 : el.openEnd;
|
|
679
|
+
return splice(source, at, at, ` style="${add}"`);
|
|
680
|
+
}
|
|
681
|
+
if (decl.test(style[1])) return source;
|
|
682
|
+
const start = el.start + style.index + style[0].indexOf('"') + 1;
|
|
683
|
+
const value = style[1].trim() ? `${style[1].replace(/;?\s*$/, '')}; ${add}` : add;
|
|
684
|
+
return splice(source, start, start + style[1].length, value);
|
|
685
|
+
}
|
|
686
|
+
if (!style) return source;
|
|
687
|
+
const value = style[1].replace(decl, '').replace(/^\s*;\s*/, '').trim();
|
|
688
|
+
const attrStart = el.start + style.index;
|
|
689
|
+
if (!value) return splice(source, attrStart, attrStart + style[0].length, '');
|
|
690
|
+
const start = attrStart + style[0].indexOf('"') + 1;
|
|
691
|
+
return splice(source, start, start + style[1].length, value);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
export function colorFields(source, el) {
|
|
695
|
+
const tag = source.slice(el.start, el.openEnd);
|
|
696
|
+
const style = /\sstyle\s*=\s*"([^"]*)"/.exec(tag)?.[1] || '';
|
|
697
|
+
return { background: /background-color\s*:\s*\{\{[^}]*:color/.test(style), text: /(^|;)\s*color\s*:\s*\{\{[^}]*:color/.test(style) };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// ════ Class strings ════
|
|
701
|
+
// Tokens with their ranges in the string, the breakpoint they fall under,
|
|
702
|
+
// and, when describe() knows them, their property and modifier types.
|
|
703
|
+
|
|
704
|
+
const MARKER = /^\/[a-zA-Z0-9]{1,3}\/$/;
|
|
705
|
+
|
|
706
|
+
export function tokenize(value) {
|
|
707
|
+
const tokens = [];
|
|
708
|
+
let bp = null;
|
|
709
|
+
let i = 0;
|
|
710
|
+
while (i < value.length) {
|
|
711
|
+
if (/\s/.test(value[i])) { i++; continue; }
|
|
712
|
+
let j = i;
|
|
713
|
+
let depth = 0;
|
|
714
|
+
while (j < value.length && (depth > 0 || !/\s/.test(value[j]))) {
|
|
715
|
+
if (value[j] === '[' || value[j] === '(') depth++;
|
|
716
|
+
else if (value[j] === ']' || value[j] === ')') depth = Math.max(0, depth - 1);
|
|
717
|
+
j++;
|
|
718
|
+
}
|
|
719
|
+
const raw = value.slice(i, j);
|
|
720
|
+
if (MARKER.test(raw)) {
|
|
721
|
+
bp = raw.slice(1, -1);
|
|
722
|
+
tokens.push({ raw, start: i, end: j, marker: true, bp });
|
|
723
|
+
} else {
|
|
724
|
+
tokens.push({ raw, start: i, end: j, bp });
|
|
725
|
+
}
|
|
726
|
+
i = j;
|
|
727
|
+
}
|
|
728
|
+
return tokens;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// What type a modifier is, for a property, using describe().
|
|
732
|
+
export function modifierType(system, prop, mod) {
|
|
733
|
+
const info = system.properties[prop];
|
|
734
|
+
if (!info) return null;
|
|
735
|
+
for (const type of info.accepts) {
|
|
736
|
+
if (system.tokens[type] && mod in system.tokens[type]) return type;
|
|
737
|
+
if (system.rules[type] && mod in system.rules[type]) return type;
|
|
738
|
+
}
|
|
739
|
+
// Prefix rules like round- in bd:round-d4.
|
|
740
|
+
for (const type of info.accepts) {
|
|
741
|
+
const rules = system.rules[type];
|
|
742
|
+
if (!rules) continue;
|
|
743
|
+
for (const [key, valueType] of Object.entries(rules)) {
|
|
744
|
+
if (key.endsWith('-') && mod.startsWith(key)) {
|
|
745
|
+
const rest = mod.slice(key.length);
|
|
746
|
+
if ((system.tokens[valueType] && rest in system.tokens[valueType]) || (system.rules[valueType] && rest in system.rules[valueType])) return `${key}${valueType}`;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return null;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
export function decodeTokens(value, system) {
|
|
754
|
+
return tokenize(value).map((t) => {
|
|
755
|
+
if (t.marker) return { raw: t.raw, marker: true, bp: t.bp };
|
|
756
|
+
if (t.raw.startsWith('@') || t.raw.startsWith('-') || t.raw.includes('(')) return { raw: t.raw, bp: t.bp, understood: false };
|
|
757
|
+
const [prop, ...mods] = t.raw.split(':');
|
|
758
|
+
if (!system.properties[prop]) return { raw: t.raw, bp: t.bp, understood: false };
|
|
759
|
+
const decoded = mods.map((m) => ({ mod: m, type: modifierType(system, prop, m) }));
|
|
760
|
+
return { raw: t.raw, bp: t.bp, prop, mods: decoded, understood: decoded.every((d) => d.type) };
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// ════ Pickers ════
|
|
765
|
+
// Each picker owns one thing: a modifier of one type on one property, or a
|
|
766
|
+
// prefix modifier (bd:round-…), or the layout token. A pick changes only
|
|
767
|
+
// that, in place, at the chosen breakpoint.
|
|
768
|
+
|
|
769
|
+
export const PICKERS = {
|
|
770
|
+
padding: { prop: 'pd', type: 'SpacingToken' },
|
|
771
|
+
'padding-x': { prop: 'pd-x', type: 'SpacingToken' },
|
|
772
|
+
'padding-y': { prop: 'pd-y', type: 'SpacingToken' },
|
|
773
|
+
margin: { prop: 'mg', type: 'SpacingToken' },
|
|
774
|
+
gap: { prop: 'gap', type: 'SpacingToken' },
|
|
775
|
+
background: { prop: 'bg-color', type: 'ColorToken' },
|
|
776
|
+
'text-color': { prop: 'text', type: 'ColorToken' },
|
|
777
|
+
leading: { prop: 'text', type: 'LineHeightToken' },
|
|
778
|
+
family: { prop: 'font', type: 'FontFamilyToken' },
|
|
779
|
+
size: { prop: 'font', type: 'FontSizeToken' },
|
|
780
|
+
weight: { prop: 'font', type: 'FontWeightToken' },
|
|
781
|
+
radius: { prop: 'bd', prefix: 'round-', type: 'SpacingToken' },
|
|
782
|
+
shadow: { prop: 'shadow', type: 'ShadowToken' },
|
|
783
|
+
// How many columns a grid has: grid:cols-3-fr. Picked per breakpoint, so a
|
|
784
|
+
// grid can be 1 across on phones, 2 on tablets and 3 on desktops.
|
|
785
|
+
columns: { layout: true, type: 'columns' },
|
|
786
|
+
'align-x': { layout: true, type: 'xAxis' },
|
|
787
|
+
'align-y': { layout: true, type: 'yAxis' },
|
|
788
|
+
layout: { layout: true },
|
|
789
|
+
width: { prop: 'w', type: 'SpacingToken' },
|
|
790
|
+
'max-width': { prop: 'max-w', type: 'SpacingToken' },
|
|
791
|
+
};
|
|
792
|
+
export const LAYOUTS = ['row', 'col', 'grid', 'stack'];
|
|
793
|
+
|
|
794
|
+
const inGroup = (t, bp) => !t.marker && (t.bp || null) === (bp || null);
|
|
795
|
+
|
|
796
|
+
// Returns the new class string. value null removes the pick.
|
|
797
|
+
export function applyPick(value, pickerName, pick, bp, system) {
|
|
798
|
+
const picker = PICKERS[pickerName];
|
|
799
|
+
if (!picker) fail(`"${pickerName}" isn't a picker.`);
|
|
800
|
+
const tokens = tokenize(value);
|
|
801
|
+
const group = tokens.filter((t) => inGroup(t, bp));
|
|
802
|
+
|
|
803
|
+
const replaceToken = (t, text) => {
|
|
804
|
+
if (text) return value.slice(0, t.start) + text + value.slice(t.end);
|
|
805
|
+
// Remove the token and one neighbouring space.
|
|
806
|
+
let { start, end } = t;
|
|
807
|
+
if (value[end] === ' ') end += 1;
|
|
808
|
+
else if (start > 0 && value[start - 1] === ' ') start -= 1;
|
|
809
|
+
return value.slice(0, start) + value.slice(end);
|
|
810
|
+
};
|
|
811
|
+
const addToken = (text) => {
|
|
812
|
+
if (!text) return value;
|
|
813
|
+
if (!bp) {
|
|
814
|
+
const firstMarker = tokens.find((t) => t.marker);
|
|
815
|
+
if (!firstMarker) return value.trim() ? `${value.replace(/\s+$/, '')} ${text}${value.match(/\s*$/)[0]}` : text;
|
|
816
|
+
return `${value.slice(0, firstMarker.start).replace(/\s*$/, '')}${firstMarker.start ? ' ' : ''}${text} ${value.slice(firstMarker.start)}`.replace(/^ /, '');
|
|
817
|
+
}
|
|
818
|
+
const marker = tokens.find((t) => t.marker && t.bp === bp);
|
|
819
|
+
if (!marker) {
|
|
820
|
+
// Breakpoints are mobile-first: a marker applies to every class after
|
|
821
|
+
// it, so groups go smallest first. Put a new one before any larger.
|
|
822
|
+
const order = (system?.breakpoints || []).map((b) => b.name);
|
|
823
|
+
const rank = (name) => { const i = order.indexOf(name); return i === -1 ? Infinity : i; };
|
|
824
|
+
const later = tokens.find((t) => t.marker && rank(t.bp) > rank(bp));
|
|
825
|
+
if (later && rank(bp) !== Infinity) return `${value.slice(0, later.start)}/${bp}/ ${text} ${value.slice(later.start)}`;
|
|
826
|
+
return `${value.replace(/\s+$/, '')}${value.trim() ? ' ' : ''}/${bp}/ ${text}`;
|
|
827
|
+
}
|
|
828
|
+
const last = [...tokens].reverse().find((t) => !t.marker && t.bp === bp) || marker;
|
|
829
|
+
return `${value.slice(0, last.end)} ${text}${value.slice(last.end)}`;
|
|
830
|
+
};
|
|
831
|
+
|
|
832
|
+
if (picker.layout) {
|
|
833
|
+
const current = group.find((t) => LAYOUTS.includes(t.raw.split(':')[0]));
|
|
834
|
+
if (pickerName === 'columns') {
|
|
835
|
+
if (pick != null && !/^[1-9]$|^1[0-2]$/.test(String(pick))) fail(`"${pick}" isn't a number of columns.`);
|
|
836
|
+
const cols = (m) => /^cols-\d+-fr$/.test(m);
|
|
837
|
+
if (!current) return pick ? addToken(`grid:cols-${pick}-fr`) : value;
|
|
838
|
+
const [prop, ...mods] = current.raw.split(':');
|
|
839
|
+
// A row or column becomes a grid; its gap comes along.
|
|
840
|
+
const kept = prop === 'grid' ? mods.filter((m) => !cols(m)) : mods.filter((m) => /^gap-/.test(m));
|
|
841
|
+
if (!pick) {
|
|
842
|
+
if (prop !== 'grid') return value;
|
|
843
|
+
// At a larger screen, a bare "grid" repeats the smaller screens' layout;
|
|
844
|
+
// drop it, and its marker if nothing else is left there.
|
|
845
|
+
if (bp && !kept.length) {
|
|
846
|
+
const out = replaceToken(current, '');
|
|
847
|
+
const rest = tokenize(out).filter((t) => inGroup(t, bp));
|
|
848
|
+
if (rest.length) return out;
|
|
849
|
+
const marker = tokenize(out).find((t) => t.marker && t.bp === bp);
|
|
850
|
+
return marker ? `${out.slice(0, marker.start)}${out.slice(marker.end)}`.replace(/\s{2,}/g, ' ').trim() : out;
|
|
851
|
+
}
|
|
852
|
+
return replaceToken(current, [prop, ...kept].join(':'));
|
|
853
|
+
}
|
|
854
|
+
return replaceToken(current, ['grid', `cols-${pick}-fr`, ...kept].join(':'));
|
|
855
|
+
}
|
|
856
|
+
if (pickerName === 'layout') {
|
|
857
|
+
if (!pick) return current ? replaceToken(current, '') : value;
|
|
858
|
+
if (!LAYOUTS.includes(pick)) fail(`"${pick}" isn't a layout.`);
|
|
859
|
+
if (!current) return addToken(pick);
|
|
860
|
+
const mods = current.raw.split(':').slice(1);
|
|
861
|
+
return replaceToken(current, [pick, ...mods].join(':'));
|
|
862
|
+
}
|
|
863
|
+
if (!current) {
|
|
864
|
+
if (!pick) return value;
|
|
865
|
+
return addToken(`row:${pick}`);
|
|
866
|
+
}
|
|
867
|
+
const [prop, ...mods] = current.raw.split(':');
|
|
868
|
+
const kept = mods.filter((m) => modifierType(system, prop, m) !== picker.type);
|
|
869
|
+
return replaceToken(current, [prop, ...kept, ...(pick ? [pick] : [])].join(':'));
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const owns = (m) => (picker.prefix ? m.startsWith(picker.prefix) : modifierType(system, picker.prop, m) === picker.type);
|
|
873
|
+
const make = (v) => (picker.prefix ? `${picker.prefix}${v}` : v);
|
|
874
|
+
// Prefer a token of this property that already has a modifier of this kind.
|
|
875
|
+
const candidates = group.filter((t) => t.raw.split(':')[0] === picker.prop);
|
|
876
|
+
const withMod = candidates.find((t) => t.raw.split(':').slice(1).some(owns));
|
|
877
|
+
const target = withMod || candidates[0];
|
|
878
|
+
if (!target) return pick ? addToken(`${picker.prop}:${make(pick)}`) : value;
|
|
879
|
+
const mods = target.raw.split(':').slice(1);
|
|
880
|
+
const index = mods.findIndex(owns);
|
|
881
|
+
if (index === -1) {
|
|
882
|
+
if (!pick) return value;
|
|
883
|
+
return replaceToken(target, [picker.prop, ...mods, make(pick)].join(':'));
|
|
884
|
+
}
|
|
885
|
+
if (pick) mods[index] = make(pick);
|
|
886
|
+
else mods.splice(index, 1);
|
|
887
|
+
return replaceToken(target, mods.length ? [picker.prop, ...mods].join(':') : '');
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// What a picker currently shows for an element at a breakpoint.
|
|
891
|
+
export function currentPick(value, pickerName, bp, system) {
|
|
892
|
+
const picker = PICKERS[pickerName];
|
|
893
|
+
const group = tokenize(value).filter((t) => inGroup(t, bp));
|
|
894
|
+
if (picker.layout) {
|
|
895
|
+
const t = group.find((x) => LAYOUTS.includes(x.raw.split(':')[0]));
|
|
896
|
+
if (!t) return null;
|
|
897
|
+
if (pickerName === 'layout') return t.raw.split(':')[0];
|
|
898
|
+
if (pickerName === 'columns') {
|
|
899
|
+
const m = t.raw.split(':').find((x) => /^cols-\d+-fr$/.test(x));
|
|
900
|
+
return m ? m.split('-')[1] : null;
|
|
901
|
+
}
|
|
902
|
+
const [prop, ...mods] = t.raw.split(':');
|
|
903
|
+
return mods.find((m) => modifierType(system, prop, m) === picker.type) || null;
|
|
904
|
+
}
|
|
905
|
+
for (const t of group) {
|
|
906
|
+
const [prop, ...mods] = t.raw.split(':');
|
|
907
|
+
if (prop !== picker.prop) continue;
|
|
908
|
+
const m = mods.find((x) => (picker.prefix ? x.startsWith(picker.prefix) : modifierType(system, prop, x) === picker.type));
|
|
909
|
+
if (m) return picker.prefix ? m.slice(picker.prefix.length) : m;
|
|
910
|
+
}
|
|
911
|
+
return null;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// ════ Names ════
|
|
915
|
+
// What a person calls an element, in words, from its tag, its layout class
|
|
916
|
+
// and what's inside it. The tag itself only shows in "View code".
|
|
917
|
+
const TAG_NAMES = {
|
|
918
|
+
img: 'Image', picture: 'Image', video: 'Video', iframe: 'Embed', svg: 'Icon',
|
|
919
|
+
h1: 'Title', h2: 'Heading', h3: 'Small heading', h4: 'Smaller heading', h5: 'Smaller heading', h6: 'Smaller heading',
|
|
920
|
+
p: 'Paragraph', span: 'Text', small: 'Small text', strong: 'Bold text', em: 'Italic text', time: 'Date',
|
|
921
|
+
ul: 'List', ol: 'Numbered list', li: 'Item', dl: 'Details', dt: 'Term', dd: 'Description',
|
|
922
|
+
section: 'Section', article: 'Card', header: 'Header', footer: 'Footer', nav: 'Menu', main: 'Main area', aside: 'Side note',
|
|
923
|
+
figure: 'Figure', figcaption: 'Caption', blockquote: 'Quote', cite: 'Source', button: 'Button',
|
|
924
|
+
table: 'Table', thead: 'Table head', tbody: 'Table body', tr: 'Table row', th: 'Table heading', td: 'Table cell',
|
|
925
|
+
form: 'Form', label: 'Label', input: 'Input', hr: 'Divider', code: 'Code', pre: 'Code block', address: 'Address',
|
|
926
|
+
};
|
|
927
|
+
const LAYOUT_NAMES = { grid: 'Grid', row: 'Row', col: 'Column', stack: 'Stack' };
|
|
928
|
+
|
|
929
|
+
export function elementName(tag, classes = '', inner = '') {
|
|
930
|
+
const t = String(tag || '').toLowerCase();
|
|
931
|
+
if (/\{\{\s*list:/.test(inner)) return 'List of items';
|
|
932
|
+
if (/\{\{\s*each:/.test(inner)) return 'List of pages';
|
|
933
|
+
if (/\{\{\s*[\w.-]+:rich\b/.test(inner)) return 'Rich text';
|
|
934
|
+
if (/\{\{\s*[\w.-]+:blocks\b/.test(inner)) return 'Block area';
|
|
935
|
+
if (t === 'a') return /(^|\s)(bd|bg-color):/.test(classes) ? 'Button' : 'Link';
|
|
936
|
+
if (TAG_NAMES[t]) return TAG_NAMES[t];
|
|
937
|
+
// Layout from the base classes (before any breakpoint marker).
|
|
938
|
+
const base = tokenize(String(classes)).filter((x) => !x.marker && !x.bp);
|
|
939
|
+
const layout = base.map((x) => x.raw.split(':')[0]).find((p) => LAYOUT_NAMES[p]);
|
|
940
|
+
if (layout) return LAYOUT_NAMES[layout];
|
|
941
|
+
return 'Box';
|
|
942
|
+
}
|