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.
Files changed (93) 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 +69 -0
  4. package/dist/components/chapter/Format.svelte +4 -0
  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/Permissions.svelte +10 -2
  15. package/dist/components/page/BookLoader.svelte +43 -6
  16. package/dist/components/page/BulkReferenceEditor.svelte +2 -2
  17. package/dist/components/page/Chapter.svelte +77 -70
  18. package/dist/components/page/Contexts.d.ts +14 -0
  19. package/dist/components/page/Contexts.js +14 -4
  20. package/dist/components/page/Edition.svelte +2 -10
  21. package/dist/components/page/EditionLoader.svelte +40 -7
  22. package/dist/components/page/Index.svelte +1 -1
  23. package/dist/components/page/Publish.svelte +0 -42
  24. package/dist/components/page/Reference.svelte +2 -2
  25. package/dist/components/page/References.svelte +5 -6
  26. package/dist/components/page/TitlePage.svelte +65 -0
  27. package/dist/components/page/TitlePage.svelte.d.ts +20 -0
  28. package/dist/components/page/UnsavedNotice.svelte +96 -0
  29. package/dist/components/page/UnsavedNotice.svelte.d.ts +7 -0
  30. package/dist/epub/xhtml.js +15 -0
  31. package/dist/models/CRUD.d.ts +28 -12
  32. package/dist/models/CRUD.emu.test.d.ts +1 -0
  33. package/dist/models/CRUD.emu.test.js +250 -0
  34. package/dist/models/CRUD.js +110 -72
  35. package/dist/models/book/Book.js +1 -1
  36. package/dist/models/book/Book.test.d.ts +1 -0
  37. package/dist/models/book/Book.test.js +124 -0
  38. package/dist/models/book/BookMedia.d.ts +5 -0
  39. package/dist/models/book/BookMedia.js +23 -7
  40. package/dist/models/book/BookMedia.test.d.ts +1 -0
  41. package/dist/models/book/BookMedia.test.js +40 -0
  42. package/dist/models/book/Chapter.js +28 -12
  43. package/dist/models/book/Chapter.test.d.ts +1 -0
  44. package/dist/models/book/Chapter.test.js +167 -0
  45. package/dist/models/book/Edition.d.ts +2 -0
  46. package/dist/models/book/Edition.js +34 -25
  47. package/dist/models/book/Edition.test.d.ts +1 -0
  48. package/dist/models/book/Edition.test.js +342 -0
  49. package/dist/models/book/Reference.js +5 -9
  50. package/dist/models/book/Reference.test.d.ts +1 -0
  51. package/dist/models/book/Reference.test.js +40 -0
  52. package/dist/models/chapter/BlocksNode.d.ts +2 -0
  53. package/dist/models/chapter/BlocksNode.js +59 -3
  54. package/dist/models/chapter/BlocksNode.test.js +2 -2
  55. package/dist/models/chapter/Caret.js +12 -3
  56. package/dist/models/chapter/Caret.test.d.ts +1 -0
  57. package/dist/models/chapter/Caret.test.js +127 -0
  58. package/dist/models/chapter/ChapterNode.d.ts +3 -0
  59. package/dist/models/chapter/ChapterNode.js +11 -1
  60. package/dist/models/chapter/CommentNode.js +3 -2
  61. package/dist/models/chapter/EmbedNode.js +1 -1
  62. package/dist/models/chapter/FootnoteNode.js +1 -1
  63. package/dist/models/chapter/FormatNode.d.ts +5 -1
  64. package/dist/models/chapter/FormatNode.js +60 -20
  65. package/dist/models/chapter/LabelNode.js +4 -2
  66. package/dist/models/chapter/ListNode.js +11 -13
  67. package/dist/models/chapter/Node.test.js +2 -11
  68. package/dist/models/chapter/Operations.test.d.ts +1 -0
  69. package/dist/models/chapter/Operations.test.js +298 -0
  70. package/dist/models/chapter/ParagraphNode.js +29 -3
  71. package/dist/models/chapter/Parser.d.ts +49 -4
  72. package/dist/models/chapter/Parser.js +210 -88
  73. package/dist/models/chapter/Parser.test.js +31 -0
  74. package/dist/models/chapter/Roundtrip.test.d.ts +1 -0
  75. package/dist/models/chapter/Roundtrip.test.js +313 -0
  76. package/dist/models/chapter/SymbolBlockNode.d.ts +40 -0
  77. package/dist/models/chapter/SymbolBlockNode.js +75 -0
  78. package/dist/models/chapter/SymbolBlockNode.test.d.ts +1 -0
  79. package/dist/models/chapter/SymbolBlockNode.test.js +130 -0
  80. package/dist/models/chapter/SymbolNode.d.ts +32 -0
  81. package/dist/models/chapter/SymbolNode.js +61 -0
  82. package/dist/models/chapter/SymbolNode.test.d.ts +1 -0
  83. package/dist/models/chapter/SymbolNode.test.js +177 -0
  84. package/dist/models/chapter/TableNode.js +2 -2
  85. package/dist/models/chapter/TextNode.d.ts +8 -1
  86. package/dist/models/chapter/TextNode.js +22 -15
  87. package/dist/util/getSubdomain.js +5 -1
  88. package/dist/util/getSubdomain.test.d.ts +1 -0
  89. package/dist/util/getSubdomain.test.js +53 -0
  90. package/dist/util/mineReference.js +12 -5
  91. package/dist/util/mineReference.test.d.ts +1 -0
  92. package/dist/util/mineReference.test.js +62 -0
  93. package/package.json +12 -7
@@ -0,0 +1,177 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import Parser from './Parser';
3
+ import Edition from '../book/Edition';
4
+ import SymbolNode from './SymbolNode';
5
+ import TextNode from './TextNode';
6
+ import { serializeChapter } from '../../epub/xhtml';
7
+ /** An edition with the given symbols and nothing else of note. */
8
+ function makeEdition(symbols) {
9
+ return Edition.fromJSON(undefined, {
10
+ title: 'Test',
11
+ authors: [],
12
+ number: 1,
13
+ summary: '',
14
+ published: null,
15
+ images: {},
16
+ description: '',
17
+ chapters: [],
18
+ license: '',
19
+ acknowledgements: '',
20
+ tags: [],
21
+ sources: {},
22
+ references: {},
23
+ symbols,
24
+ glossary: {},
25
+ theme: null,
26
+ uids: [],
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
+ });
29
+ }
30
+ const edition = makeEdition({
31
+ press: 'the *Bookish* press',
32
+ 'c++': 'C plus plus',
33
+ 'a.b': 'dotted',
34
+ foo: 'Foo',
35
+ foobar: 'Foobar',
36
+ money: 'costs $1 and $&',
37
+ outer: 'an @inner thing',
38
+ inner: 'inner',
39
+ loop: 'a @loop forever',
40
+ shadowed: 'from the edition',
41
+ });
42
+ function symbols(text, book = edition) {
43
+ return Parser.parseChapter(book, text)
44
+ .getNodes()
45
+ .filter((n) => n instanceof SymbolNode);
46
+ }
47
+ describe('symbols are saved as written', () => {
48
+ test.each([
49
+ ['chapter symbol', '@name: Amy\n\nHi, I am @name.'],
50
+ ['chapter symbol at the start', '@name: Amy\n\n@name wrote this.'],
51
+ ['several chapter symbols', '@a: A\n\n@b: B\n\n@a and @b, then @a.'],
52
+ ['formatted chapter symbol', '@name: *Amy*\n\nI am @name.'],
53
+ ['edition symbol', 'Published by @press.'],
54
+ ['edition symbol at the start', '@press published this.'],
55
+ ['edition symbol in formatting', '*Bold @press* and _@press_'],
56
+ ['edition symbol in a footnote', 'A claim{By @press.}'],
57
+ ['edition symbol in a list', '* @press\n* @foo'],
58
+ ['edition symbol in a header', '# About @press'],
59
+ ['escaped symbol', 'Email \\@press, not @press.'],
60
+ ['unknown symbol', 'An @unknown symbol.'],
61
+ ['unknown symbol at the start', '@unknown at the start.'],
62
+ ['name with regex characters', 'Use @c++ and @a.b now.'],
63
+ ['longest name wins', '@foobar and @foo.'],
64
+ ['definition with replacement patterns', 'It @money.'],
65
+ ['nested symbols', 'An @outer.'],
66
+ ['self referential symbol', 'Go @loop.'],
67
+ ['symbol before a colon at the start', '@foo\\: a paragraph'],
68
+ ])('%s', (_, text) => {
69
+ expect(Parser.parseChapter(edition, text).toBookdown()).toBe(text);
70
+ });
71
+ test('without an edition, edition symbols are kept by name', () => {
72
+ const text = 'Published by @press.';
73
+ expect(Parser.parseChapter(undefined, text).toBookdown()).toBe(text);
74
+ });
75
+ test('a save with a symbol is stable', () => {
76
+ const text = '@name: Amy\n\nI am @name.';
77
+ const once = Parser.parseChapter(edition, text).toBookdown();
78
+ expect(Parser.parseChapter(edition, once).toBookdown()).toBe(text);
79
+ });
80
+ });
81
+ describe('symbols are parsed by name', () => {
82
+ test('chapter declarations', () => {
83
+ const [symbol] = symbols('@name: Amy\n\nI am @name.');
84
+ expect(symbol.getName()).toBe('name');
85
+ expect(symbol.getDefinition()?.toText()).toBe('Amy');
86
+ });
87
+ test('at position 0, with no declarations', () => {
88
+ const [symbol] = symbols('@press published this.');
89
+ expect(symbol.getName()).toBe('press');
90
+ expect(symbol.getDefinition()).toBeDefined();
91
+ });
92
+ test('chapter declarations shadow edition symbols', () => {
93
+ const [symbol] = symbols('@shadowed: from the chapter\n\n@shadowed');
94
+ expect(symbol.getDefinition()?.toText()).toBe('from the chapter');
95
+ });
96
+ test('escaped symbols are text', () => {
97
+ expect(symbols('\\@press')).toHaveLength(0);
98
+ expect(Parser.parseChapter(edition, '\\@press').toText()).toBe('@press');
99
+ });
100
+ test('names that are regex metacharacters', () => {
101
+ expect(symbols('@c++ @a.b').map((s) => s.getName())).toEqual([
102
+ 'c++',
103
+ 'a.b',
104
+ ]);
105
+ // Without the escaping the old regex lacked, a.b matched a-b.
106
+ expect(symbols('@a-b').map((s) => s.getName())).toEqual(['a']);
107
+ });
108
+ test('the longest matching name, ending at a word boundary', () => {
109
+ expect(symbols('@foobar @foo @fooz').map((s) => [
110
+ s.getName(),
111
+ s.getDefinition()?.toText(),
112
+ ])).toEqual([
113
+ ['foobar', 'Foobar'],
114
+ ['foo', 'Foo'],
115
+ ['fooz', undefined],
116
+ ]);
117
+ });
118
+ test('a lone @ is text', () => {
119
+ const chapter = Parser.parseChapter(edition, 'email me @ home');
120
+ expect(symbols('email me @ home')).toHaveLength(0);
121
+ expect(chapter.toBookdown()).toBe('email me \\@ home');
122
+ expect(chapter.toText()).toContain('@');
123
+ });
124
+ test('a symbol is an atom with no children, so the caret skips over it', () => {
125
+ const [symbol] = symbols('@press');
126
+ expect(symbol.getChildren()).toEqual([]);
127
+ expect(symbol.getCaretPositionCount()).toBe(1);
128
+ expect(symbol.getDefaultCaret()).toEqual({ node: symbol, index: 0 });
129
+ // Its definition's text isn't part of the chapter's text nodes.
130
+ const chapter = Parser.parseChapter(edition, '@press');
131
+ expect(chapter
132
+ .getTextNodes()
133
+ .map((t) => t.getText())
134
+ .join('')).not.toContain('press');
135
+ });
136
+ test('copies keep the definition', () => {
137
+ const [symbol] = symbols('@press');
138
+ expect(symbol.copy().getDefinition()?.toText()).toBe(symbol.getDefinition()?.toText());
139
+ });
140
+ });
141
+ describe('symbols expand when rendered', () => {
142
+ test('toText expands chapter and edition symbols', () => {
143
+ const chapter = Parser.parseChapter(edition, '@name: Amy\n\n@name of @press.');
144
+ expect(chapter.toText()).toContain('Amy');
145
+ expect(chapter.toText()).toContain('Bookish');
146
+ expect(chapter.toText()).not.toContain('@');
147
+ });
148
+ test('toHTML renders the definition’s formatting', () => {
149
+ expect(Parser.parseChapter(edition, '@press').toHTML()).toContain('<strong>Bookish</strong>');
150
+ });
151
+ test('replacement patterns in definitions are literal', () => {
152
+ expect(Parser.parseChapter(edition, '@money').toText()).toContain('costs $1 and $&');
153
+ });
154
+ test('nested symbols expand', () => {
155
+ expect(Parser.parseChapter(edition, '@outer').toText()).toContain('inner');
156
+ expect(Parser.parseChapter(edition, '@outer').toText()).not.toContain('@');
157
+ });
158
+ test('self referential symbols stop expanding', () => {
159
+ expect(Parser.parseChapter(edition, '@loop').toText()).toContain('@loop');
160
+ });
161
+ test('unknown symbols render as written', () => {
162
+ const chapter = Parser.parseChapter(edition, '@unknown');
163
+ expect(chapter.toText().trim()).toBe('@unknown');
164
+ expect(chapter.toHTML()).toContain('@unknown');
165
+ });
166
+ test('inline content, like authors, expands edition symbols', () => {
167
+ expect(Parser.parseFormat(edition, 'By @press').toText()).toContain('Bookish');
168
+ expect(Parser.parseFormat(edition, 'By @press').toBookdown()).toBe('By @press');
169
+ });
170
+ test('the EPUB serializer expands symbols', () => {
171
+ const { body } = serializeChapter(Parser.parseChapter(edition, '@name: Amy\n\n@name of @press, @x.'), { edition, images: new Map(), chapters: new Set() });
172
+ expect(body).toContain('Amy');
173
+ expect(body).toContain('<strong>Bookish</strong>');
174
+ expect(body).toContain('@x');
175
+ expect(body).not.toContain('@name');
176
+ });
177
+ });
@@ -64,7 +64,7 @@ export default class TableNode extends BlockNode {
64
64
  }
65
65
  toBookdown() {
66
66
  return (this.#rows
67
- .map((row) => `,${row.map((cell) => cell.toBookdown()).join('|')}`)
67
+ .map((row) => `,${row.map((cell) => cell.toBookdown('|')).join('|')}`)
68
68
  .join('\n') +
69
69
  `\n${this.#position === '|' ? '' : this.#position}${this.#caption ? '' + this.#caption.toBookdown() : ''}`);
70
70
  }
@@ -72,7 +72,7 @@ export default class TableNode extends BlockNode {
72
72
  return `<table><tbody>${this.#rows
73
73
  .map((row) => `<tr>${row
74
74
  .map((cell) => `<td>${cell.toHTML()}</td>`)
75
- .join('')}`)
75
+ .join('')}</tr>`)
76
76
  .join('')}</tbody></table><center>${this.#caption.toHTML()}</center>`;
77
77
  }
78
78
  getChildren() {
@@ -15,7 +15,14 @@ export default class TextNode extends Node {
15
15
  getFirstCaret(): Caret;
16
16
  getLastCaret(): Caret;
17
17
  toText(): string;
18
- toBookdown(): string;
18
+ toBookdown(awaiting?: string): string;
19
+ /**
20
+ * Escape every character with special meaning inside content: \_*`<^{~[@,
21
+ * the line break symbol, a % that could start a comment, a : before a
22
+ * letter (which would start a label), and the delimiter that ends the
23
+ * content this text is in, if any (such as the } closing a footnote).
24
+ */
25
+ static escape(text: string, awaiting?: string): string;
19
26
  toHTML(): string;
20
27
  getChildren(): never[];
21
28
  copy(): this;
@@ -2,6 +2,7 @@ import FormatNode from './FormatNode';
2
2
  import Node from './Node';
3
3
  import AtomNode from './AtomNode';
4
4
  import BlocksNode from './BlocksNode';
5
+ import { LINE_BREAK } from './Symbols';
5
6
  export default class TextNode extends Node {
6
7
  #text;
7
8
  constructor(text = '') {
@@ -32,21 +33,27 @@ export default class TextNode extends Node {
32
33
  toText() {
33
34
  return this.#text;
34
35
  }
35
- toBookdown() {
36
- // Escape all characters with special meaning inside content nodes: _*`<^{~\[@% and :'s with no space after
37
- return this.#text
38
- .replace(/\\/g, '\\\\') // This has to go first! Otherwise it breaks all of the others below.
39
- .replace(/_/g, '\\_')
40
- .replace(/\*/g, '\\*')
41
- .replace(/`/g, '\\`')
42
- .replace(/</g, '\\<')
43
- .replace(/\^/g, '\\^')
44
- .replace(/{/g, '\\{')
45
- .replace(/~/g, '\\~')
46
- .replace(/ %/g, ' \\%')
47
- .replace(/\[/g, '\\[')
48
- .replace(/@/g, '\\@')
49
- .replace(/(:)([a-z])/g, '\\:$2');
36
+ toBookdown(awaiting) {
37
+ return TextNode.escape(this.#text, awaiting);
38
+ }
39
+ /**
40
+ * Escape every character with special meaning inside content: \_*`<^{~[@,
41
+ * the line break symbol, a % that could start a comment, a : before a
42
+ * letter (which would start a label), and the delimiter that ends the
43
+ * content this text is in, if any (such as the } closing a footnote).
44
+ */
45
+ static escape(text, awaiting) {
46
+ let escaped = '';
47
+ for (let i = 0; i < text.length; i++) {
48
+ const char = text.charAt(i);
49
+ const reserved = '\\_*`<^{~[@'.includes(char) ||
50
+ char === LINE_BREAK ||
51
+ char === awaiting ||
52
+ (char === '%' && (i === 0 || /\s/.test(text.charAt(i - 1)))) ||
53
+ (char === ':' && /[a-z]/i.test(text.charAt(i + 1)));
54
+ escaped += reserved ? '\\' + char : char;
55
+ }
56
+ return escaped;
50
57
  }
51
58
  toHTML() {
52
59
  // Ampersands are escaped first, or the entities introduced below get
@@ -2,7 +2,11 @@ export function getSubdomain() {
2
2
  if (typeof window === 'undefined')
3
3
  return undefined;
4
4
  // Determine if there's a subdomain.
5
- const hostParts = window.location.hostname.split('.');
5
+ const hostname = window.location.hostname;
6
+ // IP addresses have dots, but no subdomains.
7
+ if (/^[0-9.]+$/.test(hostname) || hostname.includes(':'))
8
+ return undefined;
9
+ const hostParts = hostname.split('.');
6
10
  const firstPart = hostParts[0];
7
11
  const secondPart = hostParts[1];
8
12
  return secondPart === undefined || /^(localhost|bookish)$/.test(firstPart)
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,53 @@
1
+ import { describe, test, expect, afterEach, vi } from 'vitest';
2
+ import { getSubdomain, pathWithoutSubdomain, getSubdomainURL, } from './getSubdomain';
3
+ function at(url) {
4
+ const location = new URL(url);
5
+ vi.stubGlobal('window', { location });
6
+ }
7
+ afterEach(() => {
8
+ vi.unstubAllGlobals();
9
+ });
10
+ describe('getSubdomain', () => {
11
+ test('finds a subdomain', () => {
12
+ at('https://amy.bookish.press/');
13
+ expect(getSubdomain()).toBe('amy');
14
+ at('http://amy.localhost:5173/');
15
+ expect(getSubdomain()).toBe('amy');
16
+ });
17
+ test('is undefined without one', () => {
18
+ at('https://bookish.press/');
19
+ expect(getSubdomain()).toBeUndefined();
20
+ at('http://localhost:5173/');
21
+ expect(getSubdomain()).toBeUndefined();
22
+ });
23
+ test('ignores IP addresses', () => {
24
+ at('http://127.0.0.1:5173/');
25
+ expect(getSubdomain()).toBeUndefined();
26
+ });
27
+ test('is undefined on the server', () => {
28
+ vi.stubGlobal('window', undefined);
29
+ expect(getSubdomain()).toBeUndefined();
30
+ });
31
+ });
32
+ describe('pathWithoutSubdomain', () => {
33
+ test('returns the path when there is no subdomain', () => {
34
+ at('https://bookish.press/');
35
+ expect(pathWithoutSubdomain('/write')).toBe('/write');
36
+ });
37
+ test('returns a full URL on the main domain', () => {
38
+ at('https://amy.bookish.press/chapter?x=1');
39
+ expect(pathWithoutSubdomain('/write')).toBe('https://bookish.press/write?x=1');
40
+ at('http://amy.localhost:5173/');
41
+ expect(pathWithoutSubdomain('/write')).toBe('http://localhost:5173/write');
42
+ });
43
+ });
44
+ describe('getSubdomainURL', () => {
45
+ test('builds a URL on the main domain', () => {
46
+ at('https://bookish.press/');
47
+ expect(getSubdomainURL('amy')).toBe('https://amy.bookish.press');
48
+ at('https://other.bookish.press/');
49
+ expect(getSubdomainURL('amy')).toBe('https://amy.bookish.press');
50
+ at('http://localhost:5173/');
51
+ expect(getSubdomainURL('amy')).toBe('http://amy.localhost:5173');
52
+ });
53
+ });
@@ -57,7 +57,9 @@ export function splitIntoSentences(text) {
57
57
  accumulator += char;
58
58
  }
59
59
  else {
60
- if (char === '.' && priorLetterSequence.length !== 1) {
60
+ // Periods end sentences, unless they follow an initial; question marks always do.
61
+ if ((char === '.' && priorLetterSequence.length !== 1) ||
62
+ char === '?') {
61
63
  sentences.push(accumulator);
62
64
  accumulator = '';
63
65
  priorLetterSequence = '';
@@ -78,7 +80,9 @@ export function getUniqueReferenceID(ids, authors, year) {
78
80
  const semicolons = authors.includes(';');
79
81
  const authorList = authors
80
82
  .split(semicolons ? /;\s+/ : /,\s+/)
81
- .map((t) => t.trim());
83
+ .map((t) => t.trim())
84
+ // In "Last, F. M." lists, commas also separate surnames from initials; skip the initials.
85
+ .filter((t) => !/^([A-Z]\.\s*)+$/.test(t));
82
86
  const initials = authorList.map((a) => a.charAt(0).toLocaleLowerCase());
83
87
  const id = initials.join('') + year;
84
88
  let revisedID = id;
@@ -94,9 +98,12 @@ export function getUniqueReferenceID(ids, authors, year) {
94
98
  return revisedID;
95
99
  }
96
100
  else {
97
- let id = '00';
98
- for (let i = 0; i < 4; i++)
99
- id = id + Math.round(Math.random() * 9);
101
+ let id;
102
+ do {
103
+ id = '00';
104
+ for (let i = 0; i < 4; i++)
105
+ id = id + Math.round(Math.random() * 9);
106
+ } while (ids.includes(id));
100
107
  return id;
101
108
  }
102
109
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,62 @@
1
+ import { describe, test, expect } from 'vitest';
2
+ import { mineReference, splitIntoSentences, getUniqueReferenceID, } from './mineReference';
3
+ describe('splitIntoSentences', () => {
4
+ test('splits on periods', () => {
5
+ expect(splitIntoSentences('One. Two. Three')).toEqual([
6
+ 'One',
7
+ ' Two',
8
+ ' Three',
9
+ ]);
10
+ });
11
+ test('does not split after single initials', () => {
12
+ expect(splitIntoSentences('Ko, A. J. Title here. Source')).toEqual([
13
+ 'Ko, A. J. Title here',
14
+ ' Source',
15
+ ]);
16
+ });
17
+ test('keeps digits and punctuation', () => {
18
+ expect(splitIntoSentences('Vol 2, pp 3-4.')).toEqual(['Vol 2, pp 3-4']);
19
+ });
20
+ test('handles empty text', () => {
21
+ expect(splitIntoSentences('')).toEqual([]);
22
+ });
23
+ });
24
+ describe('getUniqueReferenceID', () => {
25
+ test('combines author initials and the year', () => {
26
+ expect(getUniqueReferenceID([], 'Ko, Myers, Smith', '2020')).toBe('kms2020');
27
+ expect(getUniqueReferenceID([], 'Ko, A.; Myers, B.', '2020')).toBe('km2020');
28
+ });
29
+ test('appends letters to avoid collisions', () => {
30
+ expect(getUniqueReferenceID(['k2020'], 'Ko', '2020')).toBe('k2020a');
31
+ expect(getUniqueReferenceID(['k2020', 'k2020a'], 'Ko', '2020')).toBe('k2020b');
32
+ });
33
+ test('falls back to a random numeric ID that avoids collisions', () => {
34
+ const id = getUniqueReferenceID([]);
35
+ expect(id).toMatch(/^00[0-9]{4}$/);
36
+ // With every ID but one taken, it has to find the free one.
37
+ const taken = Array.from({ length: 10000 }, (_, i) => '00' + i.toString().padStart(4, '0')).filter((id) => id !== '001234');
38
+ expect(getUniqueReferenceID(taken, undefined, '2020')).toBe('001234');
39
+ });
40
+ });
41
+ describe('mineReference', () => {
42
+ test('extracts APA-style fields', () => {
43
+ const ref = mineReference([], 'Ko, A. J., Myers, B. A. (2004). Designing the whyline: a debugging interface. ACM CHI. https://doi.org/10.1145/985692');
44
+ expect(ref.year).toBe('2004');
45
+ expect(ref.url).toBe('https://doi.org/10.1145/985692');
46
+ expect(ref.authors).toBe('Ko, A. J., Myers, B. A.');
47
+ expect(ref.title).toBe('Designing the whyline: a debugging interface');
48
+ expect(ref.source).toBe('ACM CHI');
49
+ expect(ref.citationID).toBe('km2004');
50
+ });
51
+ test('keeps question marks on titles', () => {
52
+ const ref = mineReference([], 'Ko, A. (2020). Why bother? Venue.');
53
+ expect(ref.title).toBe('Why bother?');
54
+ });
55
+ test('handles text without a year or URL', () => {
56
+ const ref = mineReference([], 'Somebody. Something');
57
+ expect(ref.year).toBe('');
58
+ expect(ref.url).toBe('');
59
+ expect(ref.authors).toBe('Somebody');
60
+ expect(ref.title).toBe('Something');
61
+ });
62
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bookish-press",
3
3
  "private": false,
4
- "version": "0.11.2",
4
+ "version": "0.12.0",
5
5
  "type": "module",
6
6
  "description": "A platform for reading and writing readable, accessible, beautiful online books.",
7
7
  "license": "MIT",
@@ -21,8 +21,12 @@
21
21
  "emu": "firebase use dev && npm run env && firebase emulators:start",
22
22
  "package": "svelte-package --watch",
23
23
  "package:build": "svelte-package",
24
- "test": "vitest",
25
- "test:ci": "vitest run",
24
+ "test": "vitest --project unit",
25
+ "test:ci": "vitest run --project unit",
26
+ "//test:coverage": "echo 'Run the unit tests with a coverage report in coverage/.'",
27
+ "test:coverage": "vitest run --project unit --coverage",
28
+ "//test:emu": "echo 'Run the vitest emulator project (security rules, CRUD, the reader server load, Firebase contracts) against dedicated Firebase emulators.'",
29
+ "test:emu": "PUBLIC_FIREBASE_PROJECT_ID=demo-bookish PUBLIC_FIREBASE_STORAGE_BUCKET=demo-bookish.appspot.com PUBLIC_EMULATOR_AUTH=19099 PUBLIC_EMULATOR_FIRESTORE=18080 PUBLIC_EMULATOR_STORAGE=19199 PUBLIC_EMULATOR_FUNCTIONS=15001 bash scripts/test-emulators.sh 'npx vitest run --project emulator'",
26
30
  "//test:e2e": "echo 'Build the app and run the Playwright smoke tests against a preview server.'",
27
31
  "test:e2e": "npm run build && playwright test",
28
32
  "//test:e2e:emu": "echo 'Build the app and run the Playwright suite that signs in and edits against dedicated Firebase emulators (see firebase.test.json).'",
@@ -48,15 +52,16 @@
48
52
  "@testing-library/svelte": "^5",
49
53
  "@types/prismjs": "^1.26",
50
54
  "@types/smoothscroll-polyfill": "^0",
51
- "firebase-tools": "^15.28.2",
55
+ "@vitest/coverage-v8": "5.1.2",
56
+ "firebase-tools": "^15.37.0",
52
57
  "jsdom": "^31",
53
58
  "prettier": "^3",
54
59
  "prettier-plugin-svelte": "^4",
55
- "svelte": "^5.57.2",
56
- "svelte-check": "^4.7.5",
60
+ "svelte": "^5.59.2",
61
+ "svelte-check": "^4.7.8",
57
62
  "tslib": "^2",
58
63
  "typescript": "^6",
59
- "vite": "^8.0.12",
64
+ "vite": "^8.4.0",
60
65
  "vitest": "^5"
61
66
  },
62
67
  "dependencies": {