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
|
@@ -10,6 +10,17 @@ async function loadStorage() {
|
|
|
10
10
|
]);
|
|
11
11
|
return storage === undefined ? undefined : { storage, ...api };
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* The name to store an uploaded file under: the file's name with a unique suffix
|
|
15
|
+
* before its extension, so uploads with the same name don't collide.
|
|
16
|
+
*/
|
|
17
|
+
export function getUploadName(fileName, unique) {
|
|
18
|
+
// The extension is whatever follows the last dot, unless the dot starts the name (as in .hidden).
|
|
19
|
+
const dot = fileName.lastIndexOf('.');
|
|
20
|
+
const name = dot > 0 ? fileName.substring(0, dot) : fileName;
|
|
21
|
+
const extension = dot > 0 ? fileName.substring(dot + 1) : '';
|
|
22
|
+
return `${name}-${unique}${extension.length > 0 ? `.${extension}` : ''}`;
|
|
23
|
+
}
|
|
13
24
|
export default class BookMedia {
|
|
14
25
|
book;
|
|
15
26
|
_images;
|
|
@@ -75,11 +86,18 @@ export default class BookMedia {
|
|
|
75
86
|
// https://[firebase-domain]/v0/b/[projectid].appspot.com/o/images%2F[bookID]%2F[imagename]?alt=media
|
|
76
87
|
// And we want
|
|
77
88
|
// https://[firebase-domain]/v0/b/[projectid].appspot.com/o/images%2F[bookID]%2Fthumbnails%2F[imagename]?alt=media
|
|
89
|
+
// Find the book ID as a path segment in the object path, so a book ID that happens to
|
|
90
|
+
// appear elsewhere in the URL (such as the project ID) isn't mistaken for it.
|
|
78
91
|
const bookID = this.book.getID();
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
92
|
+
for (const separator of ['%2F', '/']) {
|
|
93
|
+
const segment = `${separator}${bookID}${separator}`;
|
|
94
|
+
const index = url.indexOf(segment);
|
|
95
|
+
if (index >= 0) {
|
|
96
|
+
const end = index + separator.length + bookID.length;
|
|
97
|
+
return `${url.substring(0, end)}${separator}thumbnails${url.substring(end)}`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return url;
|
|
83
101
|
}
|
|
84
102
|
async getThumbnailURL(url, thumbnailRef, error, finished, attempt = 0) {
|
|
85
103
|
const firebase = await loadStorage();
|
|
@@ -102,10 +120,8 @@ export default class BookMedia {
|
|
|
102
120
|
}
|
|
103
121
|
uploadWith(firebase, file, errorHandler, finishedHandler) {
|
|
104
122
|
const { storage, ref, uploadBytes, getDownloadURL } = firebase;
|
|
105
|
-
// Get the extension if there is one.
|
|
106
|
-
const [name, extension] = file.name.split('.');
|
|
107
123
|
// We use the file name given, but append a unique string to avoid collisions.
|
|
108
|
-
const imageName =
|
|
124
|
+
const imageName = getUploadName(file.name, crypto.randomUUID());
|
|
109
125
|
// The canonical path format for Bookish images in the store is image/{bookid}/{imageid}
|
|
110
126
|
// where {bookid} is the Firestore document ID of the book being edited and {imageid} is just a random id.
|
|
111
127
|
const imageRef = ref(storage, `${this.getImagePath()}/${imageName}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, test, expect } from 'vitest';
|
|
2
|
+
import BookMedia, { getUploadName } from './BookMedia';
|
|
3
|
+
import { makeBook } from '../../../tests/fixtures';
|
|
4
|
+
const media = new BookMedia(makeBook());
|
|
5
|
+
describe('getUploadName', () => {
|
|
6
|
+
const id = 'uuid';
|
|
7
|
+
test('keeps the extension', () => {
|
|
8
|
+
expect(getUploadName('photo.jpg', id)).toBe('photo-uuid.jpg');
|
|
9
|
+
});
|
|
10
|
+
test('uses the last dot for the extension', () => {
|
|
11
|
+
expect(getUploadName('my.photo.jpg', id)).toBe('my.photo-uuid.jpg');
|
|
12
|
+
});
|
|
13
|
+
test('omits the dot when there is no extension', () => {
|
|
14
|
+
expect(getUploadName('photo', id)).toBe('photo-uuid');
|
|
15
|
+
});
|
|
16
|
+
test('treats a leading dot as part of the name', () => {
|
|
17
|
+
expect(getUploadName('.hidden', id)).toBe('.hidden-uuid');
|
|
18
|
+
});
|
|
19
|
+
test('handles a trailing dot', () => {
|
|
20
|
+
expect(getUploadName('photo.', id)).toBe('photo-uuid');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe('URLs', () => {
|
|
24
|
+
test('makeThumbnailURL inserts the thumbnails folder after the book ID', () => {
|
|
25
|
+
expect(media.makeThumbnailURL('https://firebasestorage.googleapis.com/v0/b/p.appspot.com/o/images%2Ftestbook%2Fcat.png?alt=media')).toBe('https://firebasestorage.googleapis.com/v0/b/p.appspot.com/o/images%2Ftestbook%2Fthumbnails%2Fcat.png?alt=media');
|
|
26
|
+
});
|
|
27
|
+
test('makeThumbnailURL leaves URLs without the book ID alone', () => {
|
|
28
|
+
expect(media.makeThumbnailURL('https://example.com/cat.png')).toBe('https://example.com/cat.png');
|
|
29
|
+
});
|
|
30
|
+
test('makeThumbnailURL finds the book ID in the image path, not elsewhere', () => {
|
|
31
|
+
expect(media.makeThumbnailURL('https://host/v0/b/testbook.appspot.com/o/images%2Ftestbook%2Fcat.png?alt=media')).toBe('https://host/v0/b/testbook.appspot.com/o/images%2Ftestbook%2Fthumbnails%2Fcat.png?alt=media');
|
|
32
|
+
});
|
|
33
|
+
test('stripTokenFromURL', () => {
|
|
34
|
+
expect(media.stripTokenFromURL('https://x/a?alt=media&token=abc')).toBe('https://x/a?alt=media');
|
|
35
|
+
expect(media.stripTokenFromURL('https://x/a?alt=media')).toBe('https://x/a?alt=media');
|
|
36
|
+
});
|
|
37
|
+
test('getImagePath', () => {
|
|
38
|
+
expect(media.getImagePath()).toBe('images/testbook/');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -160,10 +160,11 @@ export default class Chapter {
|
|
|
160
160
|
return this._ast;
|
|
161
161
|
}
|
|
162
162
|
getWordCount(edition) {
|
|
163
|
-
if (this._wordCount === undefined)
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
.
|
|
163
|
+
if (this._wordCount === undefined) {
|
|
164
|
+
const text = this.getAST(edition)?.toText();
|
|
165
|
+
if (text !== undefined)
|
|
166
|
+
this._wordCount = text.match(/\S+/g)?.length ?? 0;
|
|
167
|
+
}
|
|
167
168
|
return this._wordCount;
|
|
168
169
|
}
|
|
169
170
|
/** Either a number of minutes, or undefined if there's no text yet, or null if forthcoming. */
|
|
@@ -185,22 +186,37 @@ export default class Chapter {
|
|
|
185
186
|
CommonWords.forEach((word) => commonWords.add(word));
|
|
186
187
|
// Get all the text in the chapter.
|
|
187
188
|
let text = ast.toText();
|
|
188
|
-
// Split
|
|
189
|
-
|
|
189
|
+
// Split into alternating words and non-words, keeping both, since we keep non-words for context.
|
|
190
|
+
// Apostrophes are part of words, so contractions stay whole; letters in any script count.
|
|
191
|
+
const words = text
|
|
192
|
+
.split(/([\p{L}\p{M}\p{N}_\u2019']+)/u)
|
|
193
|
+
.filter((token) => token !== '');
|
|
190
194
|
// Index the words
|
|
191
|
-
|
|
195
|
+
// No prototype: book words like "constructor" or "toString"
|
|
196
|
+
// must not find Object's built-in properties.
|
|
197
|
+
const index = Object.create(null);
|
|
192
198
|
words.forEach((word, wordNumber) => {
|
|
193
199
|
// Skip non words. We keep them for search results.
|
|
194
|
-
if (
|
|
200
|
+
if (!/\p{L}/u.test(word))
|
|
195
201
|
return;
|
|
196
202
|
word = word.toLowerCase();
|
|
203
|
+
// Index possessives under the word they belong to
|
|
204
|
+
// (children’s, students’ → children, students), and drop
|
|
205
|
+
// apostrophes that are really closing quotes (‘hello’).
|
|
206
|
+
const possessive = word.match(/^(.+?)['\u2019]s$/u) ??
|
|
207
|
+
word.match(/^(.+s)['\u2019]$/u);
|
|
208
|
+
if (possessive)
|
|
209
|
+
word = possessive[1];
|
|
210
|
+
else
|
|
211
|
+
word = word.replace(/^['\u2019]+|['\u2019]+$/gu, '');
|
|
197
212
|
// Should we include?
|
|
198
213
|
// • It shouldn't be a common word
|
|
199
|
-
// • It shouldn't
|
|
214
|
+
// • It shouldn't be a contraction (they’re, aren’t); names
|
|
215
|
+
// with apostrophes, like D’Ignazio, are fine
|
|
200
216
|
// • It should be longer than two letters
|
|
201
217
|
// • It shouldn't end in 'ly'
|
|
202
218
|
if (!commonWords.has(word) &&
|
|
203
|
-
|
|
219
|
+
!/['\u2019](t|re|ll|ve|d|m)$/u.test(word) &&
|
|
204
220
|
word.length > 2 &&
|
|
205
221
|
word.substring(word.length - 2, word.length) !== 'ly') {
|
|
206
222
|
// If we haven't started a list of occurrences, start one.
|
|
@@ -208,11 +224,11 @@ export default class Chapter {
|
|
|
208
224
|
index[word] = [];
|
|
209
225
|
let match = {
|
|
210
226
|
left: words
|
|
211
|
-
.slice(Math.max(0, wordNumber - 10),
|
|
227
|
+
.slice(Math.max(0, wordNumber - 10), wordNumber)
|
|
212
228
|
.join(''),
|
|
213
229
|
match: words[wordNumber],
|
|
214
230
|
right: words
|
|
215
|
-
.slice(
|
|
231
|
+
.slice(wordNumber + 1, wordNumber + 11)
|
|
216
232
|
.join(''),
|
|
217
233
|
};
|
|
218
234
|
// Add the occurrence, but a lower case canonical version.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, test, expect } from 'vitest';
|
|
2
|
+
import Chapter from './Chapter';
|
|
3
|
+
import { makeEdition } from '../../../tests/fixtures';
|
|
4
|
+
function chapter(text, forthcoming = false, authors = []) {
|
|
5
|
+
return new Chapter(undefined, 'c', 'Title', authors, null, true, forthcoming, undefined, text, []);
|
|
6
|
+
}
|
|
7
|
+
const edition = makeEdition();
|
|
8
|
+
describe('getIndex', () => {
|
|
9
|
+
test('indexes uncommon words with surrounding context', () => {
|
|
10
|
+
const index = chapter('Zebras graze. Zebras run.').getIndex(edition);
|
|
11
|
+
expect(Object.keys(index).toSorted()).toEqual([
|
|
12
|
+
'graze',
|
|
13
|
+
'run',
|
|
14
|
+
'zebras',
|
|
15
|
+
]);
|
|
16
|
+
expect(index.zebras.length).toBe(2);
|
|
17
|
+
});
|
|
18
|
+
test('left context excludes the matched word, even at the start', () => {
|
|
19
|
+
const index = chapter('Zebras graze quietly.').getIndex(edition);
|
|
20
|
+
const [first] = index.zebras;
|
|
21
|
+
expect(first.match).toBe('Zebras');
|
|
22
|
+
expect(first.left).toBe('');
|
|
23
|
+
expect(first.right).toBe(' graze quietly.');
|
|
24
|
+
const [graze] = index.graze;
|
|
25
|
+
expect(graze.left).toBe('Zebras ');
|
|
26
|
+
expect(graze.match).toBe('graze');
|
|
27
|
+
});
|
|
28
|
+
test('right context excludes the matched word, even at the end', () => {
|
|
29
|
+
const index = chapter('Look at zebras').getIndex(edition);
|
|
30
|
+
const [last] = index.zebras;
|
|
31
|
+
expect(last.left).toBe('Look at ');
|
|
32
|
+
expect(last.match).toBe('zebras');
|
|
33
|
+
expect(last.right).toBe('');
|
|
34
|
+
});
|
|
35
|
+
test('context is limited to ten tokens on either side', () => {
|
|
36
|
+
const words = 'one two three four five six seven eight nine ten eleven twelve target thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty';
|
|
37
|
+
const [match] = chapter(words).getIndex(edition).target;
|
|
38
|
+
expect(match.left).toBe('eight nine ten eleven twelve ');
|
|
39
|
+
expect(match.right).toBe(' thirteen fourteen fifteen sixteen seventeen');
|
|
40
|
+
});
|
|
41
|
+
test('skips common, short, -ly, and contracted words', () => {
|
|
42
|
+
const index = chapter('The cat quickly went to the store, didn’t it?').getIndex(edition);
|
|
43
|
+
expect(Object.keys(index).toSorted()).toEqual(['cat', 'store']);
|
|
44
|
+
});
|
|
45
|
+
test('indexes possessives under the word they belong to', () => {
|
|
46
|
+
// Keeping apostrophes in words (so contractions aren't split into
|
|
47
|
+
// fragments like "didn") must not drop possessives from the index.
|
|
48
|
+
const index = chapter("A children’s librarian, Apple’s software, and the students’ work. Also James's book.").getIndex(edition);
|
|
49
|
+
expect(Object.keys(index)).toEqual(expect.arrayContaining([
|
|
50
|
+
'children',
|
|
51
|
+
'apple',
|
|
52
|
+
'students',
|
|
53
|
+
'james',
|
|
54
|
+
'librarian',
|
|
55
|
+
]));
|
|
56
|
+
// The match shows the word as written.
|
|
57
|
+
expect(index.children[0].match).toBe('children’s');
|
|
58
|
+
});
|
|
59
|
+
test('indexes quoted words and names with apostrophes, but not contractions', () => {
|
|
60
|
+
const index = chapter("Also called ‘motherboard’, said ‘hello’ to D’Ignazio and O'Leary, but they’re and we’ll aren’t words.").getIndex(edition);
|
|
61
|
+
const words = Object.keys(index);
|
|
62
|
+
expect(words).toEqual(expect.arrayContaining([
|
|
63
|
+
'motherboard',
|
|
64
|
+
'hello',
|
|
65
|
+
'd’ignazio',
|
|
66
|
+
'o’leary', // the parser curls straight apostrophes
|
|
67
|
+
]));
|
|
68
|
+
for (const contraction of [
|
|
69
|
+
'they’re',
|
|
70
|
+
'we’ll',
|
|
71
|
+
'aren’t',
|
|
72
|
+
'they',
|
|
73
|
+
'aren',
|
|
74
|
+
])
|
|
75
|
+
expect(words).not.toContain(contraction);
|
|
76
|
+
});
|
|
77
|
+
test('indexes words that are also names of built-in object properties', () => {
|
|
78
|
+
// The index was a plain object, so "constructor" found an inherited
|
|
79
|
+
// function instead of a list, and the index page crashed.
|
|
80
|
+
const index = chapter('The constructor’s toString and valueOf, then hasOwnProperty and __proto__ and constructor again.').getIndex(edition);
|
|
81
|
+
expect(index.constructor).toHaveLength(2);
|
|
82
|
+
expect(index.tostring).toHaveLength(1);
|
|
83
|
+
expect(index.valueof).toHaveLength(1);
|
|
84
|
+
expect(index.hasownproperty).toHaveLength(1);
|
|
85
|
+
});
|
|
86
|
+
test('is empty when the text is not loaded', () => {
|
|
87
|
+
expect(chapter(null).getIndex(edition)).toEqual({});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe('word counts and reading time', () => {
|
|
91
|
+
test('counts words', () => {
|
|
92
|
+
expect(chapter('One two three.').getWordCount(edition)).toBe(3);
|
|
93
|
+
});
|
|
94
|
+
test('ignores leading, trailing, and repeated whitespace', () => {
|
|
95
|
+
expect(chapter(' One two ').getWordCount(edition)).toBe(2);
|
|
96
|
+
expect(chapter('# Header\n\nOne two').getWordCount(edition)).toBe(3);
|
|
97
|
+
});
|
|
98
|
+
test('an empty chapter has no words', () => {
|
|
99
|
+
expect(chapter('').getWordCount(edition)).toBe(0);
|
|
100
|
+
});
|
|
101
|
+
test('is undefined when the text is not loaded', () => {
|
|
102
|
+
expect(chapter(null).getWordCount(edition)).toBeUndefined();
|
|
103
|
+
expect(chapter(null).getReadingTime(edition)).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
test('reading time is at least a minute at 150 words per minute', () => {
|
|
106
|
+
const words = (n) => Array(n).fill('word').join(' ');
|
|
107
|
+
expect(chapter(words(10)).getReadingTime(edition)).toBe(1);
|
|
108
|
+
expect(chapter(words(300)).getReadingTime(edition)).toBe(2);
|
|
109
|
+
expect(chapter(words(1000)).getReadingTime(edition)).toBe(7);
|
|
110
|
+
expect(chapter(words(300), true).getReadingTime(edition)).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
describe('authors', () => {
|
|
114
|
+
const c = chapter('', false, ['A', 'B', 'C']);
|
|
115
|
+
test('withRenamedAuthor', () => {
|
|
116
|
+
expect(c.withRenamedAuthor(1, 'Z').getAuthors()).toEqual([
|
|
117
|
+
'A',
|
|
118
|
+
'Z',
|
|
119
|
+
'C',
|
|
120
|
+
]);
|
|
121
|
+
expect(c.withRenamedAuthor(1, 'B')).toBe(c);
|
|
122
|
+
expect(c.withRenamedAuthor(-1, 'Z')).toBe(c);
|
|
123
|
+
expect(c.withRenamedAuthor(3, 'Z')).toBe(c);
|
|
124
|
+
expect(c.getAuthors()).toEqual(['A', 'B', 'C']);
|
|
125
|
+
});
|
|
126
|
+
test('withoutAuthor', () => {
|
|
127
|
+
expect(c.withoutAuthor(0).getAuthors()).toEqual(['B', 'C']);
|
|
128
|
+
expect(c.withoutAuthor(2).getAuthors()).toEqual(['A', 'B']);
|
|
129
|
+
expect(c.withoutAuthor(3)).toBe(c);
|
|
130
|
+
expect(c.withoutAuthor(-1)).toBe(c);
|
|
131
|
+
expect(c.getAuthors()).toEqual(['A', 'B', 'C']);
|
|
132
|
+
});
|
|
133
|
+
test('withAuthor appends without mutating', () => {
|
|
134
|
+
expect(c.withAuthor('D').getAuthors()).toEqual(['A', 'B', 'C', 'D']);
|
|
135
|
+
expect(c.getAuthors()).toEqual(['A', 'B', 'C']);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe('toJSON/fromJSON', () => {
|
|
139
|
+
test('round trips metadata but not text', () => {
|
|
140
|
+
const c = new Chapter(undefined, 'id', 'T', ['A'], 'img', false, true, 'S', 'text', ['u']);
|
|
141
|
+
const json = c.toJSON();
|
|
142
|
+
expect(json).toEqual({
|
|
143
|
+
id: 'id',
|
|
144
|
+
title: 'T',
|
|
145
|
+
authors: ['A'],
|
|
146
|
+
image: 'img',
|
|
147
|
+
numbered: false,
|
|
148
|
+
forthcoming: true,
|
|
149
|
+
section: 'S',
|
|
150
|
+
uids: ['u'],
|
|
151
|
+
});
|
|
152
|
+
const restored = Chapter.fromJSON(json);
|
|
153
|
+
expect(restored.toJSON()).toEqual(json);
|
|
154
|
+
expect(restored.getText()).toBeNull();
|
|
155
|
+
});
|
|
156
|
+
test('defaults numbered to true', () => {
|
|
157
|
+
const restored = Chapter.fromJSON({
|
|
158
|
+
id: 'id',
|
|
159
|
+
title: 'T',
|
|
160
|
+
authors: [],
|
|
161
|
+
image: null,
|
|
162
|
+
uids: [],
|
|
163
|
+
});
|
|
164
|
+
expect(restored.isNumbered()).toBe(true);
|
|
165
|
+
expect(restored.isForthcoming()).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -90,6 +90,8 @@ export default class Edition {
|
|
|
90
90
|
withChapters(chapters: Chapter[]): Edition;
|
|
91
91
|
withChapterText(textByID: Map<string, string>): Edition;
|
|
92
92
|
withRevisedChapter(previous: Chapter, edited: Chapter): Edition;
|
|
93
|
+
/** Replace links to the chapter with the given ID with links to the new ID. */
|
|
94
|
+
static withChapterLinksRenamed(text: string, from: string, to: string): string;
|
|
93
95
|
withoutRefs(): Edition;
|
|
94
96
|
withIncrementedEditionNumber(): Edition;
|
|
95
97
|
withNewChapter(): Edition;
|
|
@@ -75,7 +75,7 @@ export default class Edition {
|
|
|
75
75
|
else {
|
|
76
76
|
// APA Format. Could eventually support multiple formats.
|
|
77
77
|
let [authors, year, title, source, url, summary] = ref;
|
|
78
|
-
if (source.charAt(0) === '#') {
|
|
78
|
+
if (source !== undefined && source.charAt(0) === '#') {
|
|
79
79
|
let src = edition.getSource(source);
|
|
80
80
|
if (src !== null)
|
|
81
81
|
source = src;
|
|
@@ -195,29 +195,24 @@ export default class Edition {
|
|
|
195
195
|
return this.withChapters(newChapters);
|
|
196
196
|
}
|
|
197
197
|
withRevisedChapter(previous, edited) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (previous.id !== edited.id) {
|
|
204
|
-
// Start with the chapter as is.
|
|
205
|
-
let newChapter = chapter;
|
|
206
|
-
// Replace the chapter ID in the chapter's text.
|
|
207
|
-
if (newChapter.text !== null) {
|
|
208
|
-
const revisedText = newChapter.text.replaceAll(new RegExp(`\\|(${previous.id})(:[a-zA-Z0-9]+)?\\]`, 'g'), `|${edited.id}$2]`);
|
|
209
|
-
if (revisedText !== chapter.text) {
|
|
210
|
-
newChapter = newChapter.withText(revisedText);
|
|
211
|
-
console.log('Updated chapter text: ' + newChapter.text);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
// Return the possibily revised chapter.
|
|
215
|
-
return newChapter;
|
|
216
|
-
}
|
|
217
|
-
// Otherwise, just return the chapter as is.
|
|
218
|
-
else
|
|
198
|
+
// If the chapter's ID changed, links to it (|id] and |id:label]) need the new ID,
|
|
199
|
+
// in every chapter, including the renamed chapter's links to itself.
|
|
200
|
+
const renamed = previous.id !== edited.id;
|
|
201
|
+
const relink = (chapter) => {
|
|
202
|
+
if (!renamed || chapter.text === null)
|
|
219
203
|
return chapter;
|
|
220
|
-
|
|
204
|
+
const revisedText = Edition.withChapterLinksRenamed(chapter.text, previous.id, edited.id);
|
|
205
|
+
return revisedText === chapter.text
|
|
206
|
+
? chapter
|
|
207
|
+
: chapter.withText(revisedText);
|
|
208
|
+
};
|
|
209
|
+
return this.withChapters(this.chapters.map((chapter) => relink(chapter === previous ? edited : chapter)));
|
|
210
|
+
}
|
|
211
|
+
/** Replace links to the chapter with the given ID with links to the new ID. */
|
|
212
|
+
static withChapterLinksRenamed(text, from, to) {
|
|
213
|
+
// Escape the ID, so that characters with special meaning in regular expressions match literally.
|
|
214
|
+
const escaped = from.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
215
|
+
return text.replaceAll(new RegExp(`\\|${escaped}(:[a-zA-Z0-9]+)?\\]`, 'g'), (_, label) => `|${to}${label ?? ''}]`);
|
|
221
216
|
}
|
|
222
217
|
withoutRefs() {
|
|
223
218
|
return this.withRef(undefined).withChapters(this.chapters.map((chap) => chap.withoutRef()));
|
|
@@ -249,6 +244,8 @@ export default class Edition {
|
|
|
249
244
|
withMovedChapter(chapterID, increment) {
|
|
250
245
|
// Get the index of the chapter.
|
|
251
246
|
let index = this.chapters.findIndex((chapter) => chapter.getID() === chapterID);
|
|
247
|
+
if (index < 0)
|
|
248
|
+
return this;
|
|
252
249
|
if (index + increment < 0 || index + increment >= this.chapters.length)
|
|
253
250
|
return this;
|
|
254
251
|
const newChapters = this.chapters.slice();
|
|
@@ -493,7 +490,8 @@ export default class Edition {
|
|
|
493
490
|
}
|
|
494
491
|
getBookIndex() {
|
|
495
492
|
// A map from word to a map of chapters containing the word
|
|
496
|
-
|
|
493
|
+
// No prototype, so words like "constructor" are just words.
|
|
494
|
+
const bookIndex = Object.create(null);
|
|
497
495
|
// Construct the index by building a dictionary of chapters in which each word appears.
|
|
498
496
|
this.chapters.forEach((chapter) => {
|
|
499
497
|
let index = chapter.getIndex(this);
|
|
@@ -674,7 +672,18 @@ export default class Edition {
|
|
|
674
672
|
}
|
|
675
673
|
getEditionLabel() {
|
|
676
674
|
const num = this.number;
|
|
677
|
-
|
|
675
|
+
const lastTwo = Math.abs(num) % 100;
|
|
676
|
+
const last = Math.abs(num) % 10;
|
|
677
|
+
const suffix = lastTwo >= 11 && lastTwo <= 13
|
|
678
|
+
? 'th'
|
|
679
|
+
: last === 1
|
|
680
|
+
? 'st'
|
|
681
|
+
: last === 2
|
|
682
|
+
? 'nd'
|
|
683
|
+
: last === 3
|
|
684
|
+
? 'rd'
|
|
685
|
+
: 'th';
|
|
686
|
+
return `${num}${suffix}`;
|
|
678
687
|
}
|
|
679
688
|
getInfo() {
|
|
680
689
|
return this.editionRef === undefined
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|