bookish-press 0.11.2 → 0.12.1

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.
Files changed (97) hide show
  1. package/dist/components/chapter/Block.svelte +19 -0
  2. package/dist/components/chapter/Block.svelte.d.ts +1 -0
  3. package/dist/components/chapter/ChapterBody.test.js +79 -0
  4. package/dist/components/chapter/Format.svelte +11 -1
  5. package/dist/components/chapter/Marginal.svelte +2 -1
  6. package/dist/components/chapter/Symbol.svelte +27 -0
  7. package/dist/components/chapter/Symbol.svelte.d.ts +7 -0
  8. package/dist/components/editor/BookishEditor.svelte +41 -153
  9. package/dist/components/editor/Commands.js +45 -44
  10. package/dist/components/editor/Commands.test.d.ts +1 -0
  11. package/dist/components/editor/Commands.test.js +791 -0
  12. package/dist/components/editor/Editing.d.ts +51 -0
  13. package/dist/components/editor/Editing.js +201 -0
  14. package/dist/components/editor/ImageChooser.svelte +8 -5
  15. package/dist/components/editor/ImageChooser.test.d.ts +1 -0
  16. package/dist/components/editor/ImageChooser.test.js +67 -0
  17. package/dist/components/editor/Permissions.svelte +10 -2
  18. package/dist/components/page/BookLoader.svelte +43 -6
  19. package/dist/components/page/BulkReferenceEditor.svelte +2 -2
  20. package/dist/components/page/Chapter.svelte +77 -70
  21. package/dist/components/page/Contexts.d.ts +14 -0
  22. package/dist/components/page/Contexts.js +14 -4
  23. package/dist/components/page/Edition.svelte +2 -10
  24. package/dist/components/page/EditionLoader.svelte +40 -7
  25. package/dist/components/page/Index.svelte +1 -1
  26. package/dist/components/page/Publish.svelte +0 -42
  27. package/dist/components/page/Reference.svelte +2 -2
  28. package/dist/components/page/References.svelte +5 -6
  29. package/dist/components/page/TitlePage.svelte +65 -0
  30. package/dist/components/page/TitlePage.svelte.d.ts +20 -0
  31. package/dist/components/page/UnsavedNotice.svelte +96 -0
  32. package/dist/components/page/UnsavedNotice.svelte.d.ts +7 -0
  33. package/dist/epub/xhtml.js +15 -0
  34. package/dist/models/CRUD.d.ts +28 -12
  35. package/dist/models/CRUD.emu.test.d.ts +1 -0
  36. package/dist/models/CRUD.emu.test.js +250 -0
  37. package/dist/models/CRUD.js +110 -72
  38. package/dist/models/book/Book.js +1 -1
  39. package/dist/models/book/Book.test.d.ts +1 -0
  40. package/dist/models/book/Book.test.js +124 -0
  41. package/dist/models/book/BookMedia.d.ts +5 -0
  42. package/dist/models/book/BookMedia.js +23 -7
  43. package/dist/models/book/BookMedia.test.d.ts +1 -0
  44. package/dist/models/book/BookMedia.test.js +40 -0
  45. package/dist/models/book/Chapter.js +28 -12
  46. package/dist/models/book/Chapter.test.d.ts +1 -0
  47. package/dist/models/book/Chapter.test.js +167 -0
  48. package/dist/models/book/Edition.d.ts +2 -0
  49. package/dist/models/book/Edition.js +34 -25
  50. package/dist/models/book/Edition.test.d.ts +1 -0
  51. package/dist/models/book/Edition.test.js +342 -0
  52. package/dist/models/book/Reference.js +5 -9
  53. package/dist/models/book/Reference.test.d.ts +1 -0
  54. package/dist/models/book/Reference.test.js +40 -0
  55. package/dist/models/book/Theme.js +34 -34
  56. package/dist/models/chapter/BlocksNode.d.ts +2 -0
  57. package/dist/models/chapter/BlocksNode.js +59 -3
  58. package/dist/models/chapter/BlocksNode.test.js +2 -2
  59. package/dist/models/chapter/Caret.js +12 -3
  60. package/dist/models/chapter/Caret.test.d.ts +1 -0
  61. package/dist/models/chapter/Caret.test.js +127 -0
  62. package/dist/models/chapter/ChapterNode.d.ts +3 -0
  63. package/dist/models/chapter/ChapterNode.js +11 -1
  64. package/dist/models/chapter/CommentNode.js +3 -2
  65. package/dist/models/chapter/EmbedNode.js +1 -1
  66. package/dist/models/chapter/FootnoteNode.js +1 -1
  67. package/dist/models/chapter/FormatNode.d.ts +5 -1
  68. package/dist/models/chapter/FormatNode.js +60 -20
  69. package/dist/models/chapter/LabelNode.js +4 -2
  70. package/dist/models/chapter/ListNode.js +11 -13
  71. package/dist/models/chapter/Node.test.js +2 -11
  72. package/dist/models/chapter/Operations.test.d.ts +1 -0
  73. package/dist/models/chapter/Operations.test.js +298 -0
  74. package/dist/models/chapter/ParagraphNode.js +29 -3
  75. package/dist/models/chapter/Parser.d.ts +49 -4
  76. package/dist/models/chapter/Parser.js +210 -88
  77. package/dist/models/chapter/Parser.test.js +31 -0
  78. package/dist/models/chapter/Roundtrip.test.d.ts +1 -0
  79. package/dist/models/chapter/Roundtrip.test.js +313 -0
  80. package/dist/models/chapter/SymbolBlockNode.d.ts +40 -0
  81. package/dist/models/chapter/SymbolBlockNode.js +75 -0
  82. package/dist/models/chapter/SymbolBlockNode.test.d.ts +1 -0
  83. package/dist/models/chapter/SymbolBlockNode.test.js +130 -0
  84. package/dist/models/chapter/SymbolNode.d.ts +32 -0
  85. package/dist/models/chapter/SymbolNode.js +61 -0
  86. package/dist/models/chapter/SymbolNode.test.d.ts +1 -0
  87. package/dist/models/chapter/SymbolNode.test.js +177 -0
  88. package/dist/models/chapter/TableNode.js +2 -2
  89. package/dist/models/chapter/TextNode.d.ts +8 -1
  90. package/dist/models/chapter/TextNode.js +22 -15
  91. package/dist/util/getSubdomain.js +5 -1
  92. package/dist/util/getSubdomain.test.d.ts +1 -0
  93. package/dist/util/getSubdomain.test.js +53 -0
  94. package/dist/util/mineReference.js +12 -5
  95. package/dist/util/mineReference.test.d.ts +1 -0
  96. package/dist/util/mineReference.test.js +62 -0
  97. package/package.json +12 -7
@@ -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
- return this.withChapters(this.chapters.map((chapter) => {
199
- // Is this the edited chapter? Return the edited one.
200
- if (chapter === previous)
201
- return edited;
202
- // If it's a different chapter, did the chapter change it's ID? Change all links to the chapter to have the new ID.
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
- const bookIndex = {};
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
- return `${num}${num === 1 ? 'st' : num === 2 ? 'nd' : num === 3 ? 'rd' : ''}`;
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 {};
@@ -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 (this.authors +
23
- ' ' +
24
- this.year +
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.summary == ref.summary &&
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 {};