bookish-press 0.11.2 → 0.12.0
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/dist/components/chapter/Block.svelte +19 -0
- package/dist/components/chapter/Block.svelte.d.ts +1 -0
- package/dist/components/chapter/ChapterBody.test.js +69 -0
- package/dist/components/chapter/Format.svelte +4 -0
- package/dist/components/chapter/Marginal.svelte +2 -1
- package/dist/components/chapter/Symbol.svelte +27 -0
- package/dist/components/chapter/Symbol.svelte.d.ts +7 -0
- package/dist/components/editor/BookishEditor.svelte +41 -153
- package/dist/components/editor/Commands.js +45 -44
- package/dist/components/editor/Commands.test.d.ts +1 -0
- package/dist/components/editor/Commands.test.js +791 -0
- package/dist/components/editor/Editing.d.ts +51 -0
- package/dist/components/editor/Editing.js +201 -0
- package/dist/components/editor/Permissions.svelte +10 -2
- package/dist/components/page/BookLoader.svelte +43 -6
- package/dist/components/page/BulkReferenceEditor.svelte +2 -2
- package/dist/components/page/Chapter.svelte +77 -70
- package/dist/components/page/Contexts.d.ts +14 -0
- package/dist/components/page/Contexts.js +14 -4
- package/dist/components/page/Edition.svelte +2 -10
- package/dist/components/page/EditionLoader.svelte +40 -7
- package/dist/components/page/Index.svelte +1 -1
- package/dist/components/page/Publish.svelte +0 -42
- package/dist/components/page/Reference.svelte +2 -2
- package/dist/components/page/References.svelte +5 -6
- package/dist/components/page/TitlePage.svelte +65 -0
- package/dist/components/page/TitlePage.svelte.d.ts +20 -0
- package/dist/components/page/UnsavedNotice.svelte +96 -0
- package/dist/components/page/UnsavedNotice.svelte.d.ts +7 -0
- package/dist/epub/xhtml.js +15 -0
- package/dist/models/CRUD.d.ts +28 -12
- package/dist/models/CRUD.emu.test.d.ts +1 -0
- package/dist/models/CRUD.emu.test.js +250 -0
- package/dist/models/CRUD.js +110 -72
- package/dist/models/book/Book.js +1 -1
- package/dist/models/book/Book.test.d.ts +1 -0
- package/dist/models/book/Book.test.js +124 -0
- package/dist/models/book/BookMedia.d.ts +5 -0
- package/dist/models/book/BookMedia.js +23 -7
- package/dist/models/book/BookMedia.test.d.ts +1 -0
- package/dist/models/book/BookMedia.test.js +40 -0
- package/dist/models/book/Chapter.js +28 -12
- package/dist/models/book/Chapter.test.d.ts +1 -0
- package/dist/models/book/Chapter.test.js +167 -0
- package/dist/models/book/Edition.d.ts +2 -0
- package/dist/models/book/Edition.js +34 -25
- package/dist/models/book/Edition.test.d.ts +1 -0
- package/dist/models/book/Edition.test.js +342 -0
- package/dist/models/book/Reference.js +5 -9
- package/dist/models/book/Reference.test.d.ts +1 -0
- package/dist/models/book/Reference.test.js +40 -0
- package/dist/models/chapter/BlocksNode.d.ts +2 -0
- package/dist/models/chapter/BlocksNode.js +59 -3
- package/dist/models/chapter/BlocksNode.test.js +2 -2
- package/dist/models/chapter/Caret.js +12 -3
- package/dist/models/chapter/Caret.test.d.ts +1 -0
- package/dist/models/chapter/Caret.test.js +127 -0
- package/dist/models/chapter/ChapterNode.d.ts +3 -0
- package/dist/models/chapter/ChapterNode.js +11 -1
- package/dist/models/chapter/CommentNode.js +3 -2
- package/dist/models/chapter/EmbedNode.js +1 -1
- package/dist/models/chapter/FootnoteNode.js +1 -1
- package/dist/models/chapter/FormatNode.d.ts +5 -1
- package/dist/models/chapter/FormatNode.js +60 -20
- package/dist/models/chapter/LabelNode.js +4 -2
- package/dist/models/chapter/ListNode.js +11 -13
- package/dist/models/chapter/Node.test.js +2 -11
- package/dist/models/chapter/Operations.test.d.ts +1 -0
- package/dist/models/chapter/Operations.test.js +298 -0
- package/dist/models/chapter/ParagraphNode.js +29 -3
- package/dist/models/chapter/Parser.d.ts +49 -4
- package/dist/models/chapter/Parser.js +210 -88
- package/dist/models/chapter/Parser.test.js +31 -0
- package/dist/models/chapter/Roundtrip.test.d.ts +1 -0
- package/dist/models/chapter/Roundtrip.test.js +313 -0
- package/dist/models/chapter/SymbolBlockNode.d.ts +40 -0
- package/dist/models/chapter/SymbolBlockNode.js +75 -0
- package/dist/models/chapter/SymbolBlockNode.test.d.ts +1 -0
- package/dist/models/chapter/SymbolBlockNode.test.js +130 -0
- package/dist/models/chapter/SymbolNode.d.ts +32 -0
- package/dist/models/chapter/SymbolNode.js +61 -0
- package/dist/models/chapter/SymbolNode.test.d.ts +1 -0
- package/dist/models/chapter/SymbolNode.test.js +177 -0
- package/dist/models/chapter/TableNode.js +2 -2
- package/dist/models/chapter/TextNode.d.ts +8 -1
- package/dist/models/chapter/TextNode.js +22 -15
- package/dist/util/getSubdomain.js +5 -1
- package/dist/util/getSubdomain.test.d.ts +1 -0
- package/dist/util/getSubdomain.test.js +53 -0
- package/dist/util/mineReference.js +12 -5
- package/dist/util/mineReference.test.d.ts +1 -0
- package/dist/util/mineReference.test.js +62 -0
- package/package.json +12 -7
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { describe, test, expect } from 'vitest';
|
|
2
|
+
import Edition from './Edition';
|
|
3
|
+
import * as fixtures from '../../../tests/fixtures';
|
|
4
|
+
import Chapter from './Chapter';
|
|
5
|
+
import Reference from './Reference';
|
|
6
|
+
import FormatNode from '../chapter/FormatNode';
|
|
7
|
+
import { bookRef, editionRef, makeEdition } from '../../../tests/fixtures';
|
|
8
|
+
function chapter(id, options = {}) {
|
|
9
|
+
return Chapter.fromJSON({
|
|
10
|
+
ref: options.ref === undefined
|
|
11
|
+
? undefined
|
|
12
|
+
: { id: options.ref },
|
|
13
|
+
id,
|
|
14
|
+
title: id.toUpperCase(),
|
|
15
|
+
authors: [],
|
|
16
|
+
image: null,
|
|
17
|
+
numbered: options.numbered ?? true,
|
|
18
|
+
forthcoming: options.forthcoming ?? false,
|
|
19
|
+
text: options.text === undefined ? `Text of ${id}.` : options.text,
|
|
20
|
+
uids: options.uids ?? [],
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function withChapters(...chapters) {
|
|
24
|
+
return makeEdition().withChapters(chapters);
|
|
25
|
+
}
|
|
26
|
+
function ids(edition) {
|
|
27
|
+
return edition.getChapters().map((c) => c?.getID());
|
|
28
|
+
}
|
|
29
|
+
describe('toObject/fromJSON', () => {
|
|
30
|
+
test('round trips every field', () => {
|
|
31
|
+
const original = makeEdition({
|
|
32
|
+
ko2020: ['Ko, A.', '2020', 'A title', 'A source', 'url', 'sum'],
|
|
33
|
+
note: 'An _informal_ reference',
|
|
34
|
+
})
|
|
35
|
+
.withChapters([
|
|
36
|
+
chapter('one', { uids: ['a', 'b'] }),
|
|
37
|
+
chapter('two', { uids: ['b', 'c'], numbered: false }),
|
|
38
|
+
])
|
|
39
|
+
.withTitle('Title')
|
|
40
|
+
.withAuthors(['Ann', 'Bob'])
|
|
41
|
+
.withSummary('Summary')
|
|
42
|
+
.withAcknowledgements('Thanks')
|
|
43
|
+
.setLicense('CC')
|
|
44
|
+
.withGTag('G-123')
|
|
45
|
+
.withDefinition('term', 'Term', 'A term', ['terms'])
|
|
46
|
+
.withHeader('references', 'Works cited');
|
|
47
|
+
const spec = original.toObject();
|
|
48
|
+
expect(spec.bookRef).toBe(bookRef);
|
|
49
|
+
expect(spec.title).toBe('Title');
|
|
50
|
+
expect(spec.authors).toEqual(['Ann', 'Bob']);
|
|
51
|
+
expect(spec.summary).toBe('Summary');
|
|
52
|
+
expect(spec.acknowledgements).toBe('Thanks');
|
|
53
|
+
expect(spec.license).toBe('CC');
|
|
54
|
+
expect(spec.gtagid).toBe('G-123');
|
|
55
|
+
expect(spec.uids).toEqual(['someuser']);
|
|
56
|
+
expect(spec.chapteruids.toSorted()).toEqual(['a', 'b', 'c']);
|
|
57
|
+
expect(spec.references.ko2020).toEqual([
|
|
58
|
+
'Ko, A.',
|
|
59
|
+
'2020',
|
|
60
|
+
'A title',
|
|
61
|
+
'A source',
|
|
62
|
+
'url',
|
|
63
|
+
'sum',
|
|
64
|
+
]);
|
|
65
|
+
expect(spec.references.note).toBe('An _informal_ reference');
|
|
66
|
+
expect(spec.headers?.references).toBe('Works cited');
|
|
67
|
+
// Chapter text is stored separately, not in the edition.
|
|
68
|
+
expect(spec.chapters[0]).not.toHaveProperty('text');
|
|
69
|
+
const restored = Edition.fromJSON(editionRef, spec);
|
|
70
|
+
expect(restored.toObject()).toEqual(spec);
|
|
71
|
+
expect(restored.getEditionRef()).toBe(editionRef);
|
|
72
|
+
expect(restored.getReference('ko2020')).toBeInstanceOf(Reference);
|
|
73
|
+
expect(restored.getReference('note')).toBeInstanceOf(FormatNode);
|
|
74
|
+
});
|
|
75
|
+
test('an empty base is stored as null', () => {
|
|
76
|
+
expect(makeEdition().toObject().base).toBeNull();
|
|
77
|
+
});
|
|
78
|
+
test('fills defaults for missing optional collections', () => {
|
|
79
|
+
const spec = makeEdition().toObject();
|
|
80
|
+
const partial = { ...spec };
|
|
81
|
+
delete partial.tags;
|
|
82
|
+
delete partial.sources;
|
|
83
|
+
delete partial.references;
|
|
84
|
+
delete partial.symbols;
|
|
85
|
+
delete partial.glossary;
|
|
86
|
+
const restored = Edition.fromJSON(editionRef, partial);
|
|
87
|
+
expect(restored.getTags()).toEqual([]);
|
|
88
|
+
expect(restored.getReferences()).toEqual({});
|
|
89
|
+
expect(restored.getGlossary()).toEqual({});
|
|
90
|
+
expect(restored.getSymbols()).toEqual({});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
describe('parseReference', () => {
|
|
94
|
+
const edition = makeEdition().withChapters([]);
|
|
95
|
+
test('parses a string as formatted text', () => {
|
|
96
|
+
const ref = Edition.parseReference('x', 'Some _text_', edition);
|
|
97
|
+
expect(ref).toBeInstanceOf(FormatNode);
|
|
98
|
+
expect(ref.toBookdown()).toBe('Some _text_');
|
|
99
|
+
});
|
|
100
|
+
test('parses a complete APA list', () => {
|
|
101
|
+
const ref = Edition.parseReference('X', ['A', '2000', 'T', 'S', 'U', 'Sum'], edition);
|
|
102
|
+
expect(ref.toList()).toEqual(['A', '2000', 'T', 'S', 'U', 'Sum']);
|
|
103
|
+
expect(ref.citationID).toBe('x');
|
|
104
|
+
});
|
|
105
|
+
test('tolerates lists with fewer than four fields', () => {
|
|
106
|
+
const ref = Edition.parseReference('x', ['A', '2000'], edition);
|
|
107
|
+
expect(ref.toList()).toEqual(['A', '2000', '', '', '', '']);
|
|
108
|
+
expect(Edition.parseReference('y', [], edition).toList()).toEqual(['', '', '', '', '', '']);
|
|
109
|
+
});
|
|
110
|
+
test('resolves #source shorthands', () => {
|
|
111
|
+
const withSources = new Edition(bookRef, editionRef, [], 't', [], 1, '', null, {}, undefined, '', [], '', '', [], { chi: 'ACM CHI' }, { a: ['A', '2000', 'T', '#chi'], b: ['A', '2000', 'T', '#nope'] }, {}, {}, null, null, {}, null);
|
|
112
|
+
expect(withSources.getReference('a').source).toBe('ACM CHI');
|
|
113
|
+
expect(withSources.getReference('b').source).toBe('#nope');
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('chapter list edits', () => {
|
|
117
|
+
const edition = withChapters(chapter('a'), chapter('b'), chapter('c'));
|
|
118
|
+
test('withMovedChapter moves up and down', () => {
|
|
119
|
+
expect(ids(edition.withMovedChapter('b', -1))).toEqual(['b', 'a', 'c']);
|
|
120
|
+
expect(ids(edition.withMovedChapter('b', 1))).toEqual(['a', 'c', 'b']);
|
|
121
|
+
});
|
|
122
|
+
test('withMovedChapter ignores moves past either end', () => {
|
|
123
|
+
expect(edition.withMovedChapter('a', -1)).toBe(edition);
|
|
124
|
+
expect(edition.withMovedChapter('c', 1)).toBe(edition);
|
|
125
|
+
});
|
|
126
|
+
test('withMovedChapter ignores unknown chapters', () => {
|
|
127
|
+
expect(edition.withMovedChapter('nope', 1)).toBe(edition);
|
|
128
|
+
expect(edition.withMovedChapter('nope', 2)).toBe(edition);
|
|
129
|
+
expect(ids(edition.withMovedChapter('nope', 1))).toEqual([
|
|
130
|
+
'a',
|
|
131
|
+
'b',
|
|
132
|
+
'c',
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
test('withMovedChapter does not mutate the original', () => {
|
|
136
|
+
edition.withMovedChapter('a', 1);
|
|
137
|
+
expect(ids(edition)).toEqual(['a', 'b', 'c']);
|
|
138
|
+
});
|
|
139
|
+
test('withoutChapter removes only the named chapter', () => {
|
|
140
|
+
expect(ids(edition.withoutChapter('b'))).toEqual(['a', 'c']);
|
|
141
|
+
expect(edition.withoutChapter('nope')).toBe(edition);
|
|
142
|
+
expect(ids(edition)).toEqual(['a', 'b', 'c']);
|
|
143
|
+
});
|
|
144
|
+
test('withNewChapter picks an unused ID', () => {
|
|
145
|
+
const one = withChapters(chapter('chapter1'), chapter('chapter2'));
|
|
146
|
+
const added = one.withNewChapter();
|
|
147
|
+
expect(ids(added)).toEqual(['chapter1', 'chapter2', 'chapter3']);
|
|
148
|
+
const newChapter = added.getChapter('chapter3');
|
|
149
|
+
expect(newChapter?.getTitle()).toBe('Untitled Chapter');
|
|
150
|
+
expect(newChapter?.getText()).toBe('');
|
|
151
|
+
expect(newChapter?.getRef()).toBeUndefined();
|
|
152
|
+
expect(newChapter?.isNumbered()).toBe(true);
|
|
153
|
+
// Fills gaps.
|
|
154
|
+
expect(ids(withChapters(chapter('chapter2')).withNewChapter())).toEqual(['chapter2', 'chapter1']);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
describe('withRevisedChapter', () => {
|
|
158
|
+
test('replaces the edited chapter', () => {
|
|
159
|
+
const a = chapter('a');
|
|
160
|
+
const edition = withChapters(a, chapter('b'));
|
|
161
|
+
const revised = edition.withRevisedChapter(a, a.withTitle('New'));
|
|
162
|
+
expect(revised.getChapter('a')?.getTitle()).toBe('New');
|
|
163
|
+
expect(edition.getChapter('a')?.getTitle()).toBe('A');
|
|
164
|
+
});
|
|
165
|
+
test('rewrites links to a renamed chapter in other chapters', () => {
|
|
166
|
+
const a = chapter('a', { text: 'Hi.' });
|
|
167
|
+
const b = chapter('b', {
|
|
168
|
+
text: 'See [this|a] and [that|a:label] but not [x|ab] or [y|ba].',
|
|
169
|
+
});
|
|
170
|
+
const edition = withChapters(a, b);
|
|
171
|
+
const revised = edition.withRevisedChapter(a, a.withChapterID('z'));
|
|
172
|
+
expect(revised.getChapter('b')?.getText()).toBe('See [this|z] and [that|z:label] but not [x|ab] or [y|ba].');
|
|
173
|
+
expect(ids(revised)).toEqual(['z', 'b']);
|
|
174
|
+
});
|
|
175
|
+
test('rewrites self-links inside the renamed chapter', () => {
|
|
176
|
+
const a = chapter('a', { text: 'See [above|a:top].' });
|
|
177
|
+
const edition = withChapters(a);
|
|
178
|
+
const renamed = a.withChapterID('z');
|
|
179
|
+
const revised = edition.withRevisedChapter(a, renamed);
|
|
180
|
+
expect(revised.getChapter('z')?.getText()).toBe('See [above|z:top].');
|
|
181
|
+
});
|
|
182
|
+
test('treats regex characters in chapter IDs literally', () => {
|
|
183
|
+
const a = chapter('a.b', { text: '' });
|
|
184
|
+
const b = chapter('b', { text: 'See [x|a.b] and [y|aXb].' });
|
|
185
|
+
const revised = withChapters(a, b).withRevisedChapter(a, a.withChapterID('c'));
|
|
186
|
+
expect(revised.getChapter('b')?.getText()).toBe('See [x|c] and [y|aXb].');
|
|
187
|
+
});
|
|
188
|
+
test('leaves chapters with unloaded text alone', () => {
|
|
189
|
+
const a = chapter('a');
|
|
190
|
+
const b = chapter('b', { text: null });
|
|
191
|
+
const revised = withChapters(a, b).withRevisedChapter(a, a.withChapterID('z'));
|
|
192
|
+
expect(revised.getChapter('b')?.getText()).toBeNull();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
describe('refs and numbering', () => {
|
|
196
|
+
test('withoutRefs strips edition and chapter refs', () => {
|
|
197
|
+
const edition = withChapters(chapter('a', { ref: 'r1' }));
|
|
198
|
+
expect(edition.getChapter('a')?.getRef()?.id).toBe('r1');
|
|
199
|
+
const stripped = edition.withoutRefs();
|
|
200
|
+
expect(stripped.getEditionRef()).toBeUndefined();
|
|
201
|
+
expect(stripped.getChapter('a')?.getRef()).toBeUndefined();
|
|
202
|
+
expect(stripped.getChapter('a')?.getText()).toBe('Text of a.');
|
|
203
|
+
expect(edition.getEditionRef()).toBe(editionRef);
|
|
204
|
+
});
|
|
205
|
+
test('withIncrementedEditionNumber', () => {
|
|
206
|
+
const edition = makeEdition();
|
|
207
|
+
expect(edition.withIncrementedEditionNumber().getEditionNumber()).toBe(2);
|
|
208
|
+
expect(edition.getEditionNumber()).toBe(1);
|
|
209
|
+
});
|
|
210
|
+
test('edition labels use the right ordinal suffix', () => {
|
|
211
|
+
const label = (n) => {
|
|
212
|
+
let e = makeEdition();
|
|
213
|
+
for (let i = 1; i < n; i++)
|
|
214
|
+
e = e.withIncrementedEditionNumber();
|
|
215
|
+
return e.getEditionLabel();
|
|
216
|
+
};
|
|
217
|
+
expect([1, 2, 3, 4, 11, 12, 13, 21, 22, 23, 101, 111].map(label)).toEqual([
|
|
218
|
+
'1st',
|
|
219
|
+
'2nd',
|
|
220
|
+
'3rd',
|
|
221
|
+
'4th',
|
|
222
|
+
'11th',
|
|
223
|
+
'12th',
|
|
224
|
+
'13th',
|
|
225
|
+
'21st',
|
|
226
|
+
'22nd',
|
|
227
|
+
'23rd',
|
|
228
|
+
'101st',
|
|
229
|
+
'111th',
|
|
230
|
+
]);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
describe('leases', () => {
|
|
234
|
+
test('withLock, hasLease, getLeasee, isLeased', () => {
|
|
235
|
+
const edition = makeEdition();
|
|
236
|
+
expect(edition.isLeased('ch')).toBe(false);
|
|
237
|
+
const locked = edition.withLock('u1', 'ch', true);
|
|
238
|
+
expect(locked.getLeasee('ch')).toBe('u1');
|
|
239
|
+
expect(locked.hasLease('u1', 'ch')).toBe(true);
|
|
240
|
+
expect(locked.hasLease('u2', 'ch')).toBe(false);
|
|
241
|
+
expect(locked.isLeased('ch')).toBe(true);
|
|
242
|
+
// Doesn't mutate.
|
|
243
|
+
expect(edition.isLeased('ch')).toBe(false);
|
|
244
|
+
const unlocked = locked.withLock('u1', 'ch', false);
|
|
245
|
+
expect(unlocked.isLeased('ch')).toBe(false);
|
|
246
|
+
expect(unlocked.getLeasee('ch')).toBeUndefined();
|
|
247
|
+
expect(locked.isLeased('ch')).toBe(true);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
describe('getInfo', () => {
|
|
251
|
+
test('summarizes the edition with deduplicated chapter uids', () => {
|
|
252
|
+
const edition = withChapters(chapter('a', { uids: ['x', 'y'] }), chapter('b', { uids: ['y', 'z'] })).withEditors(['e']);
|
|
253
|
+
const info = edition.getInfo();
|
|
254
|
+
expect(info).toEqual({
|
|
255
|
+
ref: editionRef,
|
|
256
|
+
summary: '',
|
|
257
|
+
number: 1,
|
|
258
|
+
published: null,
|
|
259
|
+
editionuids: ['e'],
|
|
260
|
+
chapteruids: ['x', 'y', 'z'],
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
test('is undefined without a ref', () => {
|
|
264
|
+
expect(makeEdition().withRef(undefined).getInfo()).toBeUndefined();
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
describe('navigation', () => {
|
|
268
|
+
const edition = withChapters(chapter('a'), chapter('b', { forthcoming: true }), chapter('c'));
|
|
269
|
+
test('getNextChapterID skips forthcoming chapters unless editable', () => {
|
|
270
|
+
expect(edition.getNextChapterID('a', false)).toBe('c');
|
|
271
|
+
expect(edition.getNextChapterID('a', true)).toBe('b');
|
|
272
|
+
expect(edition.getNextChapterID('c', false)).toBe('index');
|
|
273
|
+
expect(edition.getNextChapterID('nope', false)).toBeNull();
|
|
274
|
+
});
|
|
275
|
+
test('getNextChapterID walks the back matter', () => {
|
|
276
|
+
expect(edition.getNextChapterID('cover', false)).toBe('a');
|
|
277
|
+
expect(edition.getNextChapterID('references', false)).toBe('index');
|
|
278
|
+
expect(edition.getNextChapterID('glossary', false)).toBe('index');
|
|
279
|
+
expect(edition.getNextChapterID('index', false)).toBe('search');
|
|
280
|
+
expect(edition.getNextChapterID('search', false)).toBe('media');
|
|
281
|
+
expect(edition.getNextChapterID('media', false)).toBe('cover');
|
|
282
|
+
const withGlossary = edition.withDefinition('t', 'T', 'D', []);
|
|
283
|
+
expect(withGlossary.getNextChapterID('references', false)).toBe('glossary');
|
|
284
|
+
expect(withGlossary.getNextChapterID('c', false)).toBe('glossary');
|
|
285
|
+
const withRefs = edition.withNewReferences([new Reference('r')]);
|
|
286
|
+
expect(withRefs.getNextChapterID('c', false)).toBe('references');
|
|
287
|
+
});
|
|
288
|
+
test('getPreviousChapterID', () => {
|
|
289
|
+
expect(edition.getPreviousChapterID('c', false)).toBe('a');
|
|
290
|
+
expect(edition.getPreviousChapterID('c', true)).toBe('b');
|
|
291
|
+
expect(edition.getPreviousChapterID('a', false)).toBe('cover');
|
|
292
|
+
expect(edition.getPreviousChapterID('nope', false)).toBeNull();
|
|
293
|
+
expect(edition.getPreviousChapterID('references', false)).toBe('c');
|
|
294
|
+
expect(edition.getPreviousChapterID('glossary', false)).toBe('c');
|
|
295
|
+
expect(edition.getPreviousChapterID('index', false)).toBe('c');
|
|
296
|
+
expect(edition.getPreviousChapterID('search', false)).toBe('index');
|
|
297
|
+
expect(edition.getPreviousChapterID('media', false)).toBe('search');
|
|
298
|
+
const withRefs = edition.withNewReferences([new Reference('r')]);
|
|
299
|
+
expect(withRefs.getPreviousChapterID('index', false)).toBe('references');
|
|
300
|
+
expect(withRefs.getPreviousChapterID('glossary', false)).toBe('references');
|
|
301
|
+
});
|
|
302
|
+
test('getChapterNumber counts only numbered chapters', () => {
|
|
303
|
+
const numbered = withChapters(chapter('pre', { numbered: false }), chapter('a'), chapter('mid', { numbered: false }), chapter('b'));
|
|
304
|
+
expect(numbered.getChapterNumber('a')).toBe(1);
|
|
305
|
+
expect(numbered.getChapterNumber('b')).toBe(2);
|
|
306
|
+
expect(numbered.getChapterNumber('pre')).toBeUndefined();
|
|
307
|
+
expect(numbered.getChapterNumber('mid')).toBeUndefined();
|
|
308
|
+
expect(numbered.getChapterNumber('nope')).toBeUndefined();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
describe('misc', () => {
|
|
312
|
+
test('getFootnoteSymbol', () => {
|
|
313
|
+
const edition = makeEdition();
|
|
314
|
+
expect(edition.getFootnoteSymbol(0)).toBe('a');
|
|
315
|
+
expect(edition.getFootnoteSymbol(25)).toBe('z');
|
|
316
|
+
expect(edition.getFootnoteSymbol(26)).toBe('aa');
|
|
317
|
+
expect(edition.getFootnoteSymbol(27)).toBe('ab');
|
|
318
|
+
expect(edition.getFootnoteSymbol(52)).toBe('ba');
|
|
319
|
+
});
|
|
320
|
+
test('getBookReadingTime sums chapters, ignoring forthcoming ones', () => {
|
|
321
|
+
const words = (n) => Array(n).fill('word').join(' ');
|
|
322
|
+
const edition = withChapters(chapter('a', { text: words(300) }), chapter('b', { text: words(150) }), chapter('c', { text: words(3000), forthcoming: true }), chapter('d', { text: null }));
|
|
323
|
+
expect(edition.getBookReadingTime()).toBe(3);
|
|
324
|
+
});
|
|
325
|
+
test('getHeader falls back to defaults', () => {
|
|
326
|
+
const edition = makeEdition();
|
|
327
|
+
expect(edition.getHeader('references')).toBe('References');
|
|
328
|
+
expect(edition.withHeader('references', 'Cited').getHeader('references')).toBe('Cited');
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
test('the book index handles words named like built-in object properties', () => {
|
|
332
|
+
const { makeEdition } = fixtures;
|
|
333
|
+
const edition = makeEdition();
|
|
334
|
+
const chapter = edition.getChapter('chapter1');
|
|
335
|
+
const revised = edition.withRevisedChapter(chapter, chapter.withText('A constructor’s toString, and a constructor.'));
|
|
336
|
+
const index = revised.getBookIndex();
|
|
337
|
+
// Look up by variable: TypeScript, like JavaScript, thinks
|
|
338
|
+
// index.constructor is Object's.
|
|
339
|
+
const chaptersWith = (word) => [...index[word]];
|
|
340
|
+
expect(chaptersWith('constructor')).toEqual(['chapter1']);
|
|
341
|
+
expect(chaptersWith('tostring')).toEqual(['chapter1']);
|
|
342
|
+
});
|
|
@@ -19,14 +19,9 @@ export default class Reference {
|
|
|
19
19
|
this.short = short;
|
|
20
20
|
}
|
|
21
21
|
toText() {
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
' ' +
|
|
26
|
-
this.title +
|
|
27
|
-
' ' +
|
|
28
|
-
this.source +
|
|
29
|
-
(this.summary ? this.summary : ''));
|
|
22
|
+
return [this.authors, this.year, this.title, this.source, this.summary]
|
|
23
|
+
.filter((part) => part.length > 0)
|
|
24
|
+
.join(' ');
|
|
30
25
|
}
|
|
31
26
|
toList() {
|
|
32
27
|
return [
|
|
@@ -73,7 +68,8 @@ export default class Reference {
|
|
|
73
68
|
this.year === ref.year &&
|
|
74
69
|
this.title === ref.title &&
|
|
75
70
|
this.source === ref.source &&
|
|
76
|
-
this.
|
|
71
|
+
this.url === ref.url &&
|
|
72
|
+
this.summary === ref.summary &&
|
|
77
73
|
this.short === ref.short);
|
|
78
74
|
}
|
|
79
75
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, test, expect } from 'vitest';
|
|
2
|
+
import Reference from './Reference';
|
|
3
|
+
const ref = new Reference('Ko2020', 'Ko, A.', '2020', 'Title', 'Source', 'https://x', 'Summary');
|
|
4
|
+
describe('Reference', () => {
|
|
5
|
+
test('lowercases citation IDs', () => {
|
|
6
|
+
expect(ref.citationID).toBe('ko2020');
|
|
7
|
+
expect(ref.withCitationID('ABC').citationID).toBe('abc');
|
|
8
|
+
});
|
|
9
|
+
test('toList is the stored order', () => {
|
|
10
|
+
expect(ref.toList()).toEqual([
|
|
11
|
+
'Ko, A.',
|
|
12
|
+
'2020',
|
|
13
|
+
'Title',
|
|
14
|
+
'Source',
|
|
15
|
+
'https://x',
|
|
16
|
+
'Summary',
|
|
17
|
+
]);
|
|
18
|
+
expect(new Reference('x').toList()).toEqual(['', '', '', '', '', '']);
|
|
19
|
+
});
|
|
20
|
+
test('toText separates every field with a space', () => {
|
|
21
|
+
expect(ref.toText()).toBe('Ko, A. 2020 Title Source Summary');
|
|
22
|
+
expect(new Reference('x', 'A', '2000', 'T', 'S').toText()).toBe('A 2000 T S');
|
|
23
|
+
expect(new Reference('x', 'A', '', 'T').toText()).toBe('A T');
|
|
24
|
+
});
|
|
25
|
+
test('equals compares content, including the URL', () => {
|
|
26
|
+
expect(ref.equals(ref.copy())).toBe(true);
|
|
27
|
+
expect(ref.equals(ref.withCitationID('other'))).toBe(true);
|
|
28
|
+
expect(ref.equals(ref.withAuthors('B'))).toBe(false);
|
|
29
|
+
expect(ref.equals(ref.withYear('2021'))).toBe(false);
|
|
30
|
+
expect(ref.equals(ref.withTitle('T2'))).toBe(false);
|
|
31
|
+
expect(ref.equals(ref.withSource('S2'))).toBe(false);
|
|
32
|
+
expect(ref.equals(ref.withSummary('S2'))).toBe(false);
|
|
33
|
+
expect(ref.equals(ref.withURL('https://y'))).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
test('with* methods do not mutate', () => {
|
|
36
|
+
ref.withTitle('Other');
|
|
37
|
+
expect(ref.title).toBe('Title');
|
|
38
|
+
expect(ref.withURL('u').url).toBe('u');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -3,6 +3,7 @@ import BlockNode from './BlockNode';
|
|
|
3
3
|
import ListNode from './ListNode';
|
|
4
4
|
import type Caret from './Caret';
|
|
5
5
|
import type { CaretRange } from './Caret';
|
|
6
|
+
import ParagraphNode from './ParagraphNode';
|
|
6
7
|
import type { Format, FormatNodeSegmentType } from './FormatNode';
|
|
7
8
|
import FormatNode from './FormatNode';
|
|
8
9
|
import TextNode from './TextNode';
|
|
@@ -46,5 +47,6 @@ export default abstract class BlocksNode extends BlockNode {
|
|
|
46
47
|
withListAsStyle(list: ListNode, numbered: boolean): BlocksNode | undefined;
|
|
47
48
|
withoutAdjacentContent(caret: Caret, next: boolean): Edit | undefined;
|
|
48
49
|
withNodeInserted(caret: Caret, node: Node): Edit;
|
|
50
|
+
withBlocksInsertedInParagraph(paragraph: ParagraphNode, caret: Caret, blocks: BlockNode[]): Edit;
|
|
49
51
|
toHTML(): string;
|
|
50
52
|
}
|
|
@@ -169,13 +169,20 @@ export default class BlocksNode extends BlockNode {
|
|
|
169
169
|
const adjacentFormat = adjacentFormats[adjacentFormats.length - 1];
|
|
170
170
|
return adjacentFormat.getLastCaret();
|
|
171
171
|
}
|
|
172
|
-
|
|
172
|
+
// Find the format the caret is in. Carets in atoms (like footnotes) stay in the atom's format.
|
|
173
|
+
const format = caret.node instanceof TextNode || caret.node instanceof AtomNode
|
|
174
|
+
? caret.node.getFormatRoot(this)
|
|
175
|
+
: undefined;
|
|
173
176
|
if (format === undefined)
|
|
174
177
|
return;
|
|
175
178
|
// Find the format that contains the caret and return the adjacent caret if there is one.
|
|
176
179
|
const adjacentCaretInFormat = format.getAdjacentCaret(caret, next);
|
|
177
180
|
if (adjacentCaretInFormat !== undefined)
|
|
178
181
|
return adjacentCaretInFormat;
|
|
182
|
+
// If we're at the edge of an atom's format, select the atom.
|
|
183
|
+
const parent = format.getParent(this);
|
|
184
|
+
if (parent instanceof AtomNode)
|
|
185
|
+
return { node: parent, index: 0 };
|
|
179
186
|
// If there's no adjacent caret in the root format, find the adjacent format or code.
|
|
180
187
|
const formats = this.getNodes().filter((n) => {
|
|
181
188
|
const parent = n.getParent(this);
|
|
@@ -579,9 +586,9 @@ export default class BlocksNode extends BlockNode {
|
|
|
579
586
|
// For each list in the range, convert all items in range to paragraphs.
|
|
580
587
|
for (let i = 0; i < listRootsInRange.length; i++) {
|
|
581
588
|
const list = listRootsInRange[i];
|
|
589
|
+
// Each item becomes a paragraph; formats inside items stay inside them.
|
|
582
590
|
const paragraphs = list
|
|
583
|
-
.
|
|
584
|
-
.filter((n) => n instanceof FormatNode)
|
|
591
|
+
.getFormats()
|
|
585
592
|
.map((f) => new ParagraphNode(0, f));
|
|
586
593
|
// Insert in reverse order to keep them in order, since the list is our anchor.
|
|
587
594
|
for (let j = paragraphs.length - 1; j >= 0; j--) {
|
|
@@ -847,6 +854,10 @@ export default class BlocksNode extends BlockNode {
|
|
|
847
854
|
const anchor = this.#blocks.find((b) => b.contains(caret.node));
|
|
848
855
|
if (anchor === undefined)
|
|
849
856
|
return;
|
|
857
|
+
// If the caret is in a paragraph, split the paragraph at the caret and put the blocks between the halves,
|
|
858
|
+
// merging the first paragraph inserted into the first half, and the second half into the last paragraph inserted.
|
|
859
|
+
if (anchor instanceof ParagraphNode)
|
|
860
|
+
return this.withBlocksInsertedInParagraph(anchor, caret, blocks);
|
|
850
861
|
let lastInsert = anchor;
|
|
851
862
|
let newBlocks = this;
|
|
852
863
|
while (blocks.length > 0) {
|
|
@@ -900,6 +911,51 @@ export default class BlocksNode extends BlockNode {
|
|
|
900
911
|
}
|
|
901
912
|
return { root: newBlocks, range: { start: newCaret, end: newCaret } };
|
|
902
913
|
}
|
|
914
|
+
withBlocksInsertedInParagraph(paragraph, caret, blocks) {
|
|
915
|
+
const halves = paragraph.split(caret);
|
|
916
|
+
if (halves === undefined)
|
|
917
|
+
return;
|
|
918
|
+
const [before, after] = halves;
|
|
919
|
+
// Merge the first block into the first half if it's a paragraph.
|
|
920
|
+
const remaining = blocks.slice();
|
|
921
|
+
const first = remaining[0];
|
|
922
|
+
const newBlocks = [
|
|
923
|
+
first instanceof ParagraphNode
|
|
924
|
+
? before.withParagraphAppended(remaining.shift())
|
|
925
|
+
: before,
|
|
926
|
+
...remaining,
|
|
927
|
+
];
|
|
928
|
+
// Merge the second half into the last block if it's a paragraph, placing the caret between them.
|
|
929
|
+
const last = newBlocks[newBlocks.length - 1];
|
|
930
|
+
let newCaret;
|
|
931
|
+
if (last instanceof ParagraphNode) {
|
|
932
|
+
const lastCaret = last.getLastCaret();
|
|
933
|
+
if (lastCaret === undefined)
|
|
934
|
+
return;
|
|
935
|
+
const index = caretToIndex(last, lastCaret);
|
|
936
|
+
if (index === undefined)
|
|
937
|
+
return;
|
|
938
|
+
const merged = last.withParagraphAppended(after);
|
|
939
|
+
newBlocks[newBlocks.length - 1] = merged;
|
|
940
|
+
newCaret = indexToCaret(merged, index);
|
|
941
|
+
}
|
|
942
|
+
else {
|
|
943
|
+
newBlocks.push(after);
|
|
944
|
+
newCaret = after.getFirstCaret();
|
|
945
|
+
}
|
|
946
|
+
if (newCaret === undefined)
|
|
947
|
+
return;
|
|
948
|
+
// Replace the paragraph with the new blocks.
|
|
949
|
+
const index = this.#blocks.indexOf(paragraph);
|
|
950
|
+
if (index < 0)
|
|
951
|
+
return;
|
|
952
|
+
const revised = this.#blocks.slice();
|
|
953
|
+
revised.splice(index, 1, ...newBlocks);
|
|
954
|
+
return {
|
|
955
|
+
root: this.create(revised),
|
|
956
|
+
range: { start: newCaret, end: newCaret },
|
|
957
|
+
};
|
|
958
|
+
}
|
|
903
959
|
toHTML() {
|
|
904
960
|
return this.#blocks.map((b) => b.toHTML()).join('');
|
|
905
961
|
}
|
|
@@ -317,7 +317,7 @@ test('Indent/unindent list items', () => {
|
|
|
317
317
|
start: { node: firstTextNode, index: 0 },
|
|
318
318
|
end: { node: firstTextNode, index: 0 },
|
|
319
319
|
}, true)
|
|
320
|
-
?.toBookdown()).toBe('1.. First paragraph.\
|
|
320
|
+
?.toBookdown()).toBe('1.. First paragraph.\n1. Last paragraph.');
|
|
321
321
|
const indented = new ChapterNode([numberedList]).withListsIndented({
|
|
322
322
|
start: { node: firstTextNode, index: 0 },
|
|
323
323
|
end: { node: lastTextNode, index: 0 },
|
|
@@ -522,7 +522,7 @@ test('Copy/paste', () => {
|
|
|
522
522
|
end: { node: afterList, index: 4 },
|
|
523
523
|
});
|
|
524
524
|
// Lists
|
|
525
|
-
expect(partialList?.toBookdown()).toBe(`1.. a\
|
|
525
|
+
expect(partialList?.toBookdown()).toBe(`1.. a\n1. t`);
|
|
526
526
|
expect(partialListAndParagraph?.toBookdown()).toBe(`1. two\n2. three\n\nPost`);
|
|
527
527
|
// Tables
|
|
528
528
|
const partialTable = chapter.copyRange({
|
|
@@ -9,12 +9,21 @@ export function getNavigableNodes(node) {
|
|
|
9
9
|
n instanceof AtomNode ||
|
|
10
10
|
n instanceof LineBreakNode);
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The outermost formats in the node, in document order: paragraphs, list items, table cells, captions.
|
|
14
|
+
* Formats inside other formats (including inside footnotes) belong to their outer format.
|
|
15
|
+
* If the node is a format, it's the only root.
|
|
16
|
+
*/
|
|
12
17
|
function getFormatRoots(node) {
|
|
18
|
+
if (node instanceof FormatNode)
|
|
19
|
+
return [node];
|
|
13
20
|
const roots = [];
|
|
14
|
-
node.traverse((node,
|
|
21
|
+
node.traverse((node, path) => {
|
|
22
|
+
// The path includes the node itself, so check its ancestors.
|
|
15
23
|
if (node instanceof FormatNode &&
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
!path
|
|
25
|
+
.slice(0, -1)
|
|
26
|
+
.some((ancestor) => ancestor instanceof FormatNode))
|
|
18
27
|
roots.push(node);
|
|
19
28
|
});
|
|
20
29
|
return roots;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|