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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The command palette's index: every page, block and component, in one
|
|
2
|
+
// request, so the palette can filter as you type without waiting.
|
|
3
|
+
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { loadSite } from '../site.js';
|
|
7
|
+
import { humanize } from '../template.js';
|
|
8
|
+
|
|
9
|
+
const names = (dir, ext) =>
|
|
10
|
+
fs.existsSync(dir) ? fs.readdirSync(dir).filter((f) => f.endsWith(ext)).map((f) => f.slice(0, -ext.length)).sort() : [];
|
|
11
|
+
|
|
12
|
+
export async function setup(ctx) {
|
|
13
|
+
const index = () => {
|
|
14
|
+
const site = loadSite(ctx.root);
|
|
15
|
+
return {
|
|
16
|
+
pages: site.pages.map((p) => ({ url: p.url, label: p.label, title: p.title, draft: !!p.draft })),
|
|
17
|
+
blocks: names(path.join(ctx.root, 'blocks'), '.html').map((name) => ({ name, label: humanize(name) })),
|
|
18
|
+
components: names(path.join(ctx.root, 'components'), '.json').map((name) => ({ name, label: humanize(name) })),
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
async handle(req, res, url) {
|
|
23
|
+
if (req.method !== 'GET') return false;
|
|
24
|
+
if (url.pathname === '/__verso/palette' || url.pathname === '/__verso/site-blocks') {
|
|
25
|
+
ctx.sendJSON(res, 200, index());
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// People: who can edit a site that's running as a box (verso serve), and
|
|
2
|
+
// who's editing right now. Without a box there's nobody to manage, so this
|
|
3
|
+
// does nothing.
|
|
4
|
+
|
|
5
|
+
import { VersoError } from '../errors.js';
|
|
6
|
+
import { ROLES } from '../people.js';
|
|
7
|
+
|
|
8
|
+
const HERE_FOR = 45 * 1000; // how long after its last word a tab still counts as open
|
|
9
|
+
|
|
10
|
+
export async function setup(ctx) {
|
|
11
|
+
const { box, readBody } = ctx;
|
|
12
|
+
if (!box) return {};
|
|
13
|
+
// Answer, and say the request was this extension's.
|
|
14
|
+
const sendJSON = (res, status, body) => { ctx.sendJSON(res, status, body); return true; };
|
|
15
|
+
const { people } = box;
|
|
16
|
+
|
|
17
|
+
// Open editor tabs, by the id each tab makes up for itself.
|
|
18
|
+
const here = new Map();
|
|
19
|
+
const others = (tab, url) => {
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
const seen = new Map();
|
|
22
|
+
here.forEach((h, id) => {
|
|
23
|
+
if (now - h.at > HERE_FOR) { here.delete(id); return; }
|
|
24
|
+
if (id === tab) return;
|
|
25
|
+
// One person in three tabs is still one person; this page counts most.
|
|
26
|
+
const before = seen.get(h.id);
|
|
27
|
+
if (!before || (h.url === url && before.url !== url)) seen.set(h.id, h);
|
|
28
|
+
});
|
|
29
|
+
return [...seen.values()].map((h) => ({ id: h.id, name: h.name, initials: h.initials, url: h.url, label: h.label, same: h.url === url }));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const answer = async (res, work) => {
|
|
33
|
+
try {
|
|
34
|
+
return sendJSON(res, 200, { ok: true, ...(await work()) });
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (!(err instanceof VersoError)) throw err;
|
|
37
|
+
return sendJSON(res, 400, { ok: false, message: err.message });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const ownersOnly = (req, res) => {
|
|
41
|
+
if (req.me.role === 'owner') return false;
|
|
42
|
+
sendJSON(res, 403, { ok: false, message: 'Only an owner of this site can see who edits it.' });
|
|
43
|
+
return true;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
async handle(req, res, url) {
|
|
48
|
+
const p = url.pathname;
|
|
49
|
+
if (!p.startsWith('/__verso/people')) return false;
|
|
50
|
+
const me = req.me;
|
|
51
|
+
const tab = String(req.headers['x-verso-tab'] || '');
|
|
52
|
+
|
|
53
|
+
// A tab says where it is every so often, and hears who else is about.
|
|
54
|
+
if (p === '/__verso/people/here' && req.method === 'POST') {
|
|
55
|
+
const body = await readBody(req);
|
|
56
|
+
if (tab) {
|
|
57
|
+
if (body.gone) here.delete(tab);
|
|
58
|
+
else here.set(tab, { id: me.id, name: me.name, initials: me.initials, url: String(body.url || '/').slice(0, 300), label: String(body.label || '').slice(0, 120), at: Date.now() });
|
|
59
|
+
}
|
|
60
|
+
return sendJSON(res, 200, { ok: true, others: others(tab, String(body.url || '/')) });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (p === '/__verso/people' && req.method === 'GET') {
|
|
64
|
+
if (ownersOnly(req, res)) return true;
|
|
65
|
+
const now = others('', '');
|
|
66
|
+
const list = people.list();
|
|
67
|
+
return sendJSON(res, 200, {
|
|
68
|
+
ok: true,
|
|
69
|
+
me: me.id,
|
|
70
|
+
roles: ROLES,
|
|
71
|
+
people: list.people.map((person) => ({ ...person, here: now.find((o) => o.id === person.id)?.label || (person.id === me.id ? 'you' : '') })),
|
|
72
|
+
waiting: list.waiting,
|
|
73
|
+
box: await box.status(),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (p === '/__verso/people/password' && req.method === 'POST') {
|
|
78
|
+
return answer(res, async () => {
|
|
79
|
+
const body = await readBody(req);
|
|
80
|
+
const person = await people.changePassword(me.id, String(body.current || ''), String(body.next || ''));
|
|
81
|
+
// Changing it signs every browser out; this one gets a new pass.
|
|
82
|
+
res.setHeader('Set-Cookie', box.cookie(req, person));
|
|
83
|
+
return {};
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (req.method !== 'POST') return false;
|
|
88
|
+
const body = await readBody(req);
|
|
89
|
+
if (p === '/__verso/people/invite') {
|
|
90
|
+
return answer(res, () => {
|
|
91
|
+
const made = people.invite({ name: body.name, email: body.email, role: body.role }, me);
|
|
92
|
+
return { ...made, link: `${box.address(req)}${made.link}`, days: 7 };
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (p === '/__verso/people/link') {
|
|
96
|
+
return answer(res, () => {
|
|
97
|
+
const made = people.resetLink(String(body.id || ''), me);
|
|
98
|
+
return { ...made, link: `${box.address(req)}${made.link}`, days: 7 };
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (p === '/__verso/people/role') return answer(res, () => ({ person: people.setRole(String(body.id || ''), String(body.role || '')) }));
|
|
102
|
+
if (p === '/__verso/people/remove') {
|
|
103
|
+
return answer(res, () => {
|
|
104
|
+
if (String(body.id) === me.id) throw new VersoError('You can\'t remove yourself. Ask another owner to.');
|
|
105
|
+
const gone = people.remove(String(body.id || ''));
|
|
106
|
+
return { name: gone?.name || '' };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (p === '/__verso/people/cancel') return answer(res, () => ({ cancelled: people.cancelLink(String(body.id || '')) }));
|
|
110
|
+
return false;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Site settings and page types, set up in the editor instead of by hand.
|
|
2
|
+
//
|
|
3
|
+
// Site settings: the name, address, sub-folder, language and search index,
|
|
4
|
+
// kept in verso.json. Every other key stays as it was, in the same order.
|
|
5
|
+
//
|
|
6
|
+
// Page types: a new type starts as a copy of an existing template. Any page
|
|
7
|
+
// can become the starting layout for new pages of its type, kept beside the
|
|
8
|
+
// template as templates/<type>.layout.json: { "blocks": …, "fields": … }.
|
|
9
|
+
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import { listTemplates, listPageDirs, readPage, dirForUrl, slugify, PAGE_FILE, readSiteData, writeSiteData } from '../site.js';
|
|
13
|
+
import { humanize } from '../template.js';
|
|
14
|
+
import { VersoError } from '../errors.js';
|
|
15
|
+
|
|
16
|
+
const isObject = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
17
|
+
|
|
18
|
+
// The folders that hold pages, for "Lists the tags of" and channels:
|
|
19
|
+
// { key: "journal", label: "Journal", url: "/journal/", pages: 4 }.
|
|
20
|
+
export function listFolders(root) {
|
|
21
|
+
const content = path.join(root, 'content');
|
|
22
|
+
return listPageDirs(root)
|
|
23
|
+
.filter((dir) => dir !== content)
|
|
24
|
+
.map((dir) => {
|
|
25
|
+
const key = path.relative(content, dir).split(path.sep).join('/');
|
|
26
|
+
let label = humanize(key.split('/').pop());
|
|
27
|
+
try { label = readPage(root, dir).label || label; } catch { /* reported elsewhere */ }
|
|
28
|
+
const pages = fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isDirectory() && fs.existsSync(path.join(dir, e.name, PAGE_FILE))).length;
|
|
29
|
+
return { key, label, url: `/${key}/`, pages };
|
|
30
|
+
})
|
|
31
|
+
.sort((a, b) => a.key.localeCompare(b.key));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The page types that have a starting layout.
|
|
35
|
+
export const listStarters = (root) => listTemplates(root).filter((name) => fs.existsSync(path.join(root, 'templates', `${name}.layout.json`)));
|
|
36
|
+
|
|
37
|
+
// "new pages", "new project pages", "new landing pages": never "Page pages".
|
|
38
|
+
export function newPagesOf(label) {
|
|
39
|
+
const l = String(label || '').trim().toLowerCase();
|
|
40
|
+
if (!l || l === 'page') return 'new pages';
|
|
41
|
+
if (l.endsWith('page')) return `new ${l}s`;
|
|
42
|
+
return `new ${l} pages`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function setup(ctx) {
|
|
46
|
+
const { root } = ctx;
|
|
47
|
+
const configFile = path.join(root, 'verso.json');
|
|
48
|
+
const note = (file, text) => ctx.written.set(ctx.relPath(file), ctx.hash(Buffer.from(text, 'utf8')));
|
|
49
|
+
|
|
50
|
+
const readConfigRaw = () => {
|
|
51
|
+
try {
|
|
52
|
+
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
53
|
+
if (!isObject(config)) throw new Error();
|
|
54
|
+
return config;
|
|
55
|
+
} catch {
|
|
56
|
+
throw new VersoError('verso.json can\'t be read right now, so the settings can\'t change. Fix it by hand, or ask a developer.');
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const writeConfig = (config) => {
|
|
60
|
+
const text = `${JSON.stringify(config, null, 2)}\n`;
|
|
61
|
+
note(configFile, text);
|
|
62
|
+
fs.writeFileSync(configFile, text, 'utf8');
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const settings = () => {
|
|
66
|
+
const c = readConfigRaw();
|
|
67
|
+
return {
|
|
68
|
+
settings: {
|
|
69
|
+
name: typeof c.name === 'string' ? c.name : '',
|
|
70
|
+
url: typeof c.url === 'string' ? c.url : '',
|
|
71
|
+
base: typeof c.base === 'string' ? c.base : '',
|
|
72
|
+
locale: typeof c.locale === 'string' ? c.locale : 'en-US',
|
|
73
|
+
index: c.index === true,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const saveSettings = (body) => {
|
|
79
|
+
const config = readConfigRaw();
|
|
80
|
+
const name = String(body.name ?? '').trim();
|
|
81
|
+
if (!name) throw new VersoError('Give the site a name. It\'s used in page titles and feeds.');
|
|
82
|
+
const url = String(body.url ?? '').trim().replace(/\/+$/, '');
|
|
83
|
+
if (url && !/^https?:\/\/[^\s/]+\.[^\s/]+/.test(url)) {
|
|
84
|
+
throw new VersoError('The address should be the whole thing, starting with https://, like https://example.com.');
|
|
85
|
+
}
|
|
86
|
+
if (url && /^https?:\/\/[^/]+\/./.test(url)) {
|
|
87
|
+
throw new VersoError('Leave the folder out of the address and put it under Sub-folder instead, like /blog.');
|
|
88
|
+
}
|
|
89
|
+
let base = String(body.base ?? '').trim().replace(/\/+$/, '');
|
|
90
|
+
if (base && !base.startsWith('/')) base = `/${base}`;
|
|
91
|
+
if (base && !/^(\/[a-z0-9][a-z0-9._-]*)+$/i.test(base)) {
|
|
92
|
+
throw new VersoError('The sub-folder should look like /blog: a slash, then letters, numbers, dashes or dots.');
|
|
93
|
+
}
|
|
94
|
+
const locale = String(body.locale ?? '').trim() || 'en-US';
|
|
95
|
+
try {
|
|
96
|
+
new Intl.DateTimeFormat(locale);
|
|
97
|
+
} catch {
|
|
98
|
+
throw new VersoError(`"${locale}" isn't a language code. Use something like en-US, en-GB, fr-FR or de-DE.`);
|
|
99
|
+
}
|
|
100
|
+
const before = typeof config.name === 'string' ? config.name.trim() : '';
|
|
101
|
+
// Change the keys in place, so every other key keeps its spot.
|
|
102
|
+
config.name = name;
|
|
103
|
+
if (url) config.url = url; else delete config.url;
|
|
104
|
+
if (base) config.base = base; else delete config.base;
|
|
105
|
+
config.locale = locale;
|
|
106
|
+
if (body.index) config.index = true; else delete config.index;
|
|
107
|
+
writeConfig(config);
|
|
108
|
+
ctx.broadcast({ type: 'status' });
|
|
109
|
+
// The name is often also written in the shared header or footer: offer
|
|
110
|
+
// to change it there too.
|
|
111
|
+
const shared = before && before !== name ? sharedText(before).length : 0;
|
|
112
|
+
return { ...settings(), renamed: shared ? { from: before, to: name, count: shared } : null };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Text in the shared header and footer (content/site.json) that's exactly
|
|
116
|
+
// the given words: [{ list, path }], or with to, changed to it.
|
|
117
|
+
const sharedText = (from, to = null) => {
|
|
118
|
+
const data = readSiteData(root);
|
|
119
|
+
const hits = [];
|
|
120
|
+
const visit = (list) => (Array.isArray(list) ? list : []).forEach((inst) => {
|
|
121
|
+
Object.entries(inst.fields || {}).forEach(([k, v]) => {
|
|
122
|
+
if (typeof v === 'string' && v.trim() === from) { hits.push(k); if (to != null) inst.fields[k] = to; }
|
|
123
|
+
});
|
|
124
|
+
Object.values(inst.blocks || {}).forEach(visit);
|
|
125
|
+
});
|
|
126
|
+
Object.values(data.blocks || {}).forEach(visit);
|
|
127
|
+
Object.entries(data.fields || {}).forEach(([k, v]) => {
|
|
128
|
+
if (typeof v === 'string' && v.trim() === from) { hits.push(k); if (to != null) data.fields[k] = to; }
|
|
129
|
+
});
|
|
130
|
+
if (to != null && hits.length) writeSiteData(root, data);
|
|
131
|
+
return hits;
|
|
132
|
+
};
|
|
133
|
+
const renameShared = (body) => {
|
|
134
|
+
const from = String(body.from || '').trim();
|
|
135
|
+
const to = String(body.to || '').trim();
|
|
136
|
+
if (!from || !to) throw new VersoError('Say which words to change, and to what.');
|
|
137
|
+
const changed = sharedText(from, to).length;
|
|
138
|
+
ctx.broadcast({ type: 'status' });
|
|
139
|
+
return { changed };
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// ════ Page types ════
|
|
143
|
+
|
|
144
|
+
const newType = (body) => {
|
|
145
|
+
const label = String(body.name || '').trim();
|
|
146
|
+
const name = slugify(label);
|
|
147
|
+
if (!name) throw new VersoError('Give the page type a name with at least one letter or number, like Case study.');
|
|
148
|
+
const templates = listTemplates(root);
|
|
149
|
+
if (templates.includes(name)) throw new VersoError(`There's already a page type called ${humanize(name)}. Choose another name.`);
|
|
150
|
+
const from = String(body.from || '');
|
|
151
|
+
if (!templates.includes(from)) throw new VersoError('Choose a page type to start from.');
|
|
152
|
+
const source = fs.readFileSync(path.join(root, 'templates', `${from}.html`), 'utf8');
|
|
153
|
+
const file = path.join(root, 'templates', `${name}.html`);
|
|
154
|
+
note(file, source);
|
|
155
|
+
fs.writeFileSync(file, source, 'utf8');
|
|
156
|
+
// Its starting layout comes along too, if the original has one.
|
|
157
|
+
const starter = path.join(root, 'templates', `${from}.layout.json`);
|
|
158
|
+
if (fs.existsSync(starter)) {
|
|
159
|
+
const text = fs.readFileSync(starter, 'utf8');
|
|
160
|
+
const target = path.join(root, 'templates', `${name}.layout.json`);
|
|
161
|
+
note(target, text);
|
|
162
|
+
fs.writeFileSync(target, text, 'utf8');
|
|
163
|
+
}
|
|
164
|
+
ctx.broadcast({ type: 'status' });
|
|
165
|
+
return { name, label: humanize(name), starter: fs.existsSync(path.join(root, 'templates', `${name}.layout.json`)) };
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// { url } makes that page the starting layout for its type; { url, clear: true } removes it.
|
|
169
|
+
const starter = (body) => {
|
|
170
|
+
const dir = dirForUrl(root, String(body.url || ''));
|
|
171
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
172
|
+
const page = readPage(root, dir);
|
|
173
|
+
const file = path.join(root, 'templates', `${page.template}.layout.json`);
|
|
174
|
+
const type = humanize(page.template);
|
|
175
|
+
const pages = newPagesOf(type);
|
|
176
|
+
const Pages = pages[0].toUpperCase() + pages.slice(1);
|
|
177
|
+
const previous = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : null;
|
|
178
|
+
const drop = () => {
|
|
179
|
+
if (!fs.existsSync(file)) return;
|
|
180
|
+
ctx.written.set(ctx.relPath(file), 'deleted');
|
|
181
|
+
fs.rmSync(file);
|
|
182
|
+
};
|
|
183
|
+
// Undo: put back what was there before, a layout's text or nothing.
|
|
184
|
+
if ('restore' in body) {
|
|
185
|
+
if (body.restore === null) {
|
|
186
|
+
drop();
|
|
187
|
+
ctx.broadcast({ type: 'status' });
|
|
188
|
+
return { message: `${Pages} start empty again.` };
|
|
189
|
+
}
|
|
190
|
+
const text = String(body.restore);
|
|
191
|
+
try { JSON.parse(text); } catch { throw new VersoError('That layout can\'t be put back, because it isn\'t a layout Verso wrote.'); }
|
|
192
|
+
note(file, text);
|
|
193
|
+
fs.writeFileSync(file, text, 'utf8');
|
|
194
|
+
ctx.broadcast({ type: 'status' });
|
|
195
|
+
return { message: `${Pages} start with the layout they had before.` };
|
|
196
|
+
}
|
|
197
|
+
if (body.clear) {
|
|
198
|
+
drop();
|
|
199
|
+
ctx.broadcast({ type: 'status' });
|
|
200
|
+
return { message: `${Pages} start empty again.`, previous };
|
|
201
|
+
}
|
|
202
|
+
if (!fs.existsSync(path.join(root, 'templates', `${page.template}.html`))) throw new VersoError(`The page type ${type} has no template file, so it can't have a starting layout.`);
|
|
203
|
+
// Site slots ("site.header") are shared by every page, so they stay out.
|
|
204
|
+
// A layout is the blocks and their choices, not this page's words: text
|
|
205
|
+
// is left empty, so every field shows its label on a new page, and only
|
|
206
|
+
// colour choices and shared images ("/logo.png") come along.
|
|
207
|
+
const keep = (v) => (v && typeof v === 'object' && (typeof v.color === 'string' || (typeof v.file === 'string' && v.file.startsWith('/'))));
|
|
208
|
+
const bare = (fields) => Object.fromEntries(Object.entries(fields || {}).filter(([, v]) => keep(v)));
|
|
209
|
+
const shape = (inst) => ({ ...inst, fields: bare(inst.fields), ...(inst.blocks ? { blocks: Object.fromEntries(Object.entries(inst.blocks).map(([k, list]) => [k, (list || []).map(shape)])) } : {}) });
|
|
210
|
+
const blocks = Object.fromEntries(Object.entries(page.blocks || {}).filter(([slot]) => !slot.startsWith('site.')).map(([slot, list]) => [slot, (list || []).map(shape)]));
|
|
211
|
+
const text = `${JSON.stringify({ blocks, fields: bare(page.fields) }, null, 2)}\n`;
|
|
212
|
+
note(file, text);
|
|
213
|
+
fs.writeFileSync(file, text, 'utf8');
|
|
214
|
+
ctx.broadcast({ type: 'status' });
|
|
215
|
+
return { message: `${Pages} will start like this one.`, previous };
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const answer = async (res, fn) => {
|
|
219
|
+
try {
|
|
220
|
+
ctx.sendJSON(res, 200, { ok: true, ...(await fn()) });
|
|
221
|
+
} catch (err) {
|
|
222
|
+
if (!(err instanceof VersoError)) throw err;
|
|
223
|
+
ctx.sendJSON(res, 400, { ok: false, message: err.message });
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
async handle(req, res, url) {
|
|
230
|
+
const p = url.pathname;
|
|
231
|
+
if (p === '/__verso/site-settings' && req.method === 'GET') return answer(res, settings);
|
|
232
|
+
if (req.method !== 'POST') return false;
|
|
233
|
+
const action = {
|
|
234
|
+
'/__verso/site-settings': saveSettings,
|
|
235
|
+
'/__verso/site-settings/rename-shared': renameShared,
|
|
236
|
+
'/__verso/page-types/new': newType,
|
|
237
|
+
'/__verso/page-types/starter': starter,
|
|
238
|
+
}[p];
|
|
239
|
+
if (!action) return false;
|
|
240
|
+
return answer(res, async () => action(await ctx.readBody(req)));
|
|
241
|
+
},
|
|
242
|
+
editorData(site, page) {
|
|
243
|
+
const starters = listStarters(root);
|
|
244
|
+
return {
|
|
245
|
+
folders: listFolders(root),
|
|
246
|
+
starters,
|
|
247
|
+
hasStarter: !!page && starters.includes(page.template),
|
|
248
|
+
};
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|