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,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().
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
|
@@ -57,7 +57,20 @@ export default class FormatNode extends Node {
|
|
|
57
57
|
.find((b) => b !== undefined);
|
|
58
58
|
}
|
|
59
59
|
toText() {
|
|
60
|
-
|
|
60
|
+
// Segments are parts of the same run of text, so they're joined directly
|
|
61
|
+
// ("un*believ*able" is one word, and the parser splits text at escapes,
|
|
62
|
+
// so a\_b is too). Atoms like footnotes are asides, so they're set apart
|
|
63
|
+
// with spaces so their words don't merge with their neighbors; symbols
|
|
64
|
+
// stand for inline text, so they aren't.
|
|
65
|
+
return this.#segments
|
|
66
|
+
.map((segment) => {
|
|
67
|
+
if (!(segment instanceof AtomNode) ||
|
|
68
|
+
segment.getType() === 'symbol')
|
|
69
|
+
return segment.toText();
|
|
70
|
+
const text = segment.toText();
|
|
71
|
+
return text.length > 0 ? ` ${text} ` : '';
|
|
72
|
+
})
|
|
73
|
+
.join('');
|
|
61
74
|
}
|
|
62
75
|
toHTML() {
|
|
63
76
|
const format = this.#format;
|
|
@@ -73,11 +86,36 @@ export default class FormatNode extends Node {
|
|
|
73
86
|
else
|
|
74
87
|
return segments;
|
|
75
88
|
}
|
|
76
|
-
|
|
89
|
+
/**
|
|
90
|
+
* @param awaiting The delimiter that ends the content this format is in,
|
|
91
|
+
* such as the } of a footnote, which its text has to escape.
|
|
92
|
+
*/
|
|
93
|
+
toBookdown(awaiting) {
|
|
94
|
+
let bookdown = '';
|
|
95
|
+
// Escape adjacent text nodes together, since whether a character needs
|
|
96
|
+
// escaping can depend on its neighbors (a % after a space, a : before
|
|
97
|
+
// a letter), and the parser splits text at every escape.
|
|
98
|
+
let text = undefined;
|
|
99
|
+
for (const segment of this.#segments) {
|
|
100
|
+
if (segment instanceof TextNode) {
|
|
101
|
+
text = (text ?? '') + segment.getText();
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (text !== undefined) {
|
|
105
|
+
bookdown += TextNode.escape(text, awaiting);
|
|
106
|
+
text = undefined;
|
|
107
|
+
}
|
|
108
|
+
bookdown +=
|
|
109
|
+
segment instanceof AtomNode
|
|
110
|
+
? segment.toBookdown(this)
|
|
111
|
+
: segment instanceof FormatNode
|
|
112
|
+
? segment.toBookdown(awaiting)
|
|
113
|
+
: segment.toBookdown();
|
|
114
|
+
}
|
|
115
|
+
if (text !== undefined)
|
|
116
|
+
bookdown += TextNode.escape(text, awaiting);
|
|
77
117
|
return ((this.#format === 'v' ? '^v' : this.#format) +
|
|
78
|
-
|
|
79
|
-
.map((s) => s instanceof AtomNode ? s.toBookdown(this) : s.toBookdown())
|
|
80
|
-
.join('') +
|
|
118
|
+
bookdown +
|
|
81
119
|
(this.#format === 'v' ? '^' : this.#format));
|
|
82
120
|
}
|
|
83
121
|
getChildren() {
|
|
@@ -100,7 +138,14 @@ export default class FormatNode extends Node {
|
|
|
100
138
|
// Get all text or atom nodes in this format, except for any inside an atom node,
|
|
101
139
|
// as this is used for navigation and editing, and they should be isolated from that.
|
|
102
140
|
getTextAndAtomNodes() {
|
|
103
|
-
|
|
141
|
+
const nodes = [];
|
|
142
|
+
this.traverse((node, path) => {
|
|
143
|
+
if ((node instanceof TextNode || node instanceof AtomNode) &&
|
|
144
|
+
// The path includes the node itself, so check its ancestors.
|
|
145
|
+
!path.slice(0, -1).some((n) => n instanceof AtomNode))
|
|
146
|
+
nodes.push(node);
|
|
147
|
+
});
|
|
148
|
+
return nodes;
|
|
104
149
|
}
|
|
105
150
|
getAdjacentTextOrAtom(node, next) {
|
|
106
151
|
// Find all of the text and atom nodes not in atom nodes in this node.
|
|
@@ -190,6 +235,8 @@ export default class FormatNode extends Node {
|
|
|
190
235
|
return text.indexOf(node);
|
|
191
236
|
}
|
|
192
237
|
getSelectedText(range) {
|
|
238
|
+
// Selections can be backwards; read them forwards.
|
|
239
|
+
range = this.sortRange(range);
|
|
193
240
|
if (!(range.start.node instanceof TextNode) ||
|
|
194
241
|
!(range.end.node instanceof TextNode))
|
|
195
242
|
return undefined;
|
|
@@ -765,20 +812,13 @@ export default class FormatNode extends Node {
|
|
|
765
812
|
const segmentAfter = newFormat.getSegmentAfter(node);
|
|
766
813
|
const newCaret = node instanceof AtomNode
|
|
767
814
|
? node.getDefaultCaret()
|
|
768
|
-
: node instanceof
|
|
769
|
-
? node.
|
|
770
|
-
: //
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
: undefined
|
|
776
|
-
: node instanceof MetadataNode
|
|
777
|
-
? {
|
|
778
|
-
node: node.getText(),
|
|
779
|
-
index: node.getText().getLength(),
|
|
780
|
-
}
|
|
781
|
-
: { node: node, index: node.getLength() };
|
|
815
|
+
: node instanceof TextNode
|
|
816
|
+
? { node: node, index: node.getLength() }
|
|
817
|
+
: // Otherwise, place the caret just after the inserted node, so typing continues outside it.
|
|
818
|
+
segmentAfter !== undefined &&
|
|
819
|
+
segmentAfter instanceof TextNode
|
|
820
|
+
? { node: segmentAfter, index: 0 }
|
|
821
|
+
: undefined;
|
|
782
822
|
if (newCaret === undefined)
|
|
783
823
|
return;
|
|
784
824
|
return { root: newFormat, range: { start: newCaret, end: newCaret } };
|
|
@@ -19,8 +19,10 @@ export default class LabelNode extends AtomNode {
|
|
|
19
19
|
toBookdown(format) {
|
|
20
20
|
const id = this.getMeta();
|
|
21
21
|
const nextText = format?.getNextTextOrAtom(this)?.toBookdown();
|
|
22
|
-
//
|
|
23
|
-
|
|
22
|
+
// A label's ID ends at the first character that isn't a letter, so
|
|
23
|
+
// separate it from letters that follow with a space. The error
|
|
24
|
+
// placeholder handles invalid empty label IDs.
|
|
25
|
+
return `:${id === '' ? ERROR_PLACEHOLDER : id}${/^[a-z]/i.test(nextText ?? '') ? ' ' : ''}`;
|
|
24
26
|
}
|
|
25
27
|
getParentOf() {
|
|
26
28
|
return undefined;
|
|
@@ -68,19 +68,17 @@ export default class ListNode extends BlockNode {
|
|
|
68
68
|
return this.#items.map((item) => item.toText()).join(' ');
|
|
69
69
|
}
|
|
70
70
|
toBookdown(level = 1) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
item.toBookdown(level + 1))
|
|
83
|
-
.join('\n');
|
|
71
|
+
// Number items, but not nested lists, which number their own.
|
|
72
|
+
let number = 0;
|
|
73
|
+
return this.#items
|
|
74
|
+
.map((item) => item instanceof ListNode
|
|
75
|
+
? item.toBookdown(level + 1)
|
|
76
|
+
: (this.isNumbered()
|
|
77
|
+
? ++number + '.'.repeat(level)
|
|
78
|
+
: '*'.repeat(level)) +
|
|
79
|
+
' ' +
|
|
80
|
+
item.toBookdown())
|
|
81
|
+
.join('\n');
|
|
84
82
|
}
|
|
85
83
|
toHTML() {
|
|
86
84
|
return this.isNumbered()
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { test, expect } from 'vitest';
|
|
2
|
-
|
|
3
2
|
import TextNode from './TextNode';
|
|
4
3
|
import FormatNode from './FormatNode';
|
|
5
|
-
|
|
6
4
|
const textOne = new TextNode('one');
|
|
7
5
|
const textTwo = new TextNode('two');
|
|
8
6
|
const textThree = new TextNode('three');
|
|
@@ -10,17 +8,10 @@ const textFour = new TextNode('four');
|
|
|
10
8
|
const textFive = new TextNode('five');
|
|
11
9
|
const bold = new FormatNode('*', [textThree, textFour]);
|
|
12
10
|
const format = new FormatNode('', [textOne, textTwo, bold, textFive]);
|
|
13
|
-
|
|
14
11
|
test('Replace descendants', () => {
|
|
15
|
-
expect(
|
|
16
|
-
|
|
17
|
-
).toBe('onetwo*hifour*five');
|
|
18
|
-
|
|
19
|
-
expect(
|
|
20
|
-
format.withNodeReplaced(textFive, new TextNode('yo'))?.toBookdown(),
|
|
21
|
-
).toBe('onetwo*threefour*yo');
|
|
12
|
+
expect(format.withNodeReplaced(textThree, new TextNode('hi'))?.toBookdown()).toBe('onetwo*hifour*five');
|
|
13
|
+
expect(format.withNodeReplaced(textFive, new TextNode('yo'))?.toBookdown()).toBe('onetwo*threefour*yo');
|
|
22
14
|
});
|
|
23
|
-
|
|
24
15
|
test('Parents', () => {
|
|
25
16
|
const parents = format.getParentsOf(textThree);
|
|
26
17
|
expect(parents?.length).toBe(2);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|