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,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
+ });
@@ -13,12 +13,12 @@ export const BookishTheme = {
13
13
  borderColorBold: '#000000',
14
14
  // Foreground colors
15
15
  paragraphColor: '#000000',
16
- mutedColor: '#9AA1A7',
16
+ mutedColor: '#6C747C',
17
17
  highlightColor: '#E8AF22',
18
18
  commentColor: '#257F31',
19
19
  errorColor: '#721C24',
20
20
  linkColor: '#1B499C',
21
- bulletColor: '#EB2C27',
21
+ bulletColor: '#E31A15',
22
22
  },
23
23
  dark: {
24
24
  // Background colors;
@@ -30,12 +30,12 @@ export const BookishTheme = {
30
30
  borderColorBold: '#DADADA',
31
31
  // Foreground colors
32
32
  paragraphColor: '#DADADA',
33
- mutedColor: '#666666',
33
+ mutedColor: '#9C9C9C',
34
34
  highlightColor: '#c5a248',
35
- commentColor: '#1c4722',
35
+ commentColor: '#45AF54',
36
36
  errorColor: '#F8D7DA',
37
37
  linkColor: '#73a3fa',
38
- bulletColor: '#721C24',
38
+ bulletColor: '#E07F88',
39
39
  },
40
40
  // Fonts
41
41
  fonts: {
@@ -92,7 +92,7 @@ export const SeriousTheme = {
92
92
  borderColorBold: '#000000',
93
93
  // Foreground colors
94
94
  paragraphColor: '#000000',
95
- mutedColor: '#9AA1A7',
95
+ mutedColor: '#6C747C',
96
96
  highlightColor: '#bb4242',
97
97
  commentColor: '#257F31',
98
98
  errorColor: '#721C24',
@@ -109,12 +109,12 @@ export const SeriousTheme = {
109
109
  borderColorBold: '#DADADA',
110
110
  // Foreground colors
111
111
  paragraphColor: '#DADADA',
112
- mutedColor: '#666666',
112
+ mutedColor: '#9C9C9C',
113
113
  highlightColor: '#b36363',
114
- commentColor: '#1c4722',
114
+ commentColor: '#45AF54',
115
115
  errorColor: '#F8D7DA',
116
- linkColor: '#b36363',
117
- bulletColor: '#b36363',
116
+ linkColor: '#C78C8C',
117
+ bulletColor: '#C78C8C',
118
118
  },
119
119
  // Fonts
120
120
  fonts: {
@@ -171,12 +171,12 @@ export const TechTheme = {
171
171
  borderColorBold: '#000000',
172
172
  // Foreground colors
173
173
  paragraphColor: '#000000',
174
- mutedColor: '#9AA1A7',
174
+ mutedColor: '#6C747C',
175
175
  highlightColor: '#6480ff',
176
176
  commentColor: '#257F31',
177
177
  errorColor: '#721C24',
178
- linkColor: '#6480ff',
179
- bulletColor: '#6480ff',
178
+ linkColor: '#4063FF',
179
+ bulletColor: '#4063FF',
180
180
  },
181
181
  dark: {
182
182
  // Background colors;
@@ -188,12 +188,12 @@ export const TechTheme = {
188
188
  borderColorBold: '#DADADA',
189
189
  // Foreground colors
190
190
  paragraphColor: '#DADADA',
191
- mutedColor: '#959595',
191
+ mutedColor: '#9B9B9B',
192
192
  highlightColor: '#7589e6',
193
193
  commentColor: '#2bb33e',
194
194
  errorColor: '#F8D7DA',
195
- linkColor: '#7589e6',
196
- bulletColor: '#7589e6',
195
+ linkColor: '#8496E9',
196
+ bulletColor: '#8496E9',
197
197
  },
198
198
  // Fonts
199
199
  fonts: {
@@ -250,12 +250,12 @@ export const HumanTheme = {
250
250
  borderColorBold: '#000000',
251
251
  // Foreground colors
252
252
  paragraphColor: '#000000',
253
- mutedColor: '#9AA1A7',
253
+ mutedColor: '#6C747C',
254
254
  highlightColor: '#3b92e4',
255
255
  commentColor: '#257F31',
256
256
  errorColor: '#721C24',
257
- linkColor: '#3b92e4',
258
- bulletColor: '#3b92e4',
257
+ linkColor: '#1C75C9',
258
+ bulletColor: '#1C75C9',
259
259
  },
260
260
  dark: {
261
261
  // Background colors;
@@ -267,12 +267,12 @@ export const HumanTheme = {
267
267
  borderColorBold: '#DADADA',
268
268
  // Foreground colors
269
269
  paragraphColor: '#DADADA',
270
- mutedColor: '#666666',
270
+ mutedColor: '#9C9C9C',
271
271
  highlightColor: '#2b8ae3',
272
- commentColor: '#1c4722',
272
+ commentColor: '#45AF54',
273
273
  errorColor: '#F8D7DA',
274
- linkColor: '#2b8ae3',
275
- bulletColor: '#2b8ae3',
274
+ linkColor: '#54A0E8',
275
+ bulletColor: '#54A0E8',
276
276
  },
277
277
  // Fonts
278
278
  fonts: {
@@ -329,12 +329,12 @@ export const SketchyTheme = {
329
329
  borderColorBold: '#000000',
330
330
  // Foreground colors
331
331
  paragraphColor: '#000000',
332
- mutedColor: '#9AA1A7',
332
+ mutedColor: '#6C747C',
333
333
  highlightColor: '#dbb13e',
334
334
  commentColor: '#257F31',
335
335
  errorColor: '#721C24',
336
- linkColor: '#dbb13e',
337
- bulletColor: '#dbb13e',
336
+ linkColor: '#8E6F1B',
337
+ bulletColor: '#8E6F1B',
338
338
  },
339
339
  dark: {
340
340
  // Background colors;
@@ -346,12 +346,12 @@ export const SketchyTheme = {
346
346
  borderColorBold: '#DADADA',
347
347
  // Foreground colors
348
348
  paragraphColor: '#DADADA',
349
- mutedColor: '#666666',
349
+ mutedColor: '#9C9C9C',
350
350
  highlightColor: '#9c7e2c',
351
- commentColor: '#1c4722',
351
+ commentColor: '#45AF54',
352
352
  errorColor: '#F8D7DA',
353
- linkColor: '#9c7e2c',
354
- bulletColor: '#9c7e2c',
353
+ linkColor: '#BA9634',
354
+ bulletColor: '#BA9634',
355
355
  },
356
356
  // Fonts
357
357
  fonts: {
@@ -411,12 +411,12 @@ export const CriticalTheme = {
411
411
  borderColorBold: '#000000',
412
412
  // Foreground colors
413
413
  paragraphColor: '#000000',
414
- mutedColor: '#9AA1A7',
414
+ mutedColor: '#6C747C',
415
415
  highlightColor: '#E8AF22',
416
416
  commentColor: '#257F31',
417
417
  errorColor: '#721C24',
418
418
  linkColor: '#1B499C',
419
- bulletColor: '#EB2C27',
419
+ bulletColor: '#E31A15',
420
420
  },
421
421
  dark: {
422
422
  // Background colors;
@@ -430,10 +430,10 @@ export const CriticalTheme = {
430
430
  paragraphColor: '#DADADA',
431
431
  mutedColor: '#CCCCCC',
432
432
  highlightColor: '#5c4d28',
433
- commentColor: '#1c4722',
433
+ commentColor: '#45AF54',
434
434
  errorColor: '#F8D7DA',
435
435
  linkColor: '#73a3fa',
436
- bulletColor: '#721C24',
436
+ bulletColor: '#E07F88',
437
437
  },
438
438
  // Fonts
439
439
  fonts: {
@@ -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
- const format = caret.node.getFarthestParentMatching(this, (n) => n instanceof FormatNode);
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
- .getNodes()
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.\n2. Last 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\n2. t`);
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, parents) => {
21
+ node.traverse((node, path) => {
22
+ // The path includes the node itself, so check its ancestors.
15
23
  if (node instanceof FormatNode &&
16
- parents.length > 0 &&
17
- !(parents[parents.length - 1] instanceof FormatNode))
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 {};
@@ -0,0 +1,127 @@
1
+ import { describe, test, expect } from 'vitest';
2
+ import Parser from './Parser';
3
+ import TextNode from './TextNode';
4
+ import AtomNode from './AtomNode';
5
+ import LineBreakNode from './LineBreakNode';
6
+ import FootnoteNode from './FootnoteNode';
7
+ import { caretToIndex, indexToCaret, caretRangeToIndexRange, indexRangeToCaretRange, getNavigableNodes, } from './Caret';
8
+ const chapter = Parser.parseChapter(undefined, 'First *bold* and _italic_ text.\n\nA footnote{inside _it_} here.\n\n* one\n* two\n\nLine↩break');
9
+ function allCarets() {
10
+ const carets = [];
11
+ for (const node of chapter.getNodes()) {
12
+ if (node instanceof TextNode)
13
+ for (let i = 0; i <= node.getLength(); i++)
14
+ carets.push({ node, index: i });
15
+ else if (node instanceof AtomNode)
16
+ carets.push({ node, index: 0 });
17
+ }
18
+ return carets;
19
+ }
20
+ describe('getNavigableNodes', () => {
21
+ test('includes text, atoms, and line breaks in document order', () => {
22
+ const nodes = getNavigableNodes(chapter);
23
+ expect(nodes.some((n) => n instanceof FootnoteNode)).toBe(true);
24
+ expect(nodes.some((n) => n instanceof LineBreakNode)).toBe(true);
25
+ expect(nodes.every((n) => n instanceof TextNode ||
26
+ n instanceof AtomNode ||
27
+ n instanceof LineBreakNode)).toBe(true);
28
+ const text = nodes
29
+ .filter((n) => n instanceof TextNode)
30
+ .map((n) => n.getText())
31
+ .join('');
32
+ expect(text.startsWith('First bold and italic text.')).toBe(true);
33
+ });
34
+ });
35
+ describe('caretToIndex and indexToCaret', () => {
36
+ test('every caret maps to an index that maps back to the same index', () => {
37
+ for (const caret of allCarets()) {
38
+ const index = caretToIndex(chapter, caret);
39
+ expect(index).toBeDefined();
40
+ if (index === undefined)
41
+ continue;
42
+ const back = indexToCaret(chapter, index);
43
+ expect(back).toBeDefined();
44
+ if (back === undefined)
45
+ continue;
46
+ expect(caretToIndex(chapter, back)).toBe(index);
47
+ }
48
+ });
49
+ test('indexes increase through the document', () => {
50
+ const indexes = allCarets()
51
+ .filter((c) => c.node instanceof TextNode)
52
+ .filter((c) => c.node.getClosestParentOfType(chapter, FootnoteNode) ===
53
+ undefined)
54
+ .map((c) => caretToIndex(chapter, c));
55
+ for (let i = 1; i < indexes.length; i++)
56
+ expect(indexes[i]).toBeGreaterThanOrEqual(indexes[i - 1]);
57
+ });
58
+ test('the first caret is index 0', () => {
59
+ const first = chapter.getFirstCaret();
60
+ expect(first).toBeDefined();
61
+ if (first)
62
+ expect(caretToIndex(chapter, first)).toBe(0);
63
+ expect(indexToCaret(chapter, 0)?.node).toBe(first?.node);
64
+ });
65
+ test('a caret in another tree has no index', () => {
66
+ expect(caretToIndex(chapter, { node: new TextNode('x'), index: 0 })).toBeUndefined();
67
+ });
68
+ test('an index past the end has no caret', () => {
69
+ expect(indexToCaret(chapter, 100000)).toBeUndefined();
70
+ });
71
+ test('indexes survive reparsing the same bookdown', () => {
72
+ const reparsed = Parser.parseChapter(undefined, chapter.toBookdown());
73
+ for (const caret of allCarets()) {
74
+ const index = caretToIndex(chapter, caret);
75
+ if (index === undefined)
76
+ continue;
77
+ const there = indexToCaret(reparsed, index);
78
+ expect(there).toBeDefined();
79
+ if (there === undefined)
80
+ continue;
81
+ expect(caretToIndex(reparsed, there)).toBe(index);
82
+ }
83
+ });
84
+ });
85
+ describe('range conversions', () => {
86
+ test('round trip a range', () => {
87
+ const texts = chapter.getTextNodes();
88
+ const range = {
89
+ start: { node: texts[0], index: 2 },
90
+ end: { node: texts[texts.length - 1], index: 3 },
91
+ };
92
+ const indexes = caretRangeToIndexRange(chapter, range);
93
+ expect(indexes).toBeDefined();
94
+ if (indexes === undefined)
95
+ return;
96
+ expect(indexes.end).toBeGreaterThan(indexes.start);
97
+ const back = indexRangeToCaretRange(chapter, indexes);
98
+ expect(back?.start).toEqual(range.start);
99
+ expect(back?.end).toEqual(range.end);
100
+ });
101
+ test('fails when either end is not in the tree', () => {
102
+ const texts = chapter.getTextNodes();
103
+ expect(caretRangeToIndexRange(chapter, {
104
+ start: { node: texts[0], index: 0 },
105
+ end: { node: new TextNode(), index: 0 },
106
+ })).toBeUndefined();
107
+ expect(indexRangeToCaretRange(chapter, { start: 0, end: 100000 })).toBeUndefined();
108
+ });
109
+ });
110
+ describe('paragraph boundaries', () => {
111
+ test('the end of one paragraph and the start of the next have different indexes', () => {
112
+ const two = Parser.parseChapter(undefined, 'one\n\ntwo');
113
+ const [one, second] = two.getTextNodes();
114
+ expect(caretToIndex(two, { node: one, index: 3 })).toBe(3);
115
+ expect(caretToIndex(two, { node: second, index: 0 })).toBe(4);
116
+ expect(indexToCaret(two, 4)).toEqual({ node: second, index: 0 });
117
+ });
118
+ test('footnote text is counted once, inside its paragraph', () => {
119
+ const chapter = Parser.parseChapter(undefined, 'a{bc}d\n\ne');
120
+ const e = chapter.getTextNodes().find((t) => t.getText() === 'e');
121
+ expect(e).toBeDefined();
122
+ if (e === undefined)
123
+ return;
124
+ // a (1), footnote text (2), the footnote atom (1), d (1), then one for the paragraph boundary.
125
+ expect(caretToIndex(chapter, { node: e, index: 0 })).toBe(6);
126
+ });
127
+ });
@@ -18,10 +18,13 @@ export default class ChapterNode extends BlocksNode {
18
18
  getErrors(): ErrorNode[];
19
19
  getCitations(): Set<string>;
20
20
  getFootnotes(): FootnoteNode[];
21
+ /** Headers, including those that lines of symbols expand to. */
21
22
  getHeaders(): ParagraphNode[];
22
23
  getEmbeds(): EmbedNode[];
23
24
  getComments(): CommentNode[];
24
25
  getLabels(): LabelNode[];
26
+ /** The symbols declared at the top of the chapter, by name. */
27
+ getSymbols(): Record<string, string>;
25
28
  getFirstParagraph(): ParagraphNode | undefined;
26
29
  hasLabel(labelID: string): boolean;
27
30
  getCitationNumber(citationID: string): number | null;
@@ -6,6 +6,7 @@ import CitationsNode from './CitationsNode';
6
6
  import LabelNode from './LabelNode';
7
7
  import CommentNode from './CommentNode';
8
8
  import BlocksNode from './BlocksNode';
9
+ import SymbolBlockNode from './SymbolBlockNode';
9
10
  export default class ChapterNode extends BlocksNode {
10
11
  #metadata;
11
12
  constructor(blocks, metadata) {
@@ -33,8 +34,13 @@ export default class ChapterNode extends BlocksNode {
33
34
  getFootnotes() {
34
35
  return this.getNodes().filter((n) => n instanceof FootnoteNode);
35
36
  }
37
+ /** Headers, including those that lines of symbols expand to. */
36
38
  getHeaders() {
37
- return this.getNodes().filter((n) => n instanceof ParagraphNode && n.getLevel() > 0);
39
+ return this.getNodes().flatMap((n) => n instanceof SymbolBlockNode
40
+ ? n.getHeaders()
41
+ : n instanceof ParagraphNode && n.getLevel() > 0
42
+ ? [n]
43
+ : []);
38
44
  }
39
45
  getEmbeds() {
40
46
  return this.getNodes().filter((n) => n instanceof EmbedNode);
@@ -45,6 +51,10 @@ export default class ChapterNode extends BlocksNode {
45
51
  getLabels() {
46
52
  return this.getNodes().filter((n) => n instanceof LabelNode);
47
53
  }
54
+ /** The symbols declared at the top of the chapter, by name. */
55
+ getSymbols() {
56
+ return this.#metadata.symbols;
57
+ }
48
58
  getFirstParagraph() {
49
59
  return this.getBlocks().find((b) => b instanceof ParagraphNode);
50
60
  }
@@ -12,7 +12,8 @@ export default class CommentNode extends AtomNode {
12
12
  return '';
13
13
  }
14
14
  toHTML() {
15
- return `(${this.getMeta().toHTML()})`;
15
+ // Editorial comments are for authors, not readers, as in the EPUB.
16
+ return '';
16
17
  }
17
18
  getDefaultCaret() {
18
19
  return this.getMeta().getFirstCaret();
@@ -29,7 +30,7 @@ export default class CommentNode extends AtomNode {
29
30
  }
30
31
  return `${previousText !== undefined && !previousText.getText().endsWith(' ')
31
32
  ? ' '
32
- : ''}%${this.getMeta().toBookdown()}%`;
33
+ : ''}%${this.getMeta().toBookdown('%')}%`;
33
34
  }
34
35
  copy() {
35
36
  return new CommentNode(this.getMeta().copy());
@@ -91,7 +91,7 @@ export default class EmbedNode extends BlockNode {
91
91
  return `<img src="${this.getURL()}" alt="${this.getDescription()}" />`;
92
92
  }
93
93
  toBookdown() {
94
- return `|${this.#urls}|${this.#description}|${this.#caption.toBookdown()}|${this.#credit.toBookdown()}|${this.#position === '|' ? '' : this.#position}`;
94
+ return `|${this.#urls}|${this.#description}|${this.#caption.toBookdown('|')}|${this.#credit.toBookdown('|')}|${this.#position === '|' ? '' : this.#position}`;
95
95
  }
96
96
  toJSON() {
97
97
  return {
@@ -17,7 +17,7 @@ export default class FootnoteNode extends AtomNode {
17
17
  return this.getMeta().toText();
18
18
  }
19
19
  toBookdown() {
20
- return `{${this.getMeta().toBookdown()}}`;
20
+ return `{${this.getMeta().toBookdown('}')}}`;
21
21
  }
22
22
  toHTML() {
23
23
  return `(${this.getMeta().toHTML()})`;
@@ -21,7 +21,11 @@ export default class FormatNode extends Node {
21
21
  getParentOf(node: Node): Node | undefined;
22
22
  toText(): string;
23
23
  toHTML(): string;
24
- toBookdown(): string;
24
+ /**
25
+ * @param awaiting The delimiter that ends the content this format is in,
26
+ * such as the } of a footnote, which its text has to escape.
27
+ */
28
+ toBookdown(awaiting?: string): string;
25
29
  getChildren(): FormatNodeSegmentType[];
26
30
  getFirstTextNode(): TextNode;
27
31
  getLastTextNode(): TextNode;