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
@@ -18,9 +18,17 @@ import RuleNode from './RuleNode';
18
18
  import TableNode from './TableNode';
19
19
  import TextNode from './TextNode';
20
20
  import LineBreakNode from './LineBreakNode';
21
+ import SymbolNode from './SymbolNode';
22
+ import SymbolBlockNode from './SymbolBlockNode';
21
23
  import { LINE_BREAK } from './Symbols';
22
24
  const numberedRE = /^[0-9]+\.+/;
23
25
  const bulletRE = /^\*+\s+/;
26
+ /** A rule is a line of nothing but dashes. */
27
+ const ruleRE = /^-+[ \t]*(\n|$)/;
28
+ /** A symbol declaration at the top of a chapter: @name: definition */
29
+ const declarationRE = /^@[a-zA-Z0-9]+[ \t]*:/;
30
+ /** The characters a symbol's name can be made of. */
31
+ const symbolNameRE = /[a-zA-Z0-9]/;
24
32
  // TODO This grammar is slightly out of date.
25
33
  // A simple recursive descent parser for this grammar.
26
34
  // Embeds a tokenizer, since the lexical grammar is simple.
@@ -54,7 +62,11 @@ export default class Parser {
54
62
  openedDoubleQuote;
55
63
  // We pass this to all parsing functions to gather information strewn about the document.
56
64
  metadata;
57
- constructor(book, text) {
65
+ /** Symbols declared by an enclosing chapter, when parsing a definition. */
66
+ inherited;
67
+ /** Symbols whose definitions are being parsed, to stop cycles. */
68
+ resolving;
69
+ constructor(book, text, symbols = {}, resolving = new Set()) {
58
70
  if (typeof text !== 'string')
59
71
  throw 'Parser expected a string but received ' + typeof text;
60
72
  this.book = book;
@@ -64,26 +76,29 @@ export default class Parser {
64
76
  this.metadata = {
65
77
  symbols: {},
66
78
  };
67
- }
68
- static preprocessSymbols(book, text) {
69
- // Replace any remaining symbols with any definitions given.
70
- if (book && book.getSymbols()) {
71
- for (const [symbol, definition] of Object.entries(book.getSymbols())) {
72
- // Don't replace escaped at symbols. Otherwise, be liberal in replacing any matching symbol that starts with the symbol
73
- // name and ends with a word boundary.
74
- text = text.replace(new RegExp('([^\\\\])@' + symbol + '\\b', 'g'), '$1' + definition);
75
- }
76
- }
77
- return text;
79
+ this.inherited = symbols;
80
+ this.resolving = resolving;
78
81
  }
79
82
  static parseChapter(book, text) {
80
- return new Parser(book, book === undefined ? text : Parser.preprocessSymbols(book, text)).parseChapter();
83
+ return new Parser(book, text).parseChapter();
81
84
  }
82
- static parseFormat(book, text) {
83
- return new Parser(book, book === undefined ? text : Parser.preprocessSymbols(book, text)).parseFormat();
85
+ /**
86
+ * Parse inline content. Symbols resolve against the given chapter symbols,
87
+ * then the edition's.
88
+ */
89
+ static parseFormat(book, text, symbols = {}) {
90
+ return new Parser(book, text, symbols).parseFormat();
84
91
  }
85
92
  static parseEmbed(book, text) {
86
- return new Parser(book, book === undefined ? text : Parser.preprocessSymbols(book, text)).parseEmbed();
93
+ return new Parser(book, text).parseEmbed();
94
+ }
95
+ /** The symbols in scope; chapter declarations shadow the edition's. */
96
+ getSymbols() {
97
+ return {
98
+ ...(this.book?.getSymbols() ?? {}),
99
+ ...this.inherited,
100
+ ...this.metadata.symbols,
101
+ };
87
102
  }
88
103
  // Get the next character, if there is one, null otherwise.
89
104
  peek() {
@@ -162,11 +177,12 @@ export default class Parser {
162
177
  }
163
178
  // All the text until the next newline
164
179
  restOfLine() {
165
- const nextNewline = this.text.substring(this.index).indexOf('\n') + this.index;
166
- if (nextNewline < 0)
167
- return this.text.substring(this.index);
168
- else
169
- return this.text.substring(this.index, Math.max(this.index, nextNewline));
180
+ // Careful: on the last line there's no newline, and indexOf's -1
181
+ // mustn't be offset by the index, or this would return nothing.
182
+ const nextNewline = this.text.indexOf('\n', this.index);
183
+ return nextNewline < 0
184
+ ? this.text.substring(this.index)
185
+ : this.text.substring(this.index, nextNewline);
170
186
  }
171
187
  // True if the given string is occurs next in the text.
172
188
  nextIs(text) {
@@ -191,7 +207,7 @@ export default class Parser {
191
207
  next === '^' ||
192
208
  next === '<' ||
193
209
  next === '{' ||
194
- (this.charBeforeNext() === ' ' && next === '%') ||
210
+ (next === '%' && this.nextIsCommentStart()) ||
195
211
  next === '[' ||
196
212
  next === '\\');
197
213
  }
@@ -201,6 +217,11 @@ export default class Parser {
201
217
  return false;
202
218
  return regex.test(this.rest());
203
219
  }
220
+ // Inline comments start with a % after a space, or at the start of a line.
221
+ nextIsCommentStart() {
222
+ const before = this.charBeforeNext();
223
+ return before === ' ' || before === '\n' || before === null;
224
+ }
204
225
  // Returns true if all of the text between the current character and the next newline is whitespace.
205
226
  isBlankLine() {
206
227
  return this.restOfLine().trim() === '';
@@ -229,20 +250,10 @@ export default class Parser {
229
250
  }
230
251
  parseChapter() {
231
252
  let blocks = [];
232
- // Read any symbols declared for this chapter.
253
+ // Read any symbols declared for this chapter. Uses of them are parsed
254
+ // into symbol nodes, which keep the name, so that saving the chapter
255
+ // doesn't replace them with their definitions.
233
256
  this.parseSymbols();
234
- // Get the remaining text.
235
- let declarations = this.text.substring(0, this.index);
236
- let rest = this.rest();
237
- // First, replace any occurrences of the symbol with the symbol's text.
238
- for (const [symbol, text] of Object.entries(this.metadata.symbols)) {
239
- rest = rest.replace(new RegExp('([^\\\\])@' + symbol + '\\b', 'g'), '$1' + text);
240
- }
241
- // Replace all of the remaining symbols using book symbol definitions.
242
- if (this.book)
243
- rest = Parser.preprocessSymbols(this.book, rest);
244
- // Replace the text with the pre-processed text.
245
- this.text = declarations + rest;
246
257
  // While there's more text, parse a line.
247
258
  let trailingNewlines = 0;
248
259
  while (this.more()) {
@@ -265,29 +276,17 @@ export default class Parser {
265
276
  parseSymbols() {
266
277
  // Read whitespace before the symbols
267
278
  this.readWhitespace();
268
- // Keep reading symbols
269
- while (this.nextIs('@')) {
279
+ // Keep reading symbols. Anything else starting with an @, such as a
280
+ // paragraph starting with a symbol, is left for the blocks to parse.
281
+ while (this.nextMatches(declarationRE)) {
270
282
  // Read the @
271
283
  this.read();
272
284
  // Read the name.
273
- let name = this.readUntilNewlineOr(':');
274
- // Names need to be letter and numbers only.
275
- if (!/^[a-zA-Z0-9]+$/.test(name)) {
276
- new ErrorNode(this.readUntilNewLine(), name.trim() === ''
277
- ? 'Did you mean to declare a symbol? Use an @ symbol, then a name of only numbers and letters, then a colon, then whatever content you want it to represent.'
278
- : "'" +
279
- name +
280
- "' isn't a valid name for a symbol; letters and numbers only");
281
- return;
282
- }
283
- // Read any whitespace until the colon.
285
+ let name = '';
286
+ while (symbolNameRE.test(this.peek() ?? ''))
287
+ name += this.read();
288
+ // Read any whitespace until the colon, then the colon.
284
289
  this.readWhitespace();
285
- // Name declarations need to be terminated with a colon before the block starts.
286
- if (!this.nextIs(':')) {
287
- new ErrorNode(this.readUntilNewLine(), "Symbol names are to be followed by a ':'");
288
- return;
289
- }
290
- // Read the colon
291
290
  this.read();
292
291
  // Read any whitespace after the colon.
293
292
  this.readWhitespace();
@@ -305,15 +304,18 @@ export default class Parser {
305
304
  }
306
305
  }
307
306
  parseBlock() {
308
- // Read comment lines
309
- while (this.nextIs('%')) {
310
- this.readUntilNewLine();
311
- }
307
+ // Parse lines of symbols whose definitions make blocks.
308
+ const symbols = this.nextIs('@') ? this.parseSymbolBlock() : undefined;
309
+ if (symbols)
310
+ return symbols;
311
+ // Parse a comment line
312
+ else if (this.nextIs('%'))
313
+ return this.parseBlockComment();
312
314
  // Parse and return a header if it starts with a hash
313
- if (this.nextIs('#'))
315
+ else if (this.nextIs('#'))
314
316
  return this.parseHeader();
315
- // Parse and return a horizontal rule if it starts with a dash
316
- else if (this.nextIs('-'))
317
+ // Parse and return a horizontal rule if the line is only dashes
318
+ else if (this.nextMatches(ruleRE))
317
319
  return this.parseRule();
318
320
  // Parse and return an embed if it starts with a bar
319
321
  else if (this.nextIs('|'))
@@ -337,6 +339,33 @@ export default class Parser {
337
339
  else
338
340
  return this.parseParagraph();
339
341
  }
342
+ /**
343
+ * A line starting with % is a comment, to the end of the line. It becomes a
344
+ * paragraph holding only a comment, which readers don't see, so it survives
345
+ * being saved. The comment is written back with a closing %, which is
346
+ * dropped again here, so that the line doesn't grow on every save.
347
+ */
348
+ parseBlockComment() {
349
+ // Read the %
350
+ this.read();
351
+ // Find the end of the comment: the end of the line, or a closing %
352
+ // at the end of the line that isn't escaped.
353
+ const line = this.restOfLine().trimEnd();
354
+ let end = this.index + line.length;
355
+ if (line.endsWith('%')) {
356
+ let backslashes = 0;
357
+ while (line.charAt(line.length - 2 - backslashes) === '\\')
358
+ backslashes++;
359
+ if (backslashes % 2 === 0)
360
+ end--;
361
+ }
362
+ // Parse the comment's content on its own, sharing the symbols in scope.
363
+ const content = new Parser(this.book, this.text.substring(this.index, end), this.getSymbols(), this.resolving).parseFormat();
364
+ // Skip past the content and anything after it on the line.
365
+ this.index = end;
366
+ this.readUntilNewLine();
367
+ return new ParagraphNode(0, new FormatNode('', [new CommentNode(content)]));
368
+ }
340
369
  parseParagraph() {
341
370
  if (this.nextIsEmptyParagraph()) {
342
371
  this.read();
@@ -463,8 +492,9 @@ export default class Parser {
463
492
  }
464
493
  code = code + next;
465
494
  }
466
- // Trim the code.
467
- code = code.trim();
495
+ // Trim blank lines before and whitespace after the code, but not the
496
+ // indentation of its first line.
497
+ code = code.replace(/^([ \t]*\n)+/, '').trimEnd();
468
498
  // Read the backtick.
469
499
  if (this.nextIs('`'))
470
500
  this.read();
@@ -603,21 +633,11 @@ export default class Parser {
603
633
  else if (this.nextIs('['))
604
634
  segments.push(this.parseLink());
605
635
  // Parse inline comments
606
- else if (this.charBeforeNext() === ' ' && this.nextIs('%'))
636
+ else if (this.nextIs('%') && this.nextIsCommentStart())
607
637
  segments.push(this.parseComment());
608
- // Parse an unresolved symbol
609
- else if (next === '@') {
610
- // Read the @, then the symbol name.
611
- this.read();
612
- let symbol = '';
613
- // Stop at the end of the name or file.
614
- let next = this.peek();
615
- while (next !== null && /[a-zA-Z0-9]/.test(next)) {
616
- symbol = symbol + this.read();
617
- next = this.peek();
618
- }
619
- segments.push(new ErrorNode(undefined, "Couldn't find symbol @" + symbol));
620
- }
638
+ // Parse a symbol
639
+ else if (next === '@')
640
+ segments.push(this.parseSymbol());
621
641
  // Parse a label
622
642
  else if (next === ':' &&
623
643
  charAfterNext !== null &&
@@ -791,8 +811,9 @@ export default class Parser {
791
811
  this.read();
792
812
  // Read the footnote content.
793
813
  const footnote = this.parseFormat('}');
794
- // Read the closing }
795
- this.read();
814
+ // Read the closing }, if it was closed before the end of the line.
815
+ if (this.peek() === '}')
816
+ this.read();
796
817
  return new FootnoteNode(footnote);
797
818
  }
798
819
  parseDefinition() {
@@ -800,8 +821,9 @@ export default class Parser {
800
821
  this.read();
801
822
  // Read the phrase
802
823
  const text = this.readUntilNewlineOr('~');
803
- // Read the closing ~
804
- this.read();
824
+ // Read the closing ~, if it was closed before the end of the line.
825
+ if (this.peek() === '~')
826
+ this.read();
805
827
  // Read the glossary entry ID
806
828
  let glossaryID = '';
807
829
  // Stop at the end of the name or file.
@@ -813,14 +835,113 @@ export default class Parser {
813
835
  return new DefinitionNode(new TextNode(text), glossaryID);
814
836
  }
815
837
  parseEscaped() {
816
- // Skip the scape and just add the next character.
838
+ // Skip the escape and just add the next character.
817
839
  this.read();
818
- const char = this.read();
819
- if (char)
820
- return new TextNode(char);
821
- return new ErrorNode(undefined, 'Unterminated escape.');
840
+ // A backslash at the end of a line or the text escapes nothing, so
841
+ // keep it as a backslash rather than dropping it or the newline.
842
+ if (!this.more() || this.nextIs('\n'))
843
+ return new TextNode('\\');
844
+ return new TextNode(this.read() ?? '');
845
+ }
846
+ /**
847
+ * Parse @name. The name is the longest symbol in scope that the text
848
+ * continues with, so edition symbols whose names have other characters
849
+ * still resolve; otherwise it's the letters and numbers after the @. A
850
+ * symbol with no definition still keeps its name, so it is saved as written.
851
+ */
852
+ /**
853
+ * The longest name of a symbol in scope that the text after the @ at the
854
+ * given index continues with, ending at the end of a name.
855
+ */
856
+ matchSymbol(symbols, index) {
857
+ return Object.keys(symbols)
858
+ .filter((candidate) => candidate.length > 0 &&
859
+ this.text.startsWith(candidate, index) &&
860
+ !symbolNameRE.test(this.text.charAt(index + candidate.length)))
861
+ .sort((a, b) => b.length - a.length)[0];
862
+ }
863
+ /**
864
+ * Symbols used to be expanded into the text before parsing, so books use
865
+ * symbols as macros for blocks: a line that is a symbol can be a header,
866
+ * and consecutive lines that start with symbols can be the rows of one
867
+ * table, each line continuing its symbol's row with more cells. Parse a
868
+ * run of lines that each start with a defined symbol as the blocks they
869
+ * make when each line's first symbol is replaced by its definition,
870
+ * keeping the lines as written, so they are saved unchanged. If that only
871
+ * makes plain paragraphs, the symbols are left to parse inline, so they
872
+ * stay editable in their paragraph.
873
+ */
874
+ parseSymbolBlock() {
875
+ const symbols = this.getSymbols();
876
+ const lines = [];
877
+ const expanded = [];
878
+ const names = new Set(this.resolving);
879
+ const start = this.index;
880
+ let index = this.index;
881
+ while (this.text.charAt(index) === '@') {
882
+ const name = this.matchSymbol(symbols, index + 1);
883
+ if (name === undefined || this.resolving.has(name))
884
+ break;
885
+ const newline = this.text.indexOf('\n', index);
886
+ const end = newline < 0 ? this.text.length : newline;
887
+ lines.push(this.text.substring(index, end));
888
+ expanded.push(symbols[name] +
889
+ this.text.substring(index + 1 + name.length, end));
890
+ names.add(name);
891
+ this.index = end;
892
+ // Continue if the next line starts with another symbol.
893
+ index = end + 1;
894
+ }
895
+ if (lines.length === 0)
896
+ return undefined;
897
+ const blocks = new Parser(this.book, expanded.join('\n'), { ...this.inherited, ...this.metadata.symbols }, names).parseBlocks();
898
+ if (blocks.every((block) => block instanceof ParagraphNode && block.getLevel() === 0)) {
899
+ // Leave the symbols to be parsed inline.
900
+ this.index = start;
901
+ return undefined;
902
+ }
903
+ // A table ends by reading its last row's newline, as it did when
904
+ // its rows were expanded into the text, so that blank lines after it
905
+ // aren't read as an empty paragraph.
906
+ if (blocks[blocks.length - 1] instanceof TableNode && this.nextIs('\n'))
907
+ this.read();
908
+ return new SymbolBlockNode(lines, blocks);
909
+ }
910
+ /** Parse blocks until the end of the text. */
911
+ parseBlocks() {
912
+ const blocks = [];
913
+ while (this.more()) {
914
+ blocks.push(this.parseBlock());
915
+ while (this.nextIsWhitespace())
916
+ this.read();
917
+ }
918
+ return blocks;
919
+ }
920
+ parseSymbol() {
921
+ // Read the @
922
+ this.read();
923
+ const symbols = this.getSymbols();
924
+ let name = this.matchSymbol(symbols, this.index);
925
+ if (name === undefined) {
926
+ name = '';
927
+ while (symbolNameRE.test(this.peek() ?? ''))
928
+ name += this.read();
929
+ // A lone @ isn't a symbol; keep it as text.
930
+ if (name === '')
931
+ return new TextNode('@');
932
+ }
933
+ else
934
+ this.index += name.length;
935
+ const definition = symbols[name];
936
+ return new SymbolNode(name,
937
+ // Leave undefined symbols, and symbols defined in terms of
938
+ // themselves, unexpanded.
939
+ definition === undefined || this.resolving.has(name)
940
+ ? undefined
941
+ : new Parser(this.book, definition, { ...this.inherited, ...this.metadata.symbols }, new Set([...this.resolving, name])).parseFormat());
822
942
  }
823
943
  parseLink() {
944
+ const start = this.index;
824
945
  // Read the [
825
946
  this.read();
826
947
  // Read pure text until funding |
@@ -828,7 +949,8 @@ export default class Parser {
828
949
  // Catch missing bars
829
950
  if (this.peek() !== '|') {
830
951
  this.readUntilNewLine();
831
- return new ErrorNode(undefined, "Missing '|' in link");
952
+ // Keep what was read, so that saving doesn't drop it.
953
+ return new ErrorNode(this.text.substring(start, this.index), "Missing '|' in link");
832
954
  }
833
955
  // Read the |
834
956
  this.read();
@@ -837,7 +959,7 @@ export default class Parser {
837
959
  // Catch missing closing
838
960
  if (this.peek() !== ']') {
839
961
  this.readUntilNewLine();
840
- return new ErrorNode(undefined, 'Missing ] in link');
962
+ return new ErrorNode(this.text.substring(start, this.index), 'Missing ] in link');
841
963
  }
842
964
  // Read the ]
843
965
  this.read();
@@ -1,5 +1,9 @@
1
1
  import { test, expect } from 'vitest';
2
2
  import Parser from './Parser';
3
+ import ParagraphNode from './ParagraphNode';
4
+ import FormatNode from './FormatNode';
5
+ import TextNode from './TextNode';
6
+ import CommentNode from './CommentNode';
3
7
  test('Empty chapter', () => {
4
8
  expect(Parser.parseChapter(undefined, '').toBookdown()).toBe('');
5
9
  });
@@ -23,3 +27,30 @@ test('Whitespace', () => {
23
27
  p.readWhitespace();
24
28
  expect(p.peek()).toBe('H');
25
29
  });
30
+ test('a paragraph that starts with a comment keeps the text after it', () => {
31
+ // A line starting with % is a whole-line comment, so a paragraph the
32
+ // editor builds with a comment first must not save as one. (It also hit
33
+ // restOfLine() returning nothing on the last line past its first
34
+ // character.)
35
+ const paragraph = new ParagraphNode(0, new FormatNode('', [
36
+ new CommentNode(new FormatNode('', [new TextNode('note')])),
37
+ new TextNode(' hello'),
38
+ ]));
39
+ for (const before of ['', 'One.\n\n']) {
40
+ const saved = before + paragraph.toBookdown();
41
+ const once = Parser.parseChapter(undefined, saved);
42
+ expect(once.toText()).toContain('hello');
43
+ expect(once.getNodes().some((n) => n instanceof CommentNode)).toBe(true);
44
+ const twice = Parser.parseChapter(undefined, once.toBookdown());
45
+ expect(twice.toBookdown()).toBe(once.toBookdown());
46
+ }
47
+ });
48
+ test('the rest of the last line is read in full', () => {
49
+ const parser = new Parser(undefined, 'ab\ncd');
50
+ parser.read();
51
+ expect(parser.restOfLine()).toBe('b');
52
+ parser.read();
53
+ parser.read();
54
+ parser.read();
55
+ expect(parser.restOfLine()).toBe('d');
56
+ });
@@ -0,0 +1 @@
1
+ export {};