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/fonts.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// What a font file can vary: its axes (weight, optical size…), read from the
|
|
2
|
+
// file's "fvar" table. A font with a weight axis is one file for every
|
|
3
|
+
// weight, so its @font-face rule gives a range instead of one number.
|
|
4
|
+
|
|
5
|
+
import zlib from 'zlib';
|
|
6
|
+
|
|
7
|
+
// The table tags WOFF2 knows by number.
|
|
8
|
+
const WOFF2_TAGS = ['cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', 'post', 'cvt ', 'fpgm', 'glyf', 'loca', 'prep', 'CFF ', 'VORG', 'EBDT', 'EBLC', 'gasp', 'hdmx', 'kern', 'LTSH', 'PCLT', 'VDMX', 'vhea', 'vmtx', 'BASE', 'GDEF', 'GPOS', 'GSUB', 'EBSC', 'JSTF', 'MATH', 'CBDT', 'CBLC', 'COLR', 'CPAL', 'SVG ', 'sbix', 'acnt', 'avar', 'bdat', 'bloc', 'bsln', 'cvar', 'fdsc', 'feat', 'fmtx', 'fvar', 'gvar', 'hsty', 'just', 'lcar', 'mort', 'morx', 'opbd', 'prop', 'trak', 'Zapf', 'Silf', 'Glat', 'Gloc', 'Feat', 'Sill'];
|
|
9
|
+
|
|
10
|
+
// Reads whole tables out of a font file, by tag: { name: Buffer, 'OS/2': Buffer }.
|
|
11
|
+
function tablesFromSfnt(b, want) {
|
|
12
|
+
const out = {};
|
|
13
|
+
const count = b.readUInt16BE(4);
|
|
14
|
+
for (let i = 0; i < count; i += 1) {
|
|
15
|
+
const at = 12 + i * 16;
|
|
16
|
+
const tag = b.slice(at, at + 4).toString('latin1');
|
|
17
|
+
if (want.includes(tag)) out[tag] = b.slice(b.readUInt32BE(at + 8), b.readUInt32BE(at + 8) + b.readUInt32BE(at + 12));
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function tablesFromWoff(b, want) {
|
|
23
|
+
const out = {};
|
|
24
|
+
const count = b.readUInt16BE(12);
|
|
25
|
+
for (let i = 0; i < count; i += 1) {
|
|
26
|
+
const at = 44 + i * 20;
|
|
27
|
+
const tag = b.slice(at, at + 4).toString('latin1');
|
|
28
|
+
if (!want.includes(tag)) continue;
|
|
29
|
+
const data = b.slice(b.readUInt32BE(at + 4), b.readUInt32BE(at + 4) + b.readUInt32BE(at + 8));
|
|
30
|
+
out[tag] = b.readUInt32BE(at + 8) === b.readUInt32BE(at + 12) ? data : zlib.inflateSync(data);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function tablesFromWoff2(b, want) {
|
|
36
|
+
if (b.slice(4, 8).toString('latin1') === 'ttcf') return {}; // collections aren't read
|
|
37
|
+
const count = b.readUInt16BE(12);
|
|
38
|
+
const compressed = b.readUInt32BE(20);
|
|
39
|
+
let at = 48;
|
|
40
|
+
const base128 = () => {
|
|
41
|
+
let n = 0;
|
|
42
|
+
for (let i = 0; i < 5; i += 1) {
|
|
43
|
+
const byte = b[at]; at += 1;
|
|
44
|
+
n = (n * 128) + (byte & 0x7f);
|
|
45
|
+
if (!(byte & 0x80)) break;
|
|
46
|
+
}
|
|
47
|
+
return n;
|
|
48
|
+
};
|
|
49
|
+
let offset = 0;
|
|
50
|
+
const found = {};
|
|
51
|
+
for (let i = 0; i < count; i += 1) {
|
|
52
|
+
const flags = b[at]; at += 1;
|
|
53
|
+
let tag = WOFF2_TAGS[flags & 0x3f];
|
|
54
|
+
if ((flags & 0x3f) === 0x3f) { tag = b.slice(at, at + 4).toString('latin1'); at += 4; }
|
|
55
|
+
const version = flags >> 6;
|
|
56
|
+
const orig = base128();
|
|
57
|
+
// glyf and loca are transformed at version 0; other tables at any other.
|
|
58
|
+
const transformed = tag === 'glyf' || tag === 'loca' ? version === 0 : version !== 0;
|
|
59
|
+
const length = transformed ? base128() : orig;
|
|
60
|
+
if (want.includes(tag)) found[tag] = { offset, length };
|
|
61
|
+
offset += length;
|
|
62
|
+
}
|
|
63
|
+
if (!Object.keys(found).length) return {};
|
|
64
|
+
const all = zlib.brotliDecompressSync(b.slice(at, at + compressed));
|
|
65
|
+
return Object.fromEntries(Object.entries(found).map(([tag, f]) => [tag, all.slice(f.offset, f.offset + f.length)]));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const tables = (buffer, kind, want) => (kind === 'woff2' ? tablesFromWoff2 : kind === 'woff' ? tablesFromWoff : tablesFromSfnt)(buffer, want);
|
|
69
|
+
|
|
70
|
+
// The family name a font file gives itself (the typographic family if it
|
|
71
|
+
// has one), from its "name" table.
|
|
72
|
+
function familyName(name) {
|
|
73
|
+
if (!name || name.length < 6) return null;
|
|
74
|
+
const count = name.readUInt16BE(2);
|
|
75
|
+
const strings = name.readUInt16BE(4);
|
|
76
|
+
const found = {};
|
|
77
|
+
for (let i = 0; i < count; i += 1) {
|
|
78
|
+
const at = 6 + i * 12;
|
|
79
|
+
const platform = name.readUInt16BE(at);
|
|
80
|
+
const id = name.readUInt16BE(at + 6);
|
|
81
|
+
if (id !== 1 && id !== 16) continue;
|
|
82
|
+
const raw = name.slice(strings + name.readUInt16BE(at + 10), strings + name.readUInt16BE(at + 10) + name.readUInt16BE(at + 8));
|
|
83
|
+
const text = platform === 1 ? raw.toString('latin1') : Buffer.from(raw).swap16().toString('utf16le');
|
|
84
|
+
// Windows names win over Mac ones; either beats nothing.
|
|
85
|
+
if (!found[id] || platform === 3) found[id] = text.replace(/\0/g, '').trim();
|
|
86
|
+
}
|
|
87
|
+
// Without a typographic family, the plain one may end in its weight
|
|
88
|
+
// ("Plex Mono Medium"), which belongs to the weight, not the name.
|
|
89
|
+
return found[16] || (found[1] || '').replace(/(\s+(Thin|Hairline|Extra ?Light|Ultra ?Light|Light|Regular|Book|Medium|Semi ?Bold|Demi ?Bold|Bold|Extra ?Bold|Ultra ?Bold|Black|Heavy|Italic|Oblique))+$/i, '') || null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// What a font file says about itself: its family name, weight, whether it's
|
|
93
|
+
// italic, and what kind of face to fall back to. Anything it doesn't say is
|
|
94
|
+
// left out.
|
|
95
|
+
export function fontFacts(buffer, kind) {
|
|
96
|
+
try {
|
|
97
|
+
const t = tables(buffer, kind, ['name', 'OS/2', 'post', 'head']);
|
|
98
|
+
const os2 = t['OS/2'];
|
|
99
|
+
const facts = {};
|
|
100
|
+
const family = familyName(t.name);
|
|
101
|
+
if (family && /^[\w .'-]{1,60}$/.test(family)) facts.family = family;
|
|
102
|
+
if (os2 && os2.length >= 6) {
|
|
103
|
+
const w = Math.round(os2.readUInt16BE(4) / 100) * 100;
|
|
104
|
+
if (w >= 100 && w <= 900) facts.weight = String(w);
|
|
105
|
+
}
|
|
106
|
+
const italic = (os2 && os2.length >= 64 && (os2.readUInt16BE(62) & 1)) || (t.head && t.head.length >= 46 && (t.head.readUInt16BE(44) & 2));
|
|
107
|
+
if (os2 || t.head) facts.style = italic ? 'italic' : 'normal';
|
|
108
|
+
if (t.post && t.post.length >= 16 && t.post.readUInt32BE(12) !== 0) facts.fallback = 'mono';
|
|
109
|
+
else if (os2 && os2.length >= 32) {
|
|
110
|
+
// sFamilyClass: 1–5 and 7 are the serif classes, 8 is sans serif.
|
|
111
|
+
const cls = os2.readInt16BE(30) >> 8;
|
|
112
|
+
if ([1, 2, 3, 4, 5, 7].includes(cls)) facts.fallback = 'serif';
|
|
113
|
+
else if (cls === 8) facts.fallback = 'sans';
|
|
114
|
+
}
|
|
115
|
+
return facts;
|
|
116
|
+
} catch {
|
|
117
|
+
return {};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// { wght: [200, 900], opsz: [7, 72] } for a variable font; null otherwise.
|
|
122
|
+
export function fontAxes(buffer, kind) {
|
|
123
|
+
try {
|
|
124
|
+
const { fvar } = tables(buffer, kind, ['fvar']);
|
|
125
|
+
if (!fvar || fvar.length < 16) return null;
|
|
126
|
+
const start = fvar.readUInt16BE(4);
|
|
127
|
+
const count = fvar.readUInt16BE(8);
|
|
128
|
+
const size = fvar.readUInt16BE(10);
|
|
129
|
+
const axes = {};
|
|
130
|
+
for (let i = 0; i < count; i += 1) {
|
|
131
|
+
const at = start + i * size;
|
|
132
|
+
const fixed = (o) => Math.round((fvar.readInt32BE(at + o) / 65536) * 100) / 100;
|
|
133
|
+
axes[fvar.slice(at, at + 4).toString('latin1')] = [fixed(4), fixed(12)];
|
|
134
|
+
}
|
|
135
|
+
return Object.keys(axes).length ? axes : null;
|
|
136
|
+
} catch {
|
|
137
|
+
return null; // a file Verso can't read this far is treated as one weight
|
|
138
|
+
}
|
|
139
|
+
}
|
package/src/git.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// Git is the database. Editors never type a git command: opening the editor
|
|
2
|
+
// pulls, and Publish commits and pushes with a message a person can read.
|
|
3
|
+
|
|
4
|
+
import { execFile } from 'child_process';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import { humanize } from './template.js';
|
|
8
|
+
|
|
9
|
+
const git = (root, args, { buffer = false } = {}) =>
|
|
10
|
+
new Promise((resolve) => {
|
|
11
|
+
execFile('git', args, { cwd: root, maxBuffer: 50 * 1024 * 1024, encoding: buffer ? 'buffer' : 'utf8' }, (error, stdout, stderr) => {
|
|
12
|
+
resolve({ ok: !error, code: error ? error.code : 0, stdout: buffer ? stdout : String(stdout), stderr: String(stderr) });
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// "content/journal/first/page.json" → "/journal/first/"; null if not in a page folder
|
|
17
|
+
const urlForContentFile = (file) => {
|
|
18
|
+
const m = /^content\/(?:(.*)\/)?[^/]+$/.exec(file);
|
|
19
|
+
if (!m) return null;
|
|
20
|
+
return m[1] ? `/${m[1]}/` : '/';
|
|
21
|
+
};
|
|
22
|
+
const dirForPageUrl = (url) => {
|
|
23
|
+
const parts = String(url || '/').split('/').filter(Boolean);
|
|
24
|
+
if (parts.some((p) => p === '..' || p.startsWith('.'))) return null;
|
|
25
|
+
return parts.length ? `content/${parts.join('/')}/` : 'content/';
|
|
26
|
+
};
|
|
27
|
+
const labelForUrl = (url) => (url === '/' ? 'Home' : humanize(url.split('/').filter(Boolean).pop()));
|
|
28
|
+
|
|
29
|
+
export async function gitState(root) {
|
|
30
|
+
const inside = await git(root, ['rev-parse', '--is-inside-work-tree']);
|
|
31
|
+
if (!inside.ok) {
|
|
32
|
+
if (/ENOENT/.test(String(inside.code)) || /not found|ENOENT/i.test(inside.stderr)) return { available: false, repo: false };
|
|
33
|
+
return { available: true, repo: false };
|
|
34
|
+
}
|
|
35
|
+
const remotes = (await git(root, ['remote'])).stdout.split('\n').filter(Boolean);
|
|
36
|
+
const upstream = await git(root, ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']);
|
|
37
|
+
const branch = (await git(root, ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.trim();
|
|
38
|
+
const status = await git(root, ['status', '--porcelain', '-uall']);
|
|
39
|
+
const changed = status.stdout.split('\n').filter(Boolean).map((l) => l.slice(3).replace(/^"|"$/g, ''));
|
|
40
|
+
const hasCommits = (await git(root, ['rev-parse', '--verify', 'HEAD'])).ok;
|
|
41
|
+
let ahead = 0;
|
|
42
|
+
if (upstream.ok) {
|
|
43
|
+
const count = await git(root, ['rev-list', '--count', '@{u}..HEAD']);
|
|
44
|
+
ahead = Number(count.stdout.trim()) || 0;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
available: true,
|
|
48
|
+
repo: true,
|
|
49
|
+
branch,
|
|
50
|
+
remote: remotes[0] || null,
|
|
51
|
+
upstream: upstream.ok ? upstream.stdout.trim() : null,
|
|
52
|
+
changed,
|
|
53
|
+
ahead,
|
|
54
|
+
hasCommits,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A JSON file that only changed in spacing or key order isn't a change.
|
|
59
|
+
async function sameAsBefore(root, file) {
|
|
60
|
+
if (!file.endsWith('.json')) return false;
|
|
61
|
+
const shown = await git(root, ['show', `HEAD:${file}`]);
|
|
62
|
+
if (!shown.ok) return false;
|
|
63
|
+
try {
|
|
64
|
+
const before = JSON.parse(shown.stdout);
|
|
65
|
+
const after = JSON.parse(fs.readFileSync(path.join(root, file), 'utf8'));
|
|
66
|
+
const canon = (v) => (Array.isArray(v) ? v.map(canon) : v && typeof v === 'object' ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, canon(v[k])])) : v);
|
|
67
|
+
return JSON.stringify(canon(before)) === JSON.stringify(canon(after));
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Changed files that really changed.
|
|
74
|
+
export async function realChanges(root, files) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const f of files) if (!(await sameAsBefore(root, f))) out.push(f);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Files that aren't pages, in words.
|
|
81
|
+
const fileWords = (file) => (file === 'content/site.json' ? 'the shared header and footer' : file);
|
|
82
|
+
|
|
83
|
+
// The pages a set of changed files belongs to, with what changed on each.
|
|
84
|
+
async function describeChanges(root, files) {
|
|
85
|
+
const pages = [];
|
|
86
|
+
const other = [];
|
|
87
|
+
files = files.filter((f) => f !== 'content/site.json').concat(files.includes('content/site.json') ? ['content/site.json'] : []);
|
|
88
|
+
// Rich text files and images belong to the page whose folder they're in.
|
|
89
|
+
const pageFiles = new Set(files.filter((f) => /^content\/(.*\/)?page\.json$/.test(f)));
|
|
90
|
+
const extras = new Map();
|
|
91
|
+
for (const file of files) {
|
|
92
|
+
if (pageFiles.has(file) || !file.startsWith('content/') || file === 'content/site.json') continue;
|
|
93
|
+
const dir = file.slice(0, file.lastIndexOf('/') + 1);
|
|
94
|
+
if (!fs.existsSync(path.join(root, dir, 'page.json')) && !files.includes(`${dir}page.json`)) continue;
|
|
95
|
+
pageFiles.add(`${dir}page.json`);
|
|
96
|
+
const name = file.slice(dir.length);
|
|
97
|
+
if (!extras.has(dir)) extras.set(dir, new Set());
|
|
98
|
+
extras.get(dir).add(name.endsWith('.html') ? humanize(name.slice(0, -5)).toLowerCase() : 'images');
|
|
99
|
+
}
|
|
100
|
+
for (const file of [...files.filter((f) => !pageFiles.has(f) && !(f.startsWith('content/') && extras.has(f.slice(0, f.lastIndexOf('/') + 1)))), ...pageFiles]) {
|
|
101
|
+
const m = /^content\/(.*?)\/?page\.json$/.exec(file);
|
|
102
|
+
if (!m) {
|
|
103
|
+
other.push(fileWords(file));
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const extra = [...(extras.get(file.slice(0, file.lastIndexOf('/') + 1)) || [])];
|
|
107
|
+
const url = m[1] ? `/${m[1]}/` : '/';
|
|
108
|
+
let before = null;
|
|
109
|
+
const shown = await git(root, ['show', `HEAD:${file}`]);
|
|
110
|
+
if (shown.ok) {
|
|
111
|
+
try { before = JSON.parse(shown.stdout); } catch { before = null; }
|
|
112
|
+
}
|
|
113
|
+
let after = null;
|
|
114
|
+
try { after = JSON.parse(fs.readFileSync(path.join(root, file), 'utf8')); } catch { after = null; }
|
|
115
|
+
const label = url === '/' ? 'Home' : humanize(url.split('/').filter(Boolean).pop());
|
|
116
|
+
if (!after) { pages.push({ label, kind: 'removed', parts: [] }); continue; }
|
|
117
|
+
if (!before) { pages.push({ label: (after.title || label).split(/ [—|-] /)[0], kind: 'added', parts: [] }); continue; }
|
|
118
|
+
const parts = [...extra];
|
|
119
|
+
if ((before.title || '') !== (after.title || '')) parts.push('title');
|
|
120
|
+
if ((before.description || '') !== (after.description || '')) parts.push('description');
|
|
121
|
+
const keys = new Set([...Object.keys(before.fields || {}), ...Object.keys(after.fields || {})]);
|
|
122
|
+
keys.forEach((k) => {
|
|
123
|
+
if ((before.fields || {})[k] !== (after.fields || {})[k]) parts.push(humanize(k).toLowerCase());
|
|
124
|
+
});
|
|
125
|
+
if (before.template !== after.template) parts.push('page type');
|
|
126
|
+
if ((before.date || '') !== (after.date || '')) parts.push('date');
|
|
127
|
+
if (JSON.stringify(before.tags || []) !== JSON.stringify(after.tags || [])) parts.push('tags');
|
|
128
|
+
if (JSON.stringify(before.blocks || {}) !== JSON.stringify(after.blocks || {})) parts.push('blocks');
|
|
129
|
+
if (!!before.draft !== !!after.draft) parts.push(after.draft ? 'back to draft' : 'made live');
|
|
130
|
+
if ((before.order ?? null) !== (after.order ?? null)) parts.push('position');
|
|
131
|
+
if (!parts.length) parts.push('settings');
|
|
132
|
+
pages.push({ label, kind: 'changed', parts });
|
|
133
|
+
}
|
|
134
|
+
return { pages, other };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const list = (items) => (items.length <= 1 ? items.join('') : `${items.slice(0, -1).join(', ')} and ${items[items.length - 1]}`);
|
|
138
|
+
|
|
139
|
+
// "Update Home (headline, intro) and About (title)"
|
|
140
|
+
export function commitMessage({ pages, other }) {
|
|
141
|
+
const phrases = pages.map((p) => {
|
|
142
|
+
if (p.kind === 'added') return `add ${p.label}`;
|
|
143
|
+
if (p.kind === 'removed') return `remove ${p.label}`;
|
|
144
|
+
const parts = p.parts.length > 4 ? [...p.parts.slice(0, 3), `${p.parts.length - 3} more`] : p.parts;
|
|
145
|
+
return parts.length ? `${p.label} (${parts.join(', ')})` : p.label;
|
|
146
|
+
});
|
|
147
|
+
const updates = phrases.filter((p) => !/^(add|remove) /.test(p));
|
|
148
|
+
const actions = phrases.filter((p) => /^(add|remove) /.test(p));
|
|
149
|
+
const sentences = [];
|
|
150
|
+
if (updates.length) sentences.push(`Update ${list(updates)}`);
|
|
151
|
+
actions.forEach((a) => sentences.push(a.charAt(0).toUpperCase() + a.slice(1)));
|
|
152
|
+
if (other.length) {
|
|
153
|
+
const named = other.length <= 3 ? list(other) : `${other.length} site files`;
|
|
154
|
+
sentences.push(`${sentences.length ? 'change' : 'Change'} ${named}`);
|
|
155
|
+
}
|
|
156
|
+
let message = sentences.join('; ') || 'Update the site';
|
|
157
|
+
if (message.length > 120) message = `Update ${pages.length} page${pages.length === 1 ? '' : 's'}${other.length ? ` and ${other.length} site file${other.length === 1 ? '' : 's'}` : ''}`;
|
|
158
|
+
return message;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// What Publish would do right now, in words for the editor's button.
|
|
162
|
+
export async function publishSummary(root) {
|
|
163
|
+
const state = await gitState(root);
|
|
164
|
+
if (!state.repo) return { ...state, pending: 0, pages: [] };
|
|
165
|
+
const changed = await realChanges(root, state.changed);
|
|
166
|
+
const described = await describeChanges(root, changed);
|
|
167
|
+
return {
|
|
168
|
+
...state,
|
|
169
|
+
changed,
|
|
170
|
+
pending: changed.length + state.ahead,
|
|
171
|
+
pages: described.pages.map((p) => p.label),
|
|
172
|
+
changes: [...described.pages.map((p) => ({ label: p.label, kind: p.kind, parts: p.parts })), ...described.other.map((o) => ({ label: o.charAt(0).toUpperCase() + o.slice(1), kind: 'other', parts: [] }))],
|
|
173
|
+
message: changed.length ? commitMessage(described) : null,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const plain = (stderr) => {
|
|
178
|
+
if (/Please tell me who you are|empty ident/i.test(stderr)) {
|
|
179
|
+
return 'Git doesn\'t know your name yet, so it can\'t save a restore point. In a terminal, run:\n git config --global user.name "Your Name"\n git config --global user.email "you@example.com"';
|
|
180
|
+
}
|
|
181
|
+
if (/Could not resolve host|unable to access|Connection timed out|Network is unreachable/i.test(stderr)) {
|
|
182
|
+
return 'Your changes are saved here, but Verso couldn\'t reach the server to publish them. Check your internet connection and press Publish again.';
|
|
183
|
+
}
|
|
184
|
+
if (/Authentication failed|Permission denied|could not read Username|403/i.test(stderr)) {
|
|
185
|
+
return 'The server didn\'t accept your login, so nothing was published. Your changes are saved here. Check that you can push to this repository from the terminal.';
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// Called when the editor opens. Only pulls when it can't disturb anything:
|
|
191
|
+
// a clean folder, a remote branch to follow, and nothing unpublished.
|
|
192
|
+
export async function pullLatest(root) {
|
|
193
|
+
const state = await gitState(root);
|
|
194
|
+
if (!state.repo || !state.upstream) return { pulled: false, reason: 'no-remote' };
|
|
195
|
+
if (state.changed.length) return { pulled: false, reason: 'unpublished' };
|
|
196
|
+
const fetched = await git(root, ['pull', '--ff-only', '--quiet']);
|
|
197
|
+
if (!fetched.ok) return { pulled: false, reason: 'failed', message: plain(fetched.stderr) || 'Verso couldn\'t get the latest version of the site, so you\'re editing what\'s on this computer.' };
|
|
198
|
+
return { pulled: true };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export async function publish(root, { note = '', author = null } = {}) {
|
|
202
|
+
const state = await gitState(root);
|
|
203
|
+
if (!state.available) return { ok: false, message: 'Git isn\'t installed on this computer, and Verso needs it to publish. Install it from git-scm.com.' };
|
|
204
|
+
if (!state.repo) return { ok: false, message: 'This site isn\'t a git repository yet, so there\'s nowhere to publish. In the site folder, run: git init' };
|
|
205
|
+
|
|
206
|
+
let committed = null;
|
|
207
|
+
// Files that only changed in spacing go back as they were, so they're
|
|
208
|
+
// neither published nor named.
|
|
209
|
+
const real = await realChanges(root, state.changed);
|
|
210
|
+
const noops = state.changed.filter((f) => !real.includes(f));
|
|
211
|
+
if (noops.length) await git(root, ['checkout', '--quiet', 'HEAD', '--', ...noops]);
|
|
212
|
+
state.changed = real;
|
|
213
|
+
if (state.changed.length) {
|
|
214
|
+
// The person's own words lead; what Verso counted follows, so History
|
|
215
|
+
// can still say which pages changed.
|
|
216
|
+
const counted = commitMessage(await describeChanges(root, state.changed));
|
|
217
|
+
const said = String(note || '').replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
218
|
+
const message = said || counted;
|
|
219
|
+
const added = await git(root, ['add', '-A']);
|
|
220
|
+
if (!added.ok) return { ok: false, message: plain(added.stderr) || `Verso couldn't prepare your changes: ${added.stderr.trim()}` };
|
|
221
|
+
// On a shared site, the restore point carries the name of whoever published.
|
|
222
|
+
const as = author?.name && author?.email ? ['-c', `user.name=${String(author.name).replace(/[<>\n]/g, '')}`, '-c', `user.email=${String(author.email).replace(/[<>\n]/g, '')}`] : [];
|
|
223
|
+
const commit = await git(root, [...as, ...(said ? ['commit', '-m', message, '-m', counted] : ['commit', '-m', message])]);
|
|
224
|
+
if (!commit.ok) {
|
|
225
|
+
await git(root, ['reset', '--quiet']);
|
|
226
|
+
return { ok: false, message: plain(commit.stderr + commit.stdout) || `Verso couldn't save a restore point: ${(commit.stderr || commit.stdout).trim()}` };
|
|
227
|
+
}
|
|
228
|
+
committed = message;
|
|
229
|
+
} else if (!state.ahead) {
|
|
230
|
+
return { ok: true, nothing: true, message: 'Everything is already published.' };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!state.remote) {
|
|
234
|
+
return { ok: true, local: true, committed, message: 'Saved a restore point. It isn\'t online yet: connect this folder to a GitHub repository, and Publish will put it there.' };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const pushArgs = state.upstream ? ['push', '--quiet'] : ['push', '--quiet', '-u', state.remote, state.branch];
|
|
238
|
+
let pushed = await git(root, pushArgs);
|
|
239
|
+
if (!pushed.ok && /rejected|fetch first|non-fast-forward/i.test(pushed.stderr)) {
|
|
240
|
+
// Someone else published first. Bring their changes in, then try again.
|
|
241
|
+
const rebase = await git(root, ['pull', '--rebase', '--quiet']);
|
|
242
|
+
if (!rebase.ok) {
|
|
243
|
+
const conflicted = (await git(root, ['diff', '--name-only', '--diff-filter=U'])).stdout.split('\n').filter(Boolean);
|
|
244
|
+
await git(root, ['rebase', '--abort']);
|
|
245
|
+
if (conflicted.length && conflicted.every((f) => urlForContentFile(f))) {
|
|
246
|
+
// Both sides changed the same page. Keep both safe and ask which to keep.
|
|
247
|
+
const conflict = await recordConflict(root, conflicted);
|
|
248
|
+
return { ok: false, committed, conflict, message: `Someone else published changes to ${conflict.pages.map((p) => p.label).join(' and ')} while you were editing. Your version is saved here. Choose which version to keep.` };
|
|
249
|
+
}
|
|
250
|
+
if (!conflicted.length) return { ok: false, committed, message: plain(rebase.stderr) || 'Verso couldn\'t bring in the latest published version, so nothing was published. Your changes are saved here.' };
|
|
251
|
+
return { ok: false, committed, message: 'Someone else changed the same site files (not just page content) while you were editing. Your version is saved here, but it isn\'t published. Ask a developer to combine the two.' };
|
|
252
|
+
}
|
|
253
|
+
pushed = await git(root, pushArgs);
|
|
254
|
+
}
|
|
255
|
+
if (!pushed.ok) {
|
|
256
|
+
return { ok: false, committed, message: plain(pushed.stderr) || `Your changes are saved here, but publishing failed: ${pushed.stderr.trim()}` };
|
|
257
|
+
}
|
|
258
|
+
return { ok: true, committed, message: 'Published. Your host will update the live site in a minute or two.' };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ════ History ════
|
|
262
|
+
// Every publish is a restore point. history() lists them, newest first.
|
|
263
|
+
|
|
264
|
+
export async function history(root, { url = null, limit = 50 } = {}) {
|
|
265
|
+
const inside = await git(root, ['rev-parse', '--verify', 'HEAD']);
|
|
266
|
+
if (!inside.ok) return [];
|
|
267
|
+
const dir = url ? dirForPageUrl(url) : null;
|
|
268
|
+
if (url && !dir) return [];
|
|
269
|
+
const args = ['log', `-n${limit * (dir ? 3 : 1)}`, '--format=%x1e%H%x1f%s%x1f%an%x1f%aI', '--name-only'];
|
|
270
|
+
if (dir) args.push('--', dir);
|
|
271
|
+
const out = await git(root, args);
|
|
272
|
+
const upstream = await git(root, ['rev-parse', '--verify', '--quiet', '@{u}']);
|
|
273
|
+
const unpushed = new Set(upstream.ok ? (await git(root, ['rev-list', '@{u}..HEAD'])).stdout.split('\n').filter(Boolean) : []);
|
|
274
|
+
const entries = out.stdout.split('\x1e').filter((c) => c.trim()).map((chunk) => {
|
|
275
|
+
const [head, ...files] = chunk.split('\n').filter(Boolean);
|
|
276
|
+
const [sha, message, author, date] = head.split('\x1f');
|
|
277
|
+
const urls = [...new Set(files.map(urlForContentFile).filter(Boolean))];
|
|
278
|
+
return {
|
|
279
|
+
sha, message, author, date, files,
|
|
280
|
+
published: upstream.ok ? !unpushed.has(sha) : null,
|
|
281
|
+
pages: urls.map((u) => ({ url: u, label: labelForUrl(u) })),
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
// For one page, only commits that touched its own folder, not pages inside it.
|
|
285
|
+
const filtered = dir ? entries.filter((e) => e.files.some((f) => f.startsWith(dir) && !f.slice(dir.length).includes('/'))) : entries;
|
|
286
|
+
return filtered.slice(0, limit).map(({ files, ...e }) => e);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// The files of one page folder at a commit: [{ name, data: Buffer }].
|
|
290
|
+
export async function pageAt(root, sha, url) {
|
|
291
|
+
const dir = dirForPageUrl(url);
|
|
292
|
+
if (!dir || !/^[0-9a-f]{7,40}$/i.test(sha)) return null;
|
|
293
|
+
const tree = await git(root, ['ls-tree', sha, '--', dir]);
|
|
294
|
+
if (!tree.ok) return null;
|
|
295
|
+
const files = [];
|
|
296
|
+
for (const line of tree.stdout.split('\n').filter(Boolean)) {
|
|
297
|
+
const [meta, file] = line.split('\t');
|
|
298
|
+
if (meta.split(' ')[1] !== 'blob') continue;
|
|
299
|
+
const shown = await git(root, ['show', `${sha}:${file}`], { buffer: true });
|
|
300
|
+
if (shown.ok) files.push({ name: file.slice(dir.length), data: shown.stdout });
|
|
301
|
+
}
|
|
302
|
+
return files.some((f) => f.name === 'page.json') ? files : null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Puts a page back the way it was at a commit, as unpublished changes.
|
|
306
|
+
// Restoring a later version undoes it.
|
|
307
|
+
export async function restorePage(root, url, sha) {
|
|
308
|
+
const files = await pageAt(root, sha, url);
|
|
309
|
+
if (!files) return { ok: false, message: 'This page didn\'t exist at that point, so there\'s nothing to restore.' };
|
|
310
|
+
const folder = pageFolder(root, url);
|
|
311
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
312
|
+
// The page as it is now, so the restore can be undone.
|
|
313
|
+
const before = fs.readdirSync(folder, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => ({ name: e.name, data: fs.readFileSync(path.join(folder, e.name)) }));
|
|
314
|
+
writeFolder(folder, files);
|
|
315
|
+
return { ok: true, before };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Puts a page folder back to exactly these files.
|
|
319
|
+
export function writeFolder(folder, files) {
|
|
320
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
321
|
+
const keep = new Set(files.map((f) => f.name));
|
|
322
|
+
fs.readdirSync(folder, { withFileTypes: true }).forEach((e) => {
|
|
323
|
+
if (e.isFile() && !keep.has(e.name)) fs.rmSync(path.join(folder, e.name));
|
|
324
|
+
});
|
|
325
|
+
files.forEach((f) => fs.writeFileSync(path.join(folder, f.name), f.data));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export const pageFolder = (root, url) => path.join(root, ...dirForPageUrl(url).split('/').filter(Boolean));
|
|
329
|
+
|
|
330
|
+
// ════ Conflicts ════
|
|
331
|
+
// When two people publish changes to the same page, both versions stay safe:
|
|
332
|
+
// the rebase is undone, and the clash is written down so the editor can show
|
|
333
|
+
// both versions side by side.
|
|
334
|
+
|
|
335
|
+
const conflictFile = (root) => path.join(root, '.git', 'verso-conflict.json');
|
|
336
|
+
|
|
337
|
+
async function recordConflict(root, files) {
|
|
338
|
+
const head = (await git(root, ['rev-parse', 'HEAD'])).stdout.trim();
|
|
339
|
+
const theirs = (await git(root, ['rev-parse', '@{u}'])).stdout.trim();
|
|
340
|
+
const [author = 'someone', date = ''] = (await git(root, ['log', '-1', '--format=%an%x1f%aI', theirs])).stdout.trim().split('\x1f');
|
|
341
|
+
const urls = [...new Set(files.map(urlForContentFile))];
|
|
342
|
+
const record = {
|
|
343
|
+
mine: { sha: head },
|
|
344
|
+
theirs: { sha: theirs, author, date },
|
|
345
|
+
pages: urls.map((u) => ({ url: u, label: labelForUrl(u) })),
|
|
346
|
+
choices: {},
|
|
347
|
+
};
|
|
348
|
+
fs.writeFileSync(conflictFile(root), JSON.stringify(record, null, 2));
|
|
349
|
+
return { pages: record.pages, theirs: record.theirs };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function conflictPending(root) {
|
|
353
|
+
return fs.existsSync(conflictFile(root));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export async function getConflict(root) {
|
|
357
|
+
if (!conflictPending(root)) return null;
|
|
358
|
+
let record;
|
|
359
|
+
try {
|
|
360
|
+
record = JSON.parse(fs.readFileSync(conflictFile(root), 'utf8'));
|
|
361
|
+
} catch {
|
|
362
|
+
fs.rmSync(conflictFile(root), { force: true });
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
// Stale: the site moved on (published, pulled or reset) since.
|
|
366
|
+
const head = (await git(root, ['rev-parse', 'HEAD'])).stdout.trim();
|
|
367
|
+
if (head !== record.mine.sha) {
|
|
368
|
+
fs.rmSync(conflictFile(root), { force: true });
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
return record;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// keep: 'mine' or 'theirs', for one page. When every clashing page has a
|
|
375
|
+
// choice, Verso brings in their changes, keeps the chosen version of each
|
|
376
|
+
// page, and publishes.
|
|
377
|
+
export async function resolveConflict(root, url, keep) {
|
|
378
|
+
const record = await getConflict(root);
|
|
379
|
+
if (!record) return { ok: false, message: 'There\'s nothing to choose between any more. Press Publish to try again.' };
|
|
380
|
+
if (!['mine', 'theirs'].includes(keep)) return { ok: false, message: 'Choose your version or theirs.' };
|
|
381
|
+
if (!record.pages.some((p) => p.url === url)) return { ok: false, message: 'That page isn\'t part of this clash.' };
|
|
382
|
+
record.choices[url] = keep;
|
|
383
|
+
fs.writeFileSync(conflictFile(root), JSON.stringify(record, null, 2));
|
|
384
|
+
const waiting = record.pages.filter((p) => !record.choices[p.url]);
|
|
385
|
+
if (waiting.length) return { ok: true, pending: waiting, message: `Now choose for ${waiting.map((p) => p.label).join(' and ')}.` };
|
|
386
|
+
|
|
387
|
+
const fetched = await git(root, ['fetch', '--quiet']);
|
|
388
|
+
if (!fetched.ok) return { ok: false, message: plain(fetched.stderr) || 'Verso couldn\'t reach the server. Check your connection and try again.' };
|
|
389
|
+
let step = await git(root, ['rebase', '@{u}']);
|
|
390
|
+
for (let rounds = 0; !step.ok && rounds < 50; rounds++) {
|
|
391
|
+
const files = (await git(root, ['diff', '--name-only', '--diff-filter=U'])).stdout.split('\n').filter(Boolean);
|
|
392
|
+
const unknown = files.filter((f) => !record.choices[urlForContentFile(f)]);
|
|
393
|
+
if (!files.length || unknown.length) {
|
|
394
|
+
await git(root, ['rebase', '--abort']);
|
|
395
|
+
return { ok: false, message: unknown.length ? 'More changed while you were choosing. Press Publish again to see what clashes now.' : `Verso couldn't combine the two versions: ${step.stderr.trim()}` };
|
|
396
|
+
}
|
|
397
|
+
for (const f of files) {
|
|
398
|
+
// During a rebase, "ours" is what's published and "theirs" is your change.
|
|
399
|
+
const side = record.choices[urlForContentFile(f)] === 'mine' ? '--theirs' : '--ours';
|
|
400
|
+
const out = await git(root, ['checkout', side, '--', f]);
|
|
401
|
+
if (out.ok) await git(root, ['add', '--', f]);
|
|
402
|
+
else await git(root, ['rm', '--quiet', '--', f]);
|
|
403
|
+
}
|
|
404
|
+
const nothing = (await git(root, ['diff', '--cached', '--quiet'])).ok;
|
|
405
|
+
step = nothing ? await git(root, ['rebase', '--skip']) : await git(root, ['-c', 'core.editor=true', 'rebase', '--continue']);
|
|
406
|
+
}
|
|
407
|
+
if (!step.ok) {
|
|
408
|
+
await git(root, ['rebase', '--abort']);
|
|
409
|
+
return { ok: false, message: 'Verso couldn\'t combine the two versions. Your version is still saved here.' };
|
|
410
|
+
}
|
|
411
|
+
fs.rmSync(conflictFile(root), { force: true });
|
|
412
|
+
const ahead = Number((await git(root, ['rev-list', '--count', '@{u}..HEAD'])).stdout.trim()) || 0;
|
|
413
|
+
if (ahead) {
|
|
414
|
+
const pushed = await git(root, ['push', '--quiet']);
|
|
415
|
+
if (!pushed.ok) return { ok: false, message: plain(pushed.stderr) || `Your choice is saved here, but publishing failed: ${pushed.stderr.trim()}` };
|
|
416
|
+
}
|
|
417
|
+
const kept = record.pages.map((p) => `${record.choices[p.url] === 'mine' ? 'your' : 'their'} version of ${p.label}`).join(', ');
|
|
418
|
+
return { ok: true, message: `Kept ${kept}, and published. Your host will update the live site in a minute or two.` };
|
|
419
|
+
}
|