verso-cms 0.0.0-stage → 0.1.3
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 +168 -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 +1195 -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
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// The site view: the pages as a tree. Moving a page moves its folder, so its
|
|
2
|
+
// address follows, and links to it are rewritten. Drafts stay in the repo and
|
|
3
|
+
// the editor but out of the live site.
|
|
4
|
+
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import { loadSite, readPage, writePage, dirForUrl, urlForDir, listPageDirs, slugify, PAGE_FILE } from '../site.js';
|
|
8
|
+
import { renderPage } from '../template.js';
|
|
9
|
+
import { imageSize } from '../images.js';
|
|
10
|
+
import { VersoError } from '../errors.js';
|
|
11
|
+
|
|
12
|
+
const parentUrl = (url) => {
|
|
13
|
+
const parts = url.split('/').filter(Boolean);
|
|
14
|
+
return parts.length ? `/${parts.slice(0, -1).join('/')}${parts.length > 1 ? '/' : ''}` : null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const bySiteOrder = (a, b) => {
|
|
18
|
+
const ao = Number.isFinite(a.order) ? a.order : Infinity;
|
|
19
|
+
const bo = Number.isFinite(b.order) ? b.order : Infinity;
|
|
20
|
+
if (ao !== bo) return ao - bo;
|
|
21
|
+
return (a.label || '').localeCompare(b.label || '');
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function siteTree(root) {
|
|
25
|
+
const site = loadSite(root);
|
|
26
|
+
const nodes = new Map(site.pages.map((p) => [p.url, {
|
|
27
|
+
url: p.url, label: p.label, title: p.title, template: p.template, draft: p.draft, order: p.order, date: p.date, children: [],
|
|
28
|
+
}]));
|
|
29
|
+
let top = nodes.get('/') || null;
|
|
30
|
+
nodes.forEach((node) => {
|
|
31
|
+
if (node.url === '/') return;
|
|
32
|
+
// Attach to the nearest page above it (a folder without page.json is skipped).
|
|
33
|
+
let up = parentUrl(node.url);
|
|
34
|
+
while (up && !nodes.has(up)) up = parentUrl(up);
|
|
35
|
+
const parent = up ? nodes.get(up) : top;
|
|
36
|
+
if (parent) parent.children.push(node);
|
|
37
|
+
});
|
|
38
|
+
const sort = (n) => { n.children.sort(bySiteOrder); n.children.forEach(sort); };
|
|
39
|
+
if (top) sort(top);
|
|
40
|
+
return top;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function setup(ctx) {
|
|
44
|
+
const { root } = ctx;
|
|
45
|
+
const rel = (p) => path.relative(root, p).split(path.sep).join('/');
|
|
46
|
+
const track = (file, data) => ctx.written.set(rel(file), ctx.hash(data));
|
|
47
|
+
const writeTracked = (file, text) => {
|
|
48
|
+
fs.writeFileSync(file, text, 'utf8');
|
|
49
|
+
track(file, Buffer.from(text, 'utf8'));
|
|
50
|
+
};
|
|
51
|
+
const savePage = (page) => {
|
|
52
|
+
const files = writePage(root, page);
|
|
53
|
+
Object.entries(files).forEach(([r, text]) => ctx.written.set(r, ctx.hash(Buffer.from(text, 'utf8'))));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Rewrites links that start with the old address, everywhere content lives.
|
|
57
|
+
const rewriteLinks = (from, to) => {
|
|
58
|
+
let count = 0;
|
|
59
|
+
const escaped = from.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
60
|
+
const href = new RegExp(`(href=")${escaped}`, 'g');
|
|
61
|
+
listPageDirs(root).forEach((dir) => {
|
|
62
|
+
const pageFile = path.join(dir, PAGE_FILE);
|
|
63
|
+
try {
|
|
64
|
+
const data = JSON.parse(fs.readFileSync(pageFile, 'utf8'));
|
|
65
|
+
let changed = false;
|
|
66
|
+
Object.entries(data.fields || {}).forEach(([k, v]) => {
|
|
67
|
+
if (typeof v === 'string' && (v === from || v.startsWith(from))) {
|
|
68
|
+
data.fields[k] = to + v.slice(from.length);
|
|
69
|
+
changed = true;
|
|
70
|
+
count += 1;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (changed) writeTracked(pageFile, JSON.stringify(data, null, 2) + '\n');
|
|
74
|
+
} catch {
|
|
75
|
+
// A page that can't be read is reported elsewhere.
|
|
76
|
+
}
|
|
77
|
+
fs.readdirSync(dir).filter((f) => f.endsWith('.html')).forEach((f) => {
|
|
78
|
+
const file = path.join(dir, f);
|
|
79
|
+
const html = fs.readFileSync(file, 'utf8');
|
|
80
|
+
let n = 0;
|
|
81
|
+
const next = html.replace(href, (m, a) => { n += 1; return `${a}${to}`; });
|
|
82
|
+
if (n) {
|
|
83
|
+
writeTracked(file, next);
|
|
84
|
+
count += n;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
// Templates are code, so they're only reported, not changed.
|
|
89
|
+
const templates = [];
|
|
90
|
+
const tdir = path.join(root, 'templates');
|
|
91
|
+
if (fs.existsSync(tdir)) {
|
|
92
|
+
fs.readdirSync(tdir).filter((f) => f.endsWith('.html')).forEach((f) => {
|
|
93
|
+
if (fs.readFileSync(path.join(tdir, f), 'utf8').includes(`href="${from}`)) templates.push(`templates/${f}`);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return { count, templates };
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Settings that name a folder follow it when it moves or is renamed: the
|
|
100
|
+
// channels in verso.json, and pages that list a folder's tags.
|
|
101
|
+
const followFolder = (fromUrl, toUrl) => {
|
|
102
|
+
const was = fromUrl.replace(/^\/+|\/+$/g, '');
|
|
103
|
+
const now = toUrl.replace(/^\/+|\/+$/g, '');
|
|
104
|
+
const moved = (folder) => (folder === was ? now : folder.startsWith(`${was}/`) ? `${now}${folder.slice(was.length)}` : null);
|
|
105
|
+
const configFile = path.join(root, 'verso.json');
|
|
106
|
+
try {
|
|
107
|
+
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
108
|
+
if (config.channels && typeof config.channels === 'object' && Object.keys(config.channels).some((k) => moved(k))) {
|
|
109
|
+
config.channels = Object.fromEntries(Object.entries(config.channels).map(([k, v]) => [moved(k) || k, v]));
|
|
110
|
+
writeTracked(configFile, JSON.stringify(config, null, 2) + '\n');
|
|
111
|
+
}
|
|
112
|
+
} catch { /* a verso.json that can't be read is reported elsewhere */ }
|
|
113
|
+
listPageDirs(root).forEach((dir) => {
|
|
114
|
+
const file = path.join(dir, PAGE_FILE);
|
|
115
|
+
try {
|
|
116
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
117
|
+
const next = typeof data['tags-of'] === 'string' ? moved(data['tags-of'].replace(/^\/+|\/+$/g, '')) : null;
|
|
118
|
+
if (next) { data['tags-of'] = next; writeTracked(file, JSON.stringify(data, null, 2) + '\n'); }
|
|
119
|
+
} catch { /* reported elsewhere */ }
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Lists name a folder too: {{ each:journal:newest }} in a page type, a block
|
|
124
|
+
// or a part. They follow a rename, the way class lists follow a token's.
|
|
125
|
+
const codeFiles = () => ['templates', 'blocks', 'parts'].flatMap((d) => {
|
|
126
|
+
const dir = path.join(root, d);
|
|
127
|
+
return fs.existsSync(dir) ? fs.readdirSync(dir).filter((f) => f.endsWith('.html')).map((f) => path.join(dir, f)) : [];
|
|
128
|
+
});
|
|
129
|
+
const listsOf = (folder) => new RegExp(`(\\{\\{\\s*(?:each|count):)${folder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?=[/:\\s}])`, 'g');
|
|
130
|
+
const followLists = (fromUrl, toUrl) => {
|
|
131
|
+
const was = fromUrl.replace(/^\/+|\/+$/g, '');
|
|
132
|
+
const now = toUrl.replace(/^\/+|\/+$/g, '');
|
|
133
|
+
const changed = [];
|
|
134
|
+
codeFiles().forEach((f) => {
|
|
135
|
+
const text = fs.readFileSync(f, 'utf8');
|
|
136
|
+
const next = text.replace(listsOf(was), (m, lead) => `${lead}${now}`);
|
|
137
|
+
if (next !== text) { writeTracked(f, next); changed.push(rel(f)); }
|
|
138
|
+
});
|
|
139
|
+
return changed;
|
|
140
|
+
};
|
|
141
|
+
const listedBy = (url) => {
|
|
142
|
+
const re = listsOf(url.replace(/^\/+|\/+$/g, ''));
|
|
143
|
+
return codeFiles().filter((f) => { re.lastIndex = 0; return re.test(fs.readFileSync(f, 'utf8')); }).map(rel);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Every file in a folder that's about to move or go is Verso's own change.
|
|
147
|
+
const letGo = (dir, movedTo = null) => {
|
|
148
|
+
const walk = (d) => fs.readdirSync(d, { withFileTypes: true }).forEach((e) => {
|
|
149
|
+
const f = path.join(d, e.name);
|
|
150
|
+
if (e.isDirectory()) { walk(f); if (!movedTo) ctx.written.set(rel(f), 'deleted'); }
|
|
151
|
+
else {
|
|
152
|
+
if (movedTo) ctx.written.set(rel(path.join(movedTo, path.relative(dir, f))), ctx.hash(fs.readFileSync(f)));
|
|
153
|
+
ctx.written.set(rel(f), 'deleted');
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
walk(dir);
|
|
157
|
+
if (!movedTo) ctx.written.set(rel(dir), 'deleted');
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// A page's address is its folder's name. Changing it renames the folder
|
|
161
|
+
// and fixes the links, channels and tag pages that pointed at it.
|
|
162
|
+
const rename = ({ url, name }) => {
|
|
163
|
+
url = String(url || '');
|
|
164
|
+
if (url === '/' || !url) throw new VersoError('The home page\'s address is the site\'s own, so it can\'t change.');
|
|
165
|
+
const dir = dirForUrl(root, url);
|
|
166
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
167
|
+
const slug = slugify(String(name || ''));
|
|
168
|
+
if (!slug) throw new VersoError('An address needs at least one letter or number, like opening-hours.');
|
|
169
|
+
const fromUrl = urlForDir(root, dir);
|
|
170
|
+
if (slug === path.basename(dir)) return { from: fromUrl, to: fromUrl, links: 0, templates: [] };
|
|
171
|
+
const newDir = path.join(path.dirname(dir), slug);
|
|
172
|
+
const toUrl = urlForDir(root, newDir);
|
|
173
|
+
if (fs.existsSync(newDir)) throw new VersoError(`There's already a page at ${toUrl}. Pick another address.`);
|
|
174
|
+
letGo(dir, newDir);
|
|
175
|
+
fs.renameSync(dir, newDir);
|
|
176
|
+
const links = rewriteLinks(fromUrl, toUrl);
|
|
177
|
+
followFolder(fromUrl, toUrl);
|
|
178
|
+
const lists = followLists(fromUrl, toUrl);
|
|
179
|
+
return { from: fromUrl, to: toUrl, links: links.count, templates: links.templates, lists };
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// Deleting a page deletes its folder: its words, pictures and the pages
|
|
183
|
+
// inside it. A published page can be brought back from History.
|
|
184
|
+
const removePage = ({ url, inside }) => {
|
|
185
|
+
url = String(url || '');
|
|
186
|
+
if (url === '/' || !url) throw new VersoError('The home page can\'t be deleted, or the site would have no front page.');
|
|
187
|
+
const dir = dirForUrl(root, url);
|
|
188
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
189
|
+
const page = readPage(root, dir);
|
|
190
|
+
const within = listPageDirs(root).filter((d) => d !== dir && d.startsWith(dir + path.sep)).length;
|
|
191
|
+
if (within && inside !== true) return { needsYes: true, within, label: page.label };
|
|
192
|
+
const fromUrl = urlForDir(root, dir);
|
|
193
|
+
// Pages that still link to it, so they can be told.
|
|
194
|
+
const linked = listPageDirs(root).filter((d) => d !== dir && !d.startsWith(dir + path.sep)).filter((d) => fs.readdirSync(d).filter((f) => f.endsWith('.html') || f === PAGE_FILE).some((f) => fs.readFileSync(path.join(d, f), 'utf8').includes(`"${fromUrl}`))).map((d) => readPage(root, d).label);
|
|
195
|
+
const lists = listedBy(fromUrl);
|
|
196
|
+
letGo(dir);
|
|
197
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
198
|
+
return { url: fromUrl, label: page.label, within, parent: urlForDir(root, path.dirname(dir)), linked, lists };
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const move = ({ from, toParent, index }) => {
|
|
202
|
+
from = String(from || '');
|
|
203
|
+
toParent = String(toParent || '');
|
|
204
|
+
if (from === '/' || !from) throw new VersoError('The home page is the top of the site, so it can\'t move.');
|
|
205
|
+
const fromDir = dirForUrl(root, from);
|
|
206
|
+
const parentDir = dirForUrl(root, toParent);
|
|
207
|
+
if (!fromDir || !fs.existsSync(path.join(fromDir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
208
|
+
if (!parentDir || !fs.existsSync(path.join(parentDir, PAGE_FILE))) throw new VersoError('The page you dropped it on no longer exists.');
|
|
209
|
+
const fromUrl = urlForDir(root, fromDir);
|
|
210
|
+
const parent = urlForDir(root, parentDir);
|
|
211
|
+
if (parent === fromUrl || parent.startsWith(fromUrl)) throw new VersoError('A page can\'t go inside itself or one of its own pages.');
|
|
212
|
+
|
|
213
|
+
const name = path.basename(fromDir);
|
|
214
|
+
const newDir = path.join(parentDir, name);
|
|
215
|
+
const toUrl = urlForDir(root, newDir);
|
|
216
|
+
let links = { count: 0, templates: [] };
|
|
217
|
+
if (newDir !== fromDir) {
|
|
218
|
+
if (fs.existsSync(newDir)) throw new VersoError(`There's already a page at ${toUrl}. Rename one of them first.`);
|
|
219
|
+
// Every file that moves is Verso's own change.
|
|
220
|
+
const walk = (d) => fs.readdirSync(d, { withFileTypes: true }).forEach((e) => {
|
|
221
|
+
const p = path.join(d, e.name);
|
|
222
|
+
if (e.isDirectory()) walk(p);
|
|
223
|
+
else {
|
|
224
|
+
const data = fs.readFileSync(p);
|
|
225
|
+
ctx.written.set(rel(path.join(newDir, path.relative(fromDir, p))), ctx.hash(data));
|
|
226
|
+
ctx.written.set(rel(p), 'deleted');
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
walk(fromDir);
|
|
230
|
+
fs.renameSync(fromDir, newDir);
|
|
231
|
+
links = rewriteLinks(fromUrl, toUrl);
|
|
232
|
+
followFolder(fromUrl, toUrl);
|
|
233
|
+
followLists(fromUrl, toUrl);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Number the siblings so the page lands where it was dropped.
|
|
237
|
+
const siblings = listPageDirs(root)
|
|
238
|
+
.filter((d) => d !== newDir && path.dirname(d) === parentDir)
|
|
239
|
+
.map((d) => readPage(root, d))
|
|
240
|
+
.sort(bySiteOrder);
|
|
241
|
+
const at = Math.max(0, Math.min(Number.isFinite(index) ? index : siblings.length, siblings.length));
|
|
242
|
+
siblings.splice(at, 0, readPage(root, newDir));
|
|
243
|
+
siblings.forEach((page, i) => {
|
|
244
|
+
if (page.order !== i) {
|
|
245
|
+
page.order = i;
|
|
246
|
+
savePage(page);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
return { from: fromUrl, to: toUrl, links: links.count, templates: links.templates };
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// A copy of a page beside the original: its words, pictures and blocks,
|
|
253
|
+
// not the pages inside it. It starts as a draft, so it's not live by surprise.
|
|
254
|
+
const duplicate = ({ url }) => {
|
|
255
|
+
url = String(url || '');
|
|
256
|
+
if (url === '/' || !url) throw new VersoError('The home page can\'t be copied. Make a new page and use the same blocks instead.');
|
|
257
|
+
const dir = dirForUrl(root, url);
|
|
258
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
259
|
+
const base = `${path.basename(dir)}-copy`;
|
|
260
|
+
let slug = base;
|
|
261
|
+
for (let n = 2; fs.existsSync(path.join(path.dirname(dir), slug)); n++) slug = `${base}-${n}`;
|
|
262
|
+
const newDir = path.join(path.dirname(dir), slug);
|
|
263
|
+
fs.mkdirSync(newDir);
|
|
264
|
+
fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile()).forEach((e) => {
|
|
265
|
+
const data = fs.readFileSync(path.join(dir, e.name));
|
|
266
|
+
fs.writeFileSync(path.join(newDir, e.name), data);
|
|
267
|
+
track(path.join(newDir, e.name), data);
|
|
268
|
+
});
|
|
269
|
+
const original = readPage(root, dir);
|
|
270
|
+
const page = readPage(root, newDir);
|
|
271
|
+
page.title = `${original.title || original.label} (copy)`;
|
|
272
|
+
page.draft = true;
|
|
273
|
+
savePage(page);
|
|
274
|
+
const siblings = listPageDirs(root).filter((d) => path.dirname(d) === path.dirname(dir) && d !== newDir).map((d) => readPage(root, d)).sort(bySiteOrder);
|
|
275
|
+
const at = siblings.findIndex((x) => x.dir === dir) + 1;
|
|
276
|
+
move({ from: urlForDir(root, newDir), toParent: urlForDir(root, path.dirname(dir)), index: at });
|
|
277
|
+
return { url: urlForDir(root, newDir), label: page.title };
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// A page's title, as the site view and the page list show it.
|
|
281
|
+
const retitle = ({ url, title }) => {
|
|
282
|
+
const dir = dirForUrl(root, String(url || ''));
|
|
283
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
284
|
+
const text = String(title || '').replace(/\s+/g, ' ').trim().slice(0, 300);
|
|
285
|
+
if (!text) throw new VersoError('A page needs a title. Give it at least a word.');
|
|
286
|
+
const page = readPage(root, dir);
|
|
287
|
+
page.title = text;
|
|
288
|
+
savePage(page);
|
|
289
|
+
return { url: page.url, title: text };
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const setDraft = ({ url, draft }) => {
|
|
293
|
+
const dir = dirForUrl(root, String(url || ''));
|
|
294
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
295
|
+
if (dir === path.join(root, 'content')) throw new VersoError('The home page can\'t be a draft, or the site would have no front page.');
|
|
296
|
+
const page = readPage(root, dir);
|
|
297
|
+
page.draft = draft === true;
|
|
298
|
+
savePage(page);
|
|
299
|
+
return { url: page.url, draft: page.draft };
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// A page as it ships, for the small previews in the site view.
|
|
303
|
+
const thumb = (url) => {
|
|
304
|
+
const site = loadSite(root);
|
|
305
|
+
const dir = dirForUrl(root, url);
|
|
306
|
+
const page = dir && site.pages.find((p) => p.dir === dir);
|
|
307
|
+
const template = page && site.templates.get(page.template);
|
|
308
|
+
if (!template) return null;
|
|
309
|
+
return renderPage(template, page, { mode: 'build', siteName: site.config.name, pages: site.pages, rich: site.config.rich, locale: site.config.locale, imageSize, blocks: site.blocks });
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const answer = async (res, fn) => {
|
|
313
|
+
try {
|
|
314
|
+
ctx.sendJSON(res, 200, { ok: true, ...(await fn()) });
|
|
315
|
+
} catch (err) {
|
|
316
|
+
if (!(err instanceof VersoError)) throw err;
|
|
317
|
+
ctx.sendJSON(res, 400, { ok: false, message: err.message });
|
|
318
|
+
}
|
|
319
|
+
return true;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
async handle(req, res, url) {
|
|
324
|
+
const p = url.pathname;
|
|
325
|
+
if (p === '/__verso/site-tree' && req.method === 'GET') return answer(res, () => ({ tree: siteTree(root) }));
|
|
326
|
+
if (p === '/__verso/thumb' && req.method === 'GET') {
|
|
327
|
+
const html = thumb(url.searchParams.get('url') || '/');
|
|
328
|
+
if (html) ctx.send(res, 200, html);
|
|
329
|
+
else ctx.send(res, 404, 'Not found', 'text/plain');
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
if (p === '/__verso/move' && req.method === 'POST') {
|
|
333
|
+
return answer(res, async () => {
|
|
334
|
+
const out = move(await ctx.readBody(req));
|
|
335
|
+
ctx.broadcast({ type: 'pages' });
|
|
336
|
+
ctx.broadcast({ type: 'reload' });
|
|
337
|
+
return out;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (p === '/__verso/page/address' && req.method === 'POST') {
|
|
341
|
+
return answer(res, async () => {
|
|
342
|
+
const out = rename(await ctx.readBody(req));
|
|
343
|
+
ctx.broadcast({ type: 'pages' });
|
|
344
|
+
return out;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
if (p === '/__verso/page/delete' && req.method === 'POST') {
|
|
348
|
+
return answer(res, async () => {
|
|
349
|
+
const out = removePage(await ctx.readBody(req));
|
|
350
|
+
if (!out.needsYes) { ctx.broadcast({ type: 'pages' }); ctx.broadcast({ type: 'status' }); }
|
|
351
|
+
return out;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
if (p === '/__verso/page/duplicate' && req.method === 'POST') {
|
|
355
|
+
return answer(res, async () => {
|
|
356
|
+
const out = duplicate(await ctx.readBody(req));
|
|
357
|
+
ctx.broadcast({ type: 'pages' });
|
|
358
|
+
ctx.broadcast({ type: 'status' });
|
|
359
|
+
return out;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
if (p === '/__verso/page/title' && req.method === 'POST') {
|
|
363
|
+
return answer(res, async () => {
|
|
364
|
+
const out = retitle(await ctx.readBody(req));
|
|
365
|
+
ctx.broadcast({ type: 'pages' });
|
|
366
|
+
ctx.broadcast({ type: 'status' });
|
|
367
|
+
return out;
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
if (p === '/__verso/draft' && req.method === 'POST') {
|
|
371
|
+
return answer(res, async () => {
|
|
372
|
+
const out = setDraft(await ctx.readBody(req));
|
|
373
|
+
ctx.broadcast({ type: 'pages' });
|
|
374
|
+
ctx.broadcast({ type: 'status' });
|
|
375
|
+
return out;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return false;
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Tags: the editor's Tags control suggests the tags already used on the
|
|
2
|
+
// site, so the same tag isn't spelled two ways.
|
|
3
|
+
|
|
4
|
+
export async function setup() {
|
|
5
|
+
return {
|
|
6
|
+
editorData(site) {
|
|
7
|
+
const seen = new Map();
|
|
8
|
+
site.pages.forEach((p) => (p.tags || []).forEach((t) => {
|
|
9
|
+
const key = t.toLowerCase();
|
|
10
|
+
seen.set(key, { tag: seen.get(key)?.tag || t, count: (seen.get(key)?.count || 0) + 1 });
|
|
11
|
+
}));
|
|
12
|
+
return { siteTags: [...seen.values()].sort((a, b) => b.count - a.count || a.tag.localeCompare(b.tag)).map((x) => x.tag).slice(0, 200) };
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|