bookish-press 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/chapter/Block.svelte +19 -0
- package/dist/components/chapter/Block.svelte.d.ts +1 -0
- package/dist/components/chapter/ChapterBody.test.js +69 -0
- package/dist/components/chapter/Format.svelte +4 -0
- package/dist/components/chapter/Marginal.svelte +2 -1
- package/dist/components/chapter/Symbol.svelte +27 -0
- package/dist/components/chapter/Symbol.svelte.d.ts +7 -0
- package/dist/components/editor/BookishEditor.svelte +41 -153
- package/dist/components/editor/Commands.js +45 -44
- package/dist/components/editor/Commands.test.d.ts +1 -0
- package/dist/components/editor/Commands.test.js +791 -0
- package/dist/components/editor/Editing.d.ts +51 -0
- package/dist/components/editor/Editing.js +201 -0
- package/dist/components/editor/Permissions.svelte +10 -2
- package/dist/components/page/BookLoader.svelte +43 -6
- package/dist/components/page/BulkReferenceEditor.svelte +2 -2
- package/dist/components/page/Chapter.svelte +77 -70
- package/dist/components/page/Contexts.d.ts +14 -0
- package/dist/components/page/Contexts.js +14 -4
- package/dist/components/page/Edition.svelte +2 -10
- package/dist/components/page/EditionLoader.svelte +40 -7
- package/dist/components/page/Index.svelte +1 -1
- package/dist/components/page/Publish.svelte +0 -42
- package/dist/components/page/Reference.svelte +2 -2
- package/dist/components/page/References.svelte +5 -6
- package/dist/components/page/TitlePage.svelte +65 -0
- package/dist/components/page/TitlePage.svelte.d.ts +20 -0
- package/dist/components/page/UnsavedNotice.svelte +96 -0
- package/dist/components/page/UnsavedNotice.svelte.d.ts +7 -0
- package/dist/epub/xhtml.js +15 -0
- package/dist/models/CRUD.d.ts +28 -12
- package/dist/models/CRUD.emu.test.d.ts +1 -0
- package/dist/models/CRUD.emu.test.js +250 -0
- package/dist/models/CRUD.js +110 -72
- package/dist/models/book/Book.js +1 -1
- package/dist/models/book/Book.test.d.ts +1 -0
- package/dist/models/book/Book.test.js +124 -0
- package/dist/models/book/BookMedia.d.ts +5 -0
- package/dist/models/book/BookMedia.js +23 -7
- package/dist/models/book/BookMedia.test.d.ts +1 -0
- package/dist/models/book/BookMedia.test.js +40 -0
- package/dist/models/book/Chapter.js +28 -12
- package/dist/models/book/Chapter.test.d.ts +1 -0
- package/dist/models/book/Chapter.test.js +167 -0
- package/dist/models/book/Edition.d.ts +2 -0
- package/dist/models/book/Edition.js +34 -25
- package/dist/models/book/Edition.test.d.ts +1 -0
- package/dist/models/book/Edition.test.js +342 -0
- package/dist/models/book/Reference.js +5 -9
- package/dist/models/book/Reference.test.d.ts +1 -0
- package/dist/models/book/Reference.test.js +40 -0
- package/dist/models/chapter/BlocksNode.d.ts +2 -0
- package/dist/models/chapter/BlocksNode.js +59 -3
- package/dist/models/chapter/BlocksNode.test.js +2 -2
- package/dist/models/chapter/Caret.js +12 -3
- package/dist/models/chapter/Caret.test.d.ts +1 -0
- package/dist/models/chapter/Caret.test.js +127 -0
- package/dist/models/chapter/ChapterNode.d.ts +3 -0
- package/dist/models/chapter/ChapterNode.js +11 -1
- package/dist/models/chapter/CommentNode.js +3 -2
- package/dist/models/chapter/EmbedNode.js +1 -1
- package/dist/models/chapter/FootnoteNode.js +1 -1
- package/dist/models/chapter/FormatNode.d.ts +5 -1
- package/dist/models/chapter/FormatNode.js +60 -20
- package/dist/models/chapter/LabelNode.js +4 -2
- package/dist/models/chapter/ListNode.js +11 -13
- package/dist/models/chapter/Node.test.js +2 -11
- package/dist/models/chapter/Operations.test.d.ts +1 -0
- package/dist/models/chapter/Operations.test.js +298 -0
- package/dist/models/chapter/ParagraphNode.js +29 -3
- package/dist/models/chapter/Parser.d.ts +49 -4
- package/dist/models/chapter/Parser.js +210 -88
- package/dist/models/chapter/Parser.test.js +31 -0
- package/dist/models/chapter/Roundtrip.test.d.ts +1 -0
- package/dist/models/chapter/Roundtrip.test.js +313 -0
- package/dist/models/chapter/SymbolBlockNode.d.ts +40 -0
- package/dist/models/chapter/SymbolBlockNode.js +75 -0
- package/dist/models/chapter/SymbolBlockNode.test.d.ts +1 -0
- package/dist/models/chapter/SymbolBlockNode.test.js +130 -0
- package/dist/models/chapter/SymbolNode.d.ts +32 -0
- package/dist/models/chapter/SymbolNode.js +61 -0
- package/dist/models/chapter/SymbolNode.test.d.ts +1 -0
- package/dist/models/chapter/SymbolNode.test.js +177 -0
- package/dist/models/chapter/TableNode.js +2 -2
- package/dist/models/chapter/TextNode.d.ts +8 -1
- package/dist/models/chapter/TextNode.js +22 -15
- package/dist/util/getSubdomain.js +5 -1
- package/dist/util/getSubdomain.test.d.ts +1 -0
- package/dist/util/getSubdomain.test.js +53 -0
- package/dist/util/mineReference.js +12 -5
- package/dist/util/mineReference.test.d.ts +1 -0
- package/dist/util/mineReference.test.js +62 -0
- package/package.json +12 -7
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import Parser from './Parser';
|
|
3
|
+
import TextNode from './TextNode';
|
|
4
|
+
import AtomNode from './AtomNode';
|
|
5
|
+
import MetadataNode from './MetadataNode';
|
|
6
|
+
import ParagraphNode from './ParagraphNode';
|
|
7
|
+
import ListNode from './ListNode';
|
|
8
|
+
import CodeNode from './CodeNode';
|
|
9
|
+
import EmbedNode from './EmbedNode';
|
|
10
|
+
import FormatNode from './FormatNode';
|
|
11
|
+
import ErrorNode from './ErrorNode';
|
|
12
|
+
/**
|
|
13
|
+
* The editor re-serializes the chapter's AST to Bookdown on every save, so any
|
|
14
|
+
* difference between what was parsed and what is written back silently
|
|
15
|
+
* rewrites the author's chapter. These tests pin that down two ways:
|
|
16
|
+
*
|
|
17
|
+
* - Canonical text, the form the serializer itself writes, must come back
|
|
18
|
+
* byte for byte.
|
|
19
|
+
* - Any other text may be normalized once (smart quotes, list renumbering),
|
|
20
|
+
* but a second save must not change it again, or every save drifts further.
|
|
21
|
+
*/
|
|
22
|
+
function roundTrip(text) {
|
|
23
|
+
return Parser.parseChapter(undefined, text).toBookdown();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A description of a tree's meaning, ignoring node identity and how text
|
|
27
|
+
* happens to be split across adjacent text nodes, so that two parses can be
|
|
28
|
+
* compared for whether a save changed what the chapter says.
|
|
29
|
+
*/
|
|
30
|
+
function shape(node) {
|
|
31
|
+
if (node instanceof TextNode)
|
|
32
|
+
return node.getText();
|
|
33
|
+
const described = { type: node.getType() };
|
|
34
|
+
if (node instanceof AtomNode || node instanceof MetadataNode) {
|
|
35
|
+
const meta = node.getMeta();
|
|
36
|
+
if (typeof meta !== 'object' || Array.isArray(meta))
|
|
37
|
+
described.meta = meta;
|
|
38
|
+
}
|
|
39
|
+
if (node instanceof FormatNode)
|
|
40
|
+
described.format = node.getFormat();
|
|
41
|
+
if (node instanceof ParagraphNode)
|
|
42
|
+
described.level = node.getLevel();
|
|
43
|
+
if (node instanceof ListNode)
|
|
44
|
+
described.numbered = node.isNumbered();
|
|
45
|
+
if (node instanceof CodeNode)
|
|
46
|
+
described.code = [
|
|
47
|
+
node.getLanguage(),
|
|
48
|
+
node.isExecutable(),
|
|
49
|
+
node.getPosition(),
|
|
50
|
+
];
|
|
51
|
+
if (node instanceof EmbedNode)
|
|
52
|
+
described.embed = [
|
|
53
|
+
node.getURL(),
|
|
54
|
+
node.getDescription(),
|
|
55
|
+
node.getPosition(),
|
|
56
|
+
];
|
|
57
|
+
const children = [];
|
|
58
|
+
for (const child of node.getChildren()) {
|
|
59
|
+
// Errors that are only diagnostics, like an unclosed *, are dropped
|
|
60
|
+
// on saving by design: the save closes the markup instead.
|
|
61
|
+
if (child instanceof ErrorNode && child.toBookdown() === '')
|
|
62
|
+
continue;
|
|
63
|
+
const childShape = shape(child);
|
|
64
|
+
// Unformatted groups are an artifact of parsing; flatten them.
|
|
65
|
+
const flattened = child instanceof FormatNode && child.getFormat() === ''
|
|
66
|
+
? (childShape.children ?? [])
|
|
67
|
+
: [childShape];
|
|
68
|
+
for (const item of flattened) {
|
|
69
|
+
// Merge adjacent text, which the parser splits at escapes.
|
|
70
|
+
if (typeof item === 'string' &&
|
|
71
|
+
typeof children[children.length - 1] === 'string')
|
|
72
|
+
children[children.length - 1] += item;
|
|
73
|
+
else
|
|
74
|
+
children.push(item);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
described.children = children.filter((c) => c !== '');
|
|
78
|
+
return described;
|
|
79
|
+
}
|
|
80
|
+
/** Canonical Bookdown for every block and inline construct. */
|
|
81
|
+
const canonical = [
|
|
82
|
+
// Blocks
|
|
83
|
+
['empty chapter', ''],
|
|
84
|
+
['paragraph', 'Hello world.'],
|
|
85
|
+
['two paragraphs', 'One.\n\nTwo.'],
|
|
86
|
+
['header 1', '# Header'],
|
|
87
|
+
['header 2', '## Header'],
|
|
88
|
+
['header 3', '### Header'],
|
|
89
|
+
['header 4', '#### Header'],
|
|
90
|
+
['header with formatting', '## A *bold* header'],
|
|
91
|
+
['unordered list', '* One\n* Two\n* Three'],
|
|
92
|
+
['ordered list', '1. One\n2. Two\n3. Three'],
|
|
93
|
+
['nested unordered list', '* One\n** Inner\n** Inner two\n* Two'],
|
|
94
|
+
['nested ordered list', '1. One\n1.. Inner\n2.. Inner two\n2. Two'],
|
|
95
|
+
['doubly nested list', '* One\n** Two\n*** Three\n* Four'],
|
|
96
|
+
['list item with formatting', '* A _styled_ item\n* Plain'],
|
|
97
|
+
['list then paragraph', '* One\n* Two\n\nAfter.'],
|
|
98
|
+
['code block', '`\nconsole.log(1);\n`'],
|
|
99
|
+
['code block with language', '`js\nconsole.log(1);\n`'],
|
|
100
|
+
['executable code block', '`python!\nprint(1)\n`'],
|
|
101
|
+
['code block with caption', '`js\nconsole.log(1);\n`A *caption*'],
|
|
102
|
+
['code block with position', '`js\nx\n`<A caption'],
|
|
103
|
+
['multiline code block', '`\nline one\n\nline three\n`'],
|
|
104
|
+
['code with backtick', '`\na \\` b\n`'],
|
|
105
|
+
['code with backslash', '`\na \\ b \\n\n`'],
|
|
106
|
+
['code with reserved chars', '`\n*_^{}[]<>~@%:x\n`'],
|
|
107
|
+
['indented code', '`\n indented\n more\n`'],
|
|
108
|
+
['quote', '"\nA quotation.\n"'],
|
|
109
|
+
['quote with credit', '"\nA quotation.\n"Someone'],
|
|
110
|
+
['quote with formatted credit', '"\nA quotation.\n"_Someone_, 2020'],
|
|
111
|
+
['quote with position', '"\nA quotation.\n"<Someone'],
|
|
112
|
+
['quote with blocks', '"\nOne.\n\nTwo.\n"Someone'],
|
|
113
|
+
['quote with list', '"\n* One\n* Two\n"'],
|
|
114
|
+
['callout', '=\nA callout.\n='],
|
|
115
|
+
['callout with position', '=\nA callout.\n=>'],
|
|
116
|
+
['callout with blocks', '=\n# Title\n\nBody.\n='],
|
|
117
|
+
['table', ',A|B\n,C|D\n'],
|
|
118
|
+
['table with caption', ',A|B\n,C|D\nA *caption*'],
|
|
119
|
+
['table with position', ',A|B\n<A caption'],
|
|
120
|
+
['table with formatted cells', ',*A*|_B_\n,`c`|[d|e]\n'],
|
|
121
|
+
['embed', '|photo.jpg|Alt text|A caption|A credit|'],
|
|
122
|
+
['embed with empty parts', '|photo.jpg||||'],
|
|
123
|
+
['embed with position', '|photo.jpg|Alt|Cap|Credit|<'],
|
|
124
|
+
['embed with formatted caption', '|photo.jpg|Alt|A *bold* caption|_Me_|'],
|
|
125
|
+
['rule', '-'],
|
|
126
|
+
['rule between paragraphs', 'One.\n\n-\n\nTwo.'],
|
|
127
|
+
['empty paragraph between', 'One.\n\n\n\nTwo.'],
|
|
128
|
+
['trailing empty paragraph', 'One.\n\n'],
|
|
129
|
+
// Inline
|
|
130
|
+
['bold', 'Some *bold* text.'],
|
|
131
|
+
['italic', 'Some _italic_ text.'],
|
|
132
|
+
['superscript', 'E = mc^2^.'],
|
|
133
|
+
['subscript', 'H^v2^O.'],
|
|
134
|
+
['nested formatting', '*Bold _and italic_ text*'],
|
|
135
|
+
['adjacent formatting', '*a*_b_^c^^vd^'],
|
|
136
|
+
['inline code', 'Call `f()` now.'],
|
|
137
|
+
['inline code with language', 'Call `f()`js now.'],
|
|
138
|
+
['inline code with backtick', 'Tick `a\\`b` here.'],
|
|
139
|
+
['link', 'A [link|https://example.com] here.'],
|
|
140
|
+
['chapter link', 'See [the intro|intro] and [a label|intro:label].'],
|
|
141
|
+
['citation', 'A claim<ref>.'],
|
|
142
|
+
['multiple citations', 'A claim<a,b,c>.'],
|
|
143
|
+
['footnote', 'A claim{A note.}'],
|
|
144
|
+
['formatted footnote', 'A claim{A *bold* note.}'],
|
|
145
|
+
['definition', 'A ~term~gloss here.'],
|
|
146
|
+
['line break', 'One↩Two'],
|
|
147
|
+
['inline comment', 'Text %a comment% more.'],
|
|
148
|
+
['label', ':intro An introduction.'],
|
|
149
|
+
['label mid paragraph', 'Some :label text.'],
|
|
150
|
+
['label before punctuation', 'Some _Legion:Scribe_ and :a, :b.'],
|
|
151
|
+
[
|
|
152
|
+
'escaped reserved characters',
|
|
153
|
+
'a \\_ \\* \\` \\< \\^ \\{ \\~ \\[ \\@ \\\\ b',
|
|
154
|
+
],
|
|
155
|
+
['escaped percent after space', 'a \\% b'],
|
|
156
|
+
['escaped label colon', 'time\\:x'],
|
|
157
|
+
['colon followed by non-letter', 'time: 12:30'],
|
|
158
|
+
['unknown symbol', 'An @unknown symbol.'],
|
|
159
|
+
['symbol at start', '@unknown at the start.'],
|
|
160
|
+
['escaped symbol', 'An \\@escaped symbol.'],
|
|
161
|
+
['smart quotes', '\u201cQuoted\u201d and it\u2019s \u2018single\u2019.'],
|
|
162
|
+
['em dash', 'One\u2014two.'],
|
|
163
|
+
['unescaped specials', 'a > b, c } d, e ] f, g | h, i = j, 50% k'],
|
|
164
|
+
];
|
|
165
|
+
describe('canonical Bookdown round trips exactly', () => {
|
|
166
|
+
test.each(canonical)('%s', (_, text) => {
|
|
167
|
+
expect(roundTrip(text)).toBe(text);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
/** Inputs the serializer would never write, which may be normalized once. */
|
|
171
|
+
const awkward = [
|
|
172
|
+
// Every reserved character escaped, including the escape itself.
|
|
173
|
+
'\\_\\*\\`\\<\\>\\^\\{\\}\\~\\[\\]\\@\\:\\%\\\\\\|\\=\\,\\#\\-',
|
|
174
|
+
'\\\\',
|
|
175
|
+
'\\\\\\\\',
|
|
176
|
+
'trailing backslash \\',
|
|
177
|
+
'\\',
|
|
178
|
+
'a\\\\*b*',
|
|
179
|
+
'\\# not a header',
|
|
180
|
+
'\\* not a list',
|
|
181
|
+
'\\- not a rule',
|
|
182
|
+
// Straight quotes and dashes become typographic.
|
|
183
|
+
"\"Quoted\" and it's 'single' -- dash",
|
|
184
|
+
// Unterminated and empty markup.
|
|
185
|
+
'*unclosed bold',
|
|
186
|
+
'_unclosed italic',
|
|
187
|
+
'^unclosed sup',
|
|
188
|
+
'^vunclosed sub',
|
|
189
|
+
'`unclosed code',
|
|
190
|
+
'[unclosed link',
|
|
191
|
+
'[no bar]',
|
|
192
|
+
'[text|unclosed',
|
|
193
|
+
'{unclosed footnote',
|
|
194
|
+
'<unclosed citation',
|
|
195
|
+
'~unclosed definition',
|
|
196
|
+
' %unclosed comment',
|
|
197
|
+
'**',
|
|
198
|
+
'__',
|
|
199
|
+
'``',
|
|
200
|
+
'{}',
|
|
201
|
+
'<>',
|
|
202
|
+
'[|]',
|
|
203
|
+
'~~',
|
|
204
|
+
':',
|
|
205
|
+
'@',
|
|
206
|
+
'email me @ home',
|
|
207
|
+
'a @ b @c',
|
|
208
|
+
// Nesting.
|
|
209
|
+
'*bold _italic ^sup^ italic_ bold*',
|
|
210
|
+
'_italic *bold* italic_',
|
|
211
|
+
'*a{footnote *bold*}b*',
|
|
212
|
+
'{a footnote with a <citation> and `code` and [a|link]}',
|
|
213
|
+
'{nested {footnote}}',
|
|
214
|
+
' %comment with *bold*%',
|
|
215
|
+
'*bold with :label inside*',
|
|
216
|
+
// Headers, lists, blocks in odd shapes.
|
|
217
|
+
'#',
|
|
218
|
+
'##### Deep header',
|
|
219
|
+
'#No space',
|
|
220
|
+
'*',
|
|
221
|
+
'* ',
|
|
222
|
+
'1.',
|
|
223
|
+
'1. ',
|
|
224
|
+
'* One\n\n* Two',
|
|
225
|
+
'* One\n1. Two',
|
|
226
|
+
'1. One\n* Two',
|
|
227
|
+
'** Starts nested',
|
|
228
|
+
'* One\n*** Skips a level\n* Two',
|
|
229
|
+
'5. Five\n9. Nine',
|
|
230
|
+
'`\n`',
|
|
231
|
+
'`',
|
|
232
|
+
'`js\nunclosed code block',
|
|
233
|
+
'`\n\n\n code \n\n\n`',
|
|
234
|
+
'"\n"',
|
|
235
|
+
'"\nunclosed quote',
|
|
236
|
+
'=\n=',
|
|
237
|
+
'=\nunclosed callout',
|
|
238
|
+
',',
|
|
239
|
+
',|',
|
|
240
|
+
',a|b\n,c',
|
|
241
|
+
',a|b\n\nafter',
|
|
242
|
+
'|',
|
|
243
|
+
'|a',
|
|
244
|
+
'|a|b',
|
|
245
|
+
'|a|b|c',
|
|
246
|
+
'|a|b|c|d',
|
|
247
|
+
'---',
|
|
248
|
+
'- some text after a rule',
|
|
249
|
+
'% a block comment\nText',
|
|
250
|
+
'Text\n\n% a block comment',
|
|
251
|
+
'One\nTwo',
|
|
252
|
+
'One\n\n\nTwo',
|
|
253
|
+
'One\n\n\n\n\n\nTwo',
|
|
254
|
+
'\n\nLeading newlines',
|
|
255
|
+
' Leading spaces',
|
|
256
|
+
'Trailing spaces ',
|
|
257
|
+
'\tTab',
|
|
258
|
+
// Content delimiters inside the content they would end.
|
|
259
|
+
',a\\|b|c',
|
|
260
|
+
'|img.png|alt|cap\\|tion|cre\\|dit|',
|
|
261
|
+
'{a\\}b}',
|
|
262
|
+
'x %a\\%b%',
|
|
263
|
+
'{unclosed\n\n\nNext',
|
|
264
|
+
'~unclosed\n\n\nNext',
|
|
265
|
+
// Escapes whose meaning depends on their neighbors.
|
|
266
|
+
'a \\% b',
|
|
267
|
+
'a\t%b',
|
|
268
|
+
'time\\:x',
|
|
269
|
+
'time\\:X',
|
|
270
|
+
'*time\\:x*',
|
|
271
|
+
'*a \\% b*',
|
|
272
|
+
'\\↩',
|
|
273
|
+
// Text that starts a paragraph the way another block starts.
|
|
274
|
+
'\\%not a comment',
|
|
275
|
+
'\\|not an embed',
|
|
276
|
+
'\\,not a table',
|
|
277
|
+
'\\=',
|
|
278
|
+
'\\"',
|
|
279
|
+
'\\1. not a list',
|
|
280
|
+
'\\-',
|
|
281
|
+
'\\-\\-\\-',
|
|
282
|
+
'Intro\n\n\\@a: not a declaration',
|
|
283
|
+
'@a\\: not a declaration',
|
|
284
|
+
'"\n\\# in a quote\n"',
|
|
285
|
+
'=\n\\- in a callout\n=',
|
|
286
|
+
// Block comments.
|
|
287
|
+
'%closed%\n\nText',
|
|
288
|
+
'% a comment with 50% of a %nested% comment',
|
|
289
|
+
'% ends with an escaped \\%',
|
|
290
|
+
'% a\n% b\n% c',
|
|
291
|
+
// Symbols, which this book has no definitions for.
|
|
292
|
+
'@a: A\n\nUses @a.',
|
|
293
|
+
'@a: A\n@b: B\n\n@a and @b',
|
|
294
|
+
'@bad name: x\n\ntext',
|
|
295
|
+
'@noColon\n\ntext',
|
|
296
|
+
];
|
|
297
|
+
describe('serialization is idempotent', () => {
|
|
298
|
+
test.each(awkward.map((text) => [JSON.stringify(text), text]))('%s', (_, text) => {
|
|
299
|
+
const once = roundTrip(text);
|
|
300
|
+
const twice = roundTrip(once);
|
|
301
|
+
expect(twice).toBe(once);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
describe('saving does not change what the chapter says', () => {
|
|
305
|
+
test.each([...canonical.map(([, text]) => text), ...awkward].map((text) => [
|
|
306
|
+
JSON.stringify(text),
|
|
307
|
+
text,
|
|
308
|
+
]))('%s', (_, text) => {
|
|
309
|
+
const parsed = Parser.parseChapter(undefined, text);
|
|
310
|
+
const reparsed = Parser.parseChapter(undefined, parsed.toBookdown());
|
|
311
|
+
expect(shape(reparsed)).toEqual(shape(parsed));
|
|
312
|
+
});
|
|
313
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import BlockNode from './BlockNode';
|
|
2
|
+
import type FormatNode from './FormatNode';
|
|
3
|
+
import type Node from './Node';
|
|
4
|
+
import ParagraphNode from './ParagraphNode';
|
|
5
|
+
/**
|
|
6
|
+
* A run of lines that each start with a symbol, whose definitions together
|
|
7
|
+
* make blocks, such as a header, or a table whose rows are each a symbol, with
|
|
8
|
+
* perhaps more cells after it:
|
|
9
|
+
*
|
|
10
|
+
* @tableHeader| More
|
|
11
|
+
* @rowOne| cells
|
|
12
|
+
* @rowTwo
|
|
13
|
+
*
|
|
14
|
+
* Symbols used to be expanded into the text before parsing, so books rely on
|
|
15
|
+
* consecutive symbols' definitions joining into one block. This node keeps the
|
|
16
|
+
* lines, so saving writes them back as written, and keeps the blocks the
|
|
17
|
+
* expanded lines parse into, for rendering. Like a symbol's definition, the
|
|
18
|
+
* blocks are not children: they aren't the chapter's text, so the caret can't
|
|
19
|
+
* enter them and edits can't reach them.
|
|
20
|
+
*/
|
|
21
|
+
export default class SymbolBlockNode extends BlockNode {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(lines: string[], blocks: BlockNode[]);
|
|
24
|
+
getType(): string;
|
|
25
|
+
/** The lines of Bookdown, as written. */
|
|
26
|
+
getLines(): string[];
|
|
27
|
+
/** The blocks the symbols' definitions expand to. */
|
|
28
|
+
getBlocks(): BlockNode[];
|
|
29
|
+
/** The headers among the expanded blocks, so they can be linked to. */
|
|
30
|
+
getHeaders(): ParagraphNode[];
|
|
31
|
+
getFormats(): FormatNode[];
|
|
32
|
+
getChildren(): Node[];
|
|
33
|
+
getParentOf(): Node | undefined;
|
|
34
|
+
toText(): string;
|
|
35
|
+
toHTML(): string;
|
|
36
|
+
toBookdown(): string;
|
|
37
|
+
copy(): this;
|
|
38
|
+
withChildReplaced(): undefined;
|
|
39
|
+
withContentInRange(): this | undefined;
|
|
40
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import BlockNode from './BlockNode';
|
|
2
|
+
import ParagraphNode from './ParagraphNode';
|
|
3
|
+
/**
|
|
4
|
+
* A run of lines that each start with a symbol, whose definitions together
|
|
5
|
+
* make blocks, such as a header, or a table whose rows are each a symbol, with
|
|
6
|
+
* perhaps more cells after it:
|
|
7
|
+
*
|
|
8
|
+
* @tableHeader| More
|
|
9
|
+
* @rowOne| cells
|
|
10
|
+
* @rowTwo
|
|
11
|
+
*
|
|
12
|
+
* Symbols used to be expanded into the text before parsing, so books rely on
|
|
13
|
+
* consecutive symbols' definitions joining into one block. This node keeps the
|
|
14
|
+
* lines, so saving writes them back as written, and keeps the blocks the
|
|
15
|
+
* expanded lines parse into, for rendering. Like a symbol's definition, the
|
|
16
|
+
* blocks are not children: they aren't the chapter's text, so the caret can't
|
|
17
|
+
* enter them and edits can't reach them.
|
|
18
|
+
*/
|
|
19
|
+
export default class SymbolBlockNode extends BlockNode {
|
|
20
|
+
#lines;
|
|
21
|
+
#blocks;
|
|
22
|
+
constructor(lines, blocks) {
|
|
23
|
+
super();
|
|
24
|
+
this.#lines = lines;
|
|
25
|
+
this.#blocks = blocks;
|
|
26
|
+
}
|
|
27
|
+
getType() {
|
|
28
|
+
return 'symbols';
|
|
29
|
+
}
|
|
30
|
+
/** The lines of Bookdown, as written. */
|
|
31
|
+
getLines() {
|
|
32
|
+
return this.#lines;
|
|
33
|
+
}
|
|
34
|
+
/** The blocks the symbols' definitions expand to. */
|
|
35
|
+
getBlocks() {
|
|
36
|
+
return this.#blocks;
|
|
37
|
+
}
|
|
38
|
+
/** The headers among the expanded blocks, so they can be linked to. */
|
|
39
|
+
getHeaders() {
|
|
40
|
+
return this.#blocks.flatMap((block) => block
|
|
41
|
+
.getNodes()
|
|
42
|
+
.flatMap((node) => node instanceof SymbolBlockNode
|
|
43
|
+
? node.getHeaders()
|
|
44
|
+
: node instanceof ParagraphNode && node.getLevel() > 0
|
|
45
|
+
? [node]
|
|
46
|
+
: []));
|
|
47
|
+
}
|
|
48
|
+
getFormats() {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
getChildren() {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
getParentOf() {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
toText() {
|
|
58
|
+
return this.#blocks.map((block) => block.toText()).join(' ');
|
|
59
|
+
}
|
|
60
|
+
toHTML() {
|
|
61
|
+
return this.#blocks.map((block) => block.toHTML()).join('');
|
|
62
|
+
}
|
|
63
|
+
toBookdown() {
|
|
64
|
+
return this.#lines.join('\n');
|
|
65
|
+
}
|
|
66
|
+
copy() {
|
|
67
|
+
return new SymbolBlockNode([...this.#lines], this.#blocks);
|
|
68
|
+
}
|
|
69
|
+
withChildReplaced() {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
withContentInRange() {
|
|
73
|
+
return this.copy();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,130 @@
|
|
|
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 SymbolBlockNode from './SymbolBlockNode';
|
|
6
|
+
import TableNode from './TableNode';
|
|
7
|
+
import ParagraphNode from './ParagraphNode';
|
|
8
|
+
import { serializeChapter } from '../../epub/xhtml';
|
|
9
|
+
/**
|
|
10
|
+
* Books use symbols as block macros. Before symbols were parsed, they were
|
|
11
|
+
* expanded into the text, so a symbol alone on a line could be a header, and
|
|
12
|
+
* consecutive lines starting with symbols, perhaps followed by more cells, the
|
|
13
|
+
* rows of one table. Critically Conscious Computing's standards tables are
|
|
14
|
+
* built this way.
|
|
15
|
+
*/
|
|
16
|
+
const edition = Edition.fromJSON(undefined, {
|
|
17
|
+
title: 'Test',
|
|
18
|
+
authors: [],
|
|
19
|
+
number: 1,
|
|
20
|
+
summary: '',
|
|
21
|
+
published: null,
|
|
22
|
+
images: {},
|
|
23
|
+
description: '',
|
|
24
|
+
chapters: [],
|
|
25
|
+
license: '',
|
|
26
|
+
acknowledgements: '',
|
|
27
|
+
tags: [],
|
|
28
|
+
sources: {},
|
|
29
|
+
references: {},
|
|
30
|
+
symbols: {
|
|
31
|
+
standardsHeader: '# Relevant learning standards',
|
|
32
|
+
blurb: 'These are the [standards|https://example.com].',
|
|
33
|
+
tableHeader: ',*Standard*| Description',
|
|
34
|
+
rowOne: ',1a| Apply *practices*.',
|
|
35
|
+
rowTwo: ',1b| Reflect.',
|
|
36
|
+
chapterLink: '[Design|design]',
|
|
37
|
+
word: 'Word',
|
|
38
|
+
},
|
|
39
|
+
glossary: {},
|
|
40
|
+
theme: null,
|
|
41
|
+
uids: [],
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
});
|
|
44
|
+
const text = 'Intro.\n\n@standardsHeader\n@blurb\n\n@tableHeader| Chapters\n@rowOne| @chapterLink\n@rowTwo\n\nAfter.';
|
|
45
|
+
function parse(markup = text) {
|
|
46
|
+
return Parser.parseChapter(edition, markup);
|
|
47
|
+
}
|
|
48
|
+
function symbolBlocks(chapter) {
|
|
49
|
+
return chapter
|
|
50
|
+
.getBlocks()
|
|
51
|
+
.filter((b) => b instanceof SymbolBlockNode);
|
|
52
|
+
}
|
|
53
|
+
describe('symbols whose definitions are blocks', () => {
|
|
54
|
+
test('round trip as written', () => {
|
|
55
|
+
expect(parse().toBookdown()).toBe(text);
|
|
56
|
+
expect(parse(parse().toBookdown()).toBookdown()).toBe(text);
|
|
57
|
+
});
|
|
58
|
+
test('parse into a header and a table', () => {
|
|
59
|
+
const [header, table] = symbolBlocks(parse());
|
|
60
|
+
expect(header.getLines()).toEqual(['@standardsHeader', '@blurb']);
|
|
61
|
+
const [heading, paragraph] = header.getBlocks();
|
|
62
|
+
expect(heading).toBeInstanceOf(ParagraphNode);
|
|
63
|
+
expect(heading.getLevel()).toBe(1);
|
|
64
|
+
expect(paragraph.toText()).toContain('standards');
|
|
65
|
+
expect(table.getBlocks()).toHaveLength(1);
|
|
66
|
+
const node = table.getBlocks()[0];
|
|
67
|
+
expect(node).toBeInstanceOf(TableNode);
|
|
68
|
+
expect(node.getRows().map((row) => row.length)).toEqual([3, 3, 2]);
|
|
69
|
+
});
|
|
70
|
+
test('render as a header and a table', () => {
|
|
71
|
+
const html = parse().toHTML();
|
|
72
|
+
expect(html).toContain('<h1>Relevant learning standards</h1>');
|
|
73
|
+
expect(html).toContain('<table>');
|
|
74
|
+
expect(html).toContain('<td><strong>Standard</strong></td>');
|
|
75
|
+
expect(html).toContain('<td>Chapters</td>');
|
|
76
|
+
expect(html).toContain('<a href="design">Design</a>');
|
|
77
|
+
expect(html).not.toContain('@');
|
|
78
|
+
expect(html).not.toContain(',1a');
|
|
79
|
+
expect(parse().toText()).toContain('Relevant learning standards');
|
|
80
|
+
});
|
|
81
|
+
test('render in the EPUB', () => {
|
|
82
|
+
const { body } = serializeChapter(parse(), {
|
|
83
|
+
edition,
|
|
84
|
+
images: new Map(),
|
|
85
|
+
chapters: new Set(['design']),
|
|
86
|
+
});
|
|
87
|
+
expect(body).toContain('Relevant learning standards</h2>');
|
|
88
|
+
expect(body).toContain('<table');
|
|
89
|
+
expect(body).not.toContain('@rowOne');
|
|
90
|
+
});
|
|
91
|
+
test('their headers are the chapter’s headers', () => {
|
|
92
|
+
expect(parse()
|
|
93
|
+
.getHeaders()
|
|
94
|
+
.map((h) => h.toText())).toEqual(['Relevant learning standards']);
|
|
95
|
+
});
|
|
96
|
+
test('their blocks are not the chapter’s editable text', () => {
|
|
97
|
+
const [header] = symbolBlocks(parse());
|
|
98
|
+
expect(header.getChildren()).toEqual([]);
|
|
99
|
+
expect(parse()
|
|
100
|
+
.getTextNodes()
|
|
101
|
+
.map((t) => t.getText())
|
|
102
|
+
.join('')).not.toContain('Relevant');
|
|
103
|
+
});
|
|
104
|
+
test('blank lines after a table are read with it, as before', () => {
|
|
105
|
+
const chapter = parse('@rowOne\n@rowTwo\n\n\nAfter.');
|
|
106
|
+
expect(chapter.getBlocks()).toHaveLength(2);
|
|
107
|
+
expect(chapter.toBookdown()).toBe('@rowOne\n@rowTwo\n\nAfter.');
|
|
108
|
+
});
|
|
109
|
+
test('symbols that make plain paragraphs stay inline', () => {
|
|
110
|
+
const chapter = parse('@word\n\n@word and more');
|
|
111
|
+
expect(symbolBlocks(chapter)).toHaveLength(0);
|
|
112
|
+
expect(chapter.getNodes().filter((n) => n instanceof SymbolNode)).toHaveLength(2);
|
|
113
|
+
expect(chapter.toBookdown()).toBe('@word\n\n@word and more');
|
|
114
|
+
});
|
|
115
|
+
test('a line starting with an unknown symbol ends the run', () => {
|
|
116
|
+
const chapter = parse('@rowOne\n@unknown\n@rowTwo');
|
|
117
|
+
expect(symbolBlocks(chapter)).toHaveLength(2);
|
|
118
|
+
expect(chapter.toBookdown()).toBe('@rowOne\n\n@unknown\n\n@rowTwo');
|
|
119
|
+
});
|
|
120
|
+
test('a symbol defined in terms of itself as a block terminates', () => {
|
|
121
|
+
const loop = Edition.fromJSON(undefined, {
|
|
122
|
+
...edition.toObject(),
|
|
123
|
+
symbols: { loop: '# Again @loop\n@loop' },
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
125
|
+
});
|
|
126
|
+
const chapter = Parser.parseChapter(loop, '@loop');
|
|
127
|
+
expect(chapter.toBookdown()).toBe('@loop');
|
|
128
|
+
expect(chapter.toHTML()).toContain('<h1>');
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import AtomNode from './AtomNode';
|
|
2
|
+
import type Caret from './Caret';
|
|
3
|
+
import type FormatNode from './FormatNode';
|
|
4
|
+
import type Node from './Node';
|
|
5
|
+
/**
|
|
6
|
+
* A use of a symbol, `@name`, declared either at the top of the chapter
|
|
7
|
+
* (`@name: definition`) or in the edition's symbols.
|
|
8
|
+
*
|
|
9
|
+
* The node keeps the name, so that saving writes `@name` back rather than the
|
|
10
|
+
* definition, and expands it only when rendering. The parser resolves the
|
|
11
|
+
* definition while it still knows the chapter's declarations and the edition's
|
|
12
|
+
* symbols, and stores it here for rendering. The definition is deliberately not
|
|
13
|
+
* a child: it isn't part of this chapter's text, so the caret can't enter it
|
|
14
|
+
* and edits can't reach it. A symbol with no definition renders as `@name`.
|
|
15
|
+
*/
|
|
16
|
+
export default class SymbolNode extends AtomNode<string> {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(name: string, definition?: FormatNode);
|
|
19
|
+
getType(): string;
|
|
20
|
+
getName(): string;
|
|
21
|
+
/** The parsed definition, or undefined if the symbol isn't defined. */
|
|
22
|
+
getDefinition(): FormatNode | undefined;
|
|
23
|
+
getDefaultCaret(): Caret;
|
|
24
|
+
toText(): string;
|
|
25
|
+
toHTML(): string;
|
|
26
|
+
toBookdown(): string;
|
|
27
|
+
getParentOf(): Node | undefined;
|
|
28
|
+
copy(): this;
|
|
29
|
+
withMeta(name: string): SymbolNode;
|
|
30
|
+
withChildReplaced(): undefined;
|
|
31
|
+
withContentInRange(): this | undefined;
|
|
32
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import AtomNode from './AtomNode';
|
|
2
|
+
import TextNode from './TextNode';
|
|
3
|
+
/**
|
|
4
|
+
* A use of a symbol, `@name`, declared either at the top of the chapter
|
|
5
|
+
* (`@name: definition`) or in the edition's symbols.
|
|
6
|
+
*
|
|
7
|
+
* The node keeps the name, so that saving writes `@name` back rather than the
|
|
8
|
+
* definition, and expands it only when rendering. The parser resolves the
|
|
9
|
+
* definition while it still knows the chapter's declarations and the edition's
|
|
10
|
+
* symbols, and stores it here for rendering. The definition is deliberately not
|
|
11
|
+
* a child: it isn't part of this chapter's text, so the caret can't enter it
|
|
12
|
+
* and edits can't reach it. A symbol with no definition renders as `@name`.
|
|
13
|
+
*/
|
|
14
|
+
export default class SymbolNode extends AtomNode {
|
|
15
|
+
#definition;
|
|
16
|
+
constructor(name, definition) {
|
|
17
|
+
super(name);
|
|
18
|
+
this.#definition = definition;
|
|
19
|
+
}
|
|
20
|
+
getType() {
|
|
21
|
+
return 'symbol';
|
|
22
|
+
}
|
|
23
|
+
getName() {
|
|
24
|
+
return this.getMeta();
|
|
25
|
+
}
|
|
26
|
+
/** The parsed definition, or undefined if the symbol isn't defined. */
|
|
27
|
+
getDefinition() {
|
|
28
|
+
return this.#definition;
|
|
29
|
+
}
|
|
30
|
+
getDefaultCaret() {
|
|
31
|
+
return { node: this, index: 0 };
|
|
32
|
+
}
|
|
33
|
+
toText() {
|
|
34
|
+
return this.#definition
|
|
35
|
+
? this.#definition.toText()
|
|
36
|
+
: `@${this.getMeta()}`;
|
|
37
|
+
}
|
|
38
|
+
toHTML() {
|
|
39
|
+
return this.#definition
|
|
40
|
+
? this.#definition.toHTML()
|
|
41
|
+
: new TextNode(`@${this.getMeta()}`).toHTML();
|
|
42
|
+
}
|
|
43
|
+
toBookdown() {
|
|
44
|
+
return `@${this.getMeta()}`;
|
|
45
|
+
}
|
|
46
|
+
getParentOf() {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
copy() {
|
|
50
|
+
return new SymbolNode(this.getMeta(), this.#definition);
|
|
51
|
+
}
|
|
52
|
+
withMeta(name) {
|
|
53
|
+
return new SymbolNode(name);
|
|
54
|
+
}
|
|
55
|
+
withChildReplaced() {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
withContentInRange() {
|
|
59
|
+
return this.copy();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|