@usememos/editor 26.10.24 → 26.10.26
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/README.md +17 -14
- package/dist/base.css +22 -5
- package/dist/emoji.js +2 -1
- package/dist/emoji.js.map +2 -2
- package/dist/index.js +4576 -3279
- package/dist/index.js.map +4 -4
- package/dist/react.js.map +2 -2
- package/dist/style.css +96 -11
- package/dist/theme.css +49 -14
- package/dist/types/decorations.d.ts +5 -0
- package/dist/types/editor.d.ts +17 -1
- package/dist/types/extensions/suggestion.d.ts +5 -0
- package/dist/types/icons.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/labels.d.ts +3 -0
- package/dist/types/markdown/ast.d.ts +92 -16
- package/dist/types/markdown/lower.d.ts +11 -3
- package/dist/types/markdown/markup.d.ts +24 -0
- package/dist/types/markdown/options.d.ts +3 -1
- package/dist/types/markdown/source.d.ts +8 -2
- package/dist/types/markdown/sourceMap.d.ts +66 -0
- package/dist/types/react/index.d.ts +1 -1
- package/dist/types/sourceHighlight.d.ts +77 -0
- package/dist/types/sourceMode.d.ts +58 -4
- package/dist/types/theme.d.ts +5 -0
- package/dist/types/view/dom.d.ts +24 -0
- package/package.json +1 -1
|
@@ -1,46 +1,87 @@
|
|
|
1
1
|
import type { Align } from '../model.js';
|
|
2
|
-
|
|
2
|
+
/** A range of the parsed text: `from` inclusive, `to` exclusive. */
|
|
3
|
+
export interface Range {
|
|
4
|
+
readonly from: number;
|
|
5
|
+
readonly to: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A piece of markup of an inline node, in the coordinates of its `pos`:
|
|
9
|
+
* `open` and `close` are delimiter runs, brackets, backticks or the `<` and
|
|
10
|
+
* `>` of an autolink; `tail` is a link's `(destination "title")` or
|
|
11
|
+
* `[label]`, with `dest` and `title` the destination and title text in it,
|
|
12
|
+
* or a bare URL's text.
|
|
13
|
+
*/
|
|
14
|
+
export interface SyntaxRange extends Range {
|
|
15
|
+
readonly kind: 'open' | 'close' | 'tail' | 'dest' | 'title';
|
|
16
|
+
}
|
|
17
|
+
/** A run of a text node's value that is a verbatim slice of the parsed text: `value.slice(at, pieceEnd(piece))`. */
|
|
18
|
+
export interface TextPiece extends Range {
|
|
19
|
+
readonly at: number;
|
|
20
|
+
}
|
|
21
|
+
/** Where a piece ends in its text node's value. */
|
|
22
|
+
export declare function pieceEnd(p: TextPiece): number;
|
|
23
|
+
/**
|
|
24
|
+
* Where an inline node sits in the text it was parsed from (a paragraph's
|
|
25
|
+
* content as the block parser built it; see `ContentSource`). Nodes made
|
|
26
|
+
* elsewhere than by the parser have no `pos`.
|
|
27
|
+
*/
|
|
28
|
+
export interface Positioned {
|
|
29
|
+
readonly pos?: Range;
|
|
30
|
+
readonly syntax?: readonly SyntaxRange[];
|
|
31
|
+
}
|
|
32
|
+
export type InlineNode = TextNode | ({
|
|
3
33
|
readonly type: 'softBreak';
|
|
4
|
-
} | {
|
|
34
|
+
} & Positioned) | ({
|
|
5
35
|
readonly type: 'hardBreak';
|
|
6
|
-
} |
|
|
7
|
-
readonly type: 'code';
|
|
8
|
-
readonly value: string;
|
|
9
|
-
}
|
|
36
|
+
} & Positioned) | CodeNode
|
|
10
37
|
/** Raw inline HTML, verbatim. */
|
|
11
|
-
| {
|
|
38
|
+
| ({
|
|
12
39
|
readonly type: 'html';
|
|
13
40
|
readonly value: string;
|
|
14
|
-
}
|
|
41
|
+
} & Positioned)
|
|
15
42
|
/** A token of an inline syntax extension (`name`), verbatim source. */
|
|
16
|
-
| {
|
|
43
|
+
| ({
|
|
17
44
|
readonly type: 'token';
|
|
18
45
|
readonly name: string;
|
|
19
46
|
readonly value: string;
|
|
20
|
-
} | {
|
|
47
|
+
} & Positioned) | ({
|
|
21
48
|
readonly type: 'emphasis';
|
|
22
49
|
readonly children: InlineNode[];
|
|
23
|
-
} | {
|
|
50
|
+
} & Positioned) | ({
|
|
24
51
|
readonly type: 'strong';
|
|
25
52
|
readonly children: InlineNode[];
|
|
26
|
-
}
|
|
53
|
+
} & Positioned)
|
|
27
54
|
/** GFM strikethrough (`~text~` or `~~text~~`). */
|
|
28
|
-
| {
|
|
55
|
+
| ({
|
|
29
56
|
readonly type: 'strike';
|
|
30
57
|
readonly children: InlineNode[];
|
|
31
|
-
} | LinkNode | ImageNode;
|
|
58
|
+
} & Positioned) | LinkNode | ImageNode;
|
|
32
59
|
export interface TextNode {
|
|
33
60
|
readonly type: 'text';
|
|
34
61
|
readonly value: string;
|
|
62
|
+
readonly pos?: Range;
|
|
63
|
+
/**
|
|
64
|
+
* The verbatim runs of `value` when it is not one verbatim slice of `pos`:
|
|
65
|
+
* an escape or entity stands for text that is not written as such, and the
|
|
66
|
+
* gaps between the pieces are those. Empty when nothing in it is verbatim.
|
|
67
|
+
* Absent when all of it is.
|
|
68
|
+
*/
|
|
69
|
+
readonly pieces?: readonly TextPiece[];
|
|
70
|
+
}
|
|
71
|
+
export interface CodeNode extends Positioned {
|
|
72
|
+
readonly type: 'code';
|
|
73
|
+
readonly value: string;
|
|
74
|
+
/** Where `value` is in the text: between the backtick runs, less a stripped space on each side. */
|
|
75
|
+
readonly content?: Range;
|
|
35
76
|
}
|
|
36
|
-
export interface LinkNode {
|
|
77
|
+
export interface LinkNode extends Positioned {
|
|
37
78
|
readonly type: 'link';
|
|
38
79
|
/** Decoded destination, not yet percent-encoded. */
|
|
39
80
|
readonly destination: string;
|
|
40
81
|
readonly title: string;
|
|
41
82
|
readonly children: InlineNode[];
|
|
42
83
|
}
|
|
43
|
-
export interface ImageNode {
|
|
84
|
+
export interface ImageNode extends Positioned {
|
|
44
85
|
readonly type: 'image';
|
|
45
86
|
readonly destination: string;
|
|
46
87
|
readonly title: string;
|
|
@@ -81,10 +122,35 @@ export declare function imageSuffixText(s: {
|
|
|
81
122
|
}): string;
|
|
82
123
|
export type BlockNode = ParagraphNode | HeadingNode | {
|
|
83
124
|
readonly type: 'thematicBreak';
|
|
125
|
+
readonly lines?: SourceLines;
|
|
84
126
|
} | BlockQuoteNode | ListNode | CodeBlockNode | HtmlBlockNode | RawNode | TableNode;
|
|
127
|
+
/**
|
|
128
|
+
* One line of a leaf block's content as the block parser built it: the line's
|
|
129
|
+
* text after the block's prefixes (quote markers, list indentation), followed
|
|
130
|
+
* by "\n". `at` is where it starts in that content, `line` its 1-based source
|
|
131
|
+
* line, `offset` where its text starts on that line, and `pad` the spaces that
|
|
132
|
+
* stand in for the rest of a tab the prefix consumed only partly.
|
|
133
|
+
*/
|
|
134
|
+
export interface ContentLine {
|
|
135
|
+
readonly at: number;
|
|
136
|
+
readonly line: number;
|
|
137
|
+
readonly offset: number;
|
|
138
|
+
readonly pad: number;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Where a leaf block's content came from, so its text maps back to the
|
|
142
|
+
* source. The inline nodes' `pos` and a code block's text count from `start`
|
|
143
|
+
* in the built content: link reference definitions, a task marker, a fence's
|
|
144
|
+
* info line or trimmed whitespace come before it.
|
|
145
|
+
*/
|
|
146
|
+
export interface ContentSource {
|
|
147
|
+
readonly lines: readonly ContentLine[];
|
|
148
|
+
readonly start: number;
|
|
149
|
+
}
|
|
85
150
|
export interface ParagraphNode {
|
|
86
151
|
readonly type: 'paragraph';
|
|
87
152
|
readonly children: InlineNode[];
|
|
153
|
+
readonly source?: ContentSource;
|
|
88
154
|
}
|
|
89
155
|
export interface HeadingNode {
|
|
90
156
|
readonly type: 'heading';
|
|
@@ -92,6 +158,7 @@ export interface HeadingNode {
|
|
|
92
158
|
/** True for a setext heading (underlined with `=` or `-`). */
|
|
93
159
|
readonly setext: boolean;
|
|
94
160
|
readonly children: InlineNode[];
|
|
161
|
+
readonly source?: ContentSource;
|
|
95
162
|
}
|
|
96
163
|
export interface BlockQuoteNode {
|
|
97
164
|
readonly type: 'blockQuote';
|
|
@@ -114,6 +181,8 @@ export interface ListItemNode {
|
|
|
114
181
|
readonly task: boolean;
|
|
115
182
|
readonly checked: boolean;
|
|
116
183
|
readonly children: BlockNode[];
|
|
184
|
+
/** The item's source lines, the first holding its marker. */
|
|
185
|
+
readonly lines?: SourceLines;
|
|
117
186
|
}
|
|
118
187
|
export interface CodeBlockNode {
|
|
119
188
|
readonly type: 'codeBlock';
|
|
@@ -122,17 +191,22 @@ export interface CodeBlockNode {
|
|
|
122
191
|
readonly info: string;
|
|
123
192
|
/** Code text. Ends with "\n" unless empty. */
|
|
124
193
|
readonly value: string;
|
|
194
|
+
readonly source?: ContentSource;
|
|
195
|
+
/** The block's source lines: for a fenced block, from the opening fence to the closing one, or to the last line it has. */
|
|
196
|
+
readonly lines?: SourceLines;
|
|
125
197
|
}
|
|
126
198
|
export interface HtmlBlockNode {
|
|
127
199
|
readonly type: 'htmlBlock';
|
|
128
200
|
/** Raw HTML, verbatim, without the final line ending. */
|
|
129
201
|
readonly value: string;
|
|
202
|
+
readonly source?: ContentSource;
|
|
130
203
|
}
|
|
131
204
|
/** A block of a block syntax extension (`name`): its source lines, verbatim, without the final line ending. */
|
|
132
205
|
export interface RawNode {
|
|
133
206
|
readonly type: 'raw';
|
|
134
207
|
readonly name: string;
|
|
135
208
|
readonly value: string;
|
|
209
|
+
readonly source?: ContentSource;
|
|
136
210
|
}
|
|
137
211
|
export type TableAlign = Align;
|
|
138
212
|
export interface TableNode {
|
|
@@ -140,6 +214,8 @@ export interface TableNode {
|
|
|
140
214
|
readonly align: TableAlign[];
|
|
141
215
|
/** `rows[0]` is the header row. Every row has `align.length` cells. */
|
|
142
216
|
readonly rows: InlineNode[][][];
|
|
217
|
+
/** The table's source lines: the header row, the delimiter row and the body rows. */
|
|
218
|
+
readonly lines?: SourceLines;
|
|
143
219
|
}
|
|
144
220
|
/** Source lines of a top-level block, 1-based and inclusive. */
|
|
145
221
|
export interface SourceLines {
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import { type Block, type CalloutType, type Span } from '../model.js';
|
|
2
2
|
import { type BlockNode, type InlineNode } from './ast.js';
|
|
3
3
|
import { type MarkdownOptions } from './options.js';
|
|
4
|
+
import { type LowerContext, SegmentBuilder } from './sourceMap.js';
|
|
4
5
|
/**
|
|
5
6
|
* Lowers one top-level block node. Structure the model cannot hold becomes
|
|
6
7
|
* one raw Markdown block holding `source`, the node's Markdown. `before` is
|
|
7
8
|
* the last block lowered before it.
|
|
8
9
|
*/
|
|
9
10
|
export declare function lowerTopLevel(node: BlockNode, options: MarkdownOptions, source: string, before?: Block): Block[];
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Lowers one block node, or returns null when the model cannot hold its
|
|
13
|
+
* structure (see `fitsModel`). With `ctx`, each block that holds text gets a
|
|
14
|
+
* source map (see `sourceMap.ts`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function lowerIfFits(node: BlockNode, options: MarkdownOptions, before?: Block, ctx?: LowerContext): Block[] | null;
|
|
17
|
+
/** With `map`, where each span's text was written is recorded as it is lowered. */
|
|
18
|
+
export declare function lowerInlines(nodes: readonly InlineNode[], options?: MarkdownOptions, map?: SegmentBuilder): Span[];
|
|
19
|
+
/** The language of a fence: the first word of its info string. */
|
|
20
|
+
export declare function firstWord(info: string): string;
|
|
13
21
|
/**
|
|
14
22
|
* A GitHub alert: the first paragraph starts with a marker such as `[!NOTE]`
|
|
15
23
|
* alone on its line. Returns its type and the paragraph's content after the marker line.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Range } from './ast.js';
|
|
2
|
+
import type { MarkdownOptions } from './options.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a range of the text is: `marker` is syntax (delimiters, brackets,
|
|
5
|
+
* list and quote markers, `#`, fences, pipes, a hard break's backslash);
|
|
6
|
+
* the others are the text the markup marks, and `lang` a fence's info
|
|
7
|
+
* string, `escape` an escape or entity, `url` a destination or bare URL,
|
|
8
|
+
* `html` raw HTML and `token` an extension's syntax.
|
|
9
|
+
*/
|
|
10
|
+
export type MarkupKind = 'marker' | 'heading' | 'em' | 'strong' | 'strike' | 'code' | 'link' | 'url' | 'html' | 'token' | 'escape' | 'lang';
|
|
11
|
+
export interface MarkupRange extends Range {
|
|
12
|
+
readonly kind: MarkupKind;
|
|
13
|
+
}
|
|
14
|
+
/** The content of a fenced or indented code block, for a code highlighter. */
|
|
15
|
+
export interface CodeRange extends Range {
|
|
16
|
+
readonly language: string;
|
|
17
|
+
}
|
|
18
|
+
export interface Markup {
|
|
19
|
+
/** In order of `from`; ranges of different kinds may nest (emphasis in a link). */
|
|
20
|
+
readonly ranges: readonly MarkupRange[];
|
|
21
|
+
readonly code: readonly CodeRange[];
|
|
22
|
+
}
|
|
23
|
+
/** The Markdown syntax in `text`, as the parser reads it with `options`. */
|
|
24
|
+
export declare function markdownMarkup(text: string, options?: MarkdownOptions): Markup;
|
|
@@ -70,7 +70,9 @@ export interface BlockSyntax {
|
|
|
70
70
|
export interface BlockSyntaxMatcher {
|
|
71
71
|
/**
|
|
72
72
|
* For each following line: `continue` adds it, `close` adds it and ends
|
|
73
|
-
* the block, `end` ends the block before it.
|
|
73
|
+
* the block, `end` ends the block before it. Blank lines at the end of the
|
|
74
|
+
* block are dropped from it, so a matcher may continue through a blank
|
|
75
|
+
* line to see whether the block goes on after it.
|
|
74
76
|
*/
|
|
75
77
|
next(line: string): 'continue' | 'close' | 'end';
|
|
76
78
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Block, type Doc } from '../model.js';
|
|
2
|
+
import { type Segment } from './sourceMap.js';
|
|
2
3
|
import { type MarkdownOptions } from './options.js';
|
|
3
4
|
export interface SourceGroup {
|
|
4
5
|
/** Position among the groups of the source. */
|
|
@@ -21,9 +22,14 @@ export interface MarkdownSource {
|
|
|
21
22
|
readonly groupOf: WeakMap<Block, SourceGroup>;
|
|
22
23
|
/** Source of every link reference definition, so a kept block's reference links keep working. */
|
|
23
24
|
readonly definitions: readonly string[];
|
|
25
|
+
/** Where each loaded block's text is in `text` (see `sourceMap.ts`). Tables and blocks with no text have none. */
|
|
26
|
+
readonly mapOf: WeakMap<Block, readonly Segment[]>;
|
|
24
27
|
}
|
|
25
|
-
/**
|
|
26
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Parses Markdown and remembers its source, for `serializeMarkdown(doc, { source })`.
|
|
30
|
+
* `maps` false skips the source maps, for a caller that keeps only the document.
|
|
31
|
+
*/
|
|
32
|
+
export declare function loadMarkdown(text: string, options?: MarkdownOptions, maps?: boolean): {
|
|
27
33
|
doc: Doc;
|
|
28
34
|
source: MarkdownSource;
|
|
29
35
|
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Block } from '../model.js';
|
|
2
|
+
import { type ContentSource, type InlineNode, type Range } from './ast.js';
|
|
3
|
+
export interface Segment {
|
|
4
|
+
/** Range in the block's text. */
|
|
5
|
+
readonly from: number;
|
|
6
|
+
readonly to: number;
|
|
7
|
+
/** Range in the source text. */
|
|
8
|
+
readonly srcFrom: number;
|
|
9
|
+
readonly srcTo: number;
|
|
10
|
+
/** True when the text was written as such, offset for offset. */
|
|
11
|
+
readonly linear: boolean;
|
|
12
|
+
/** For a line break: where its next line starts in the source. An offset from there maps after the break, not before it. */
|
|
13
|
+
readonly nextLine?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Where each 1-based line of a text starts, and where it ends before its line ending. A byte order mark is not on line 1. */
|
|
16
|
+
export interface LineTable {
|
|
17
|
+
readonly starts: readonly number[];
|
|
18
|
+
readonly ends: readonly number[];
|
|
19
|
+
}
|
|
20
|
+
export declare function lineTable(text: string): LineTable;
|
|
21
|
+
/** What lowering needs to record source maps: the line table and, per block, the map. */
|
|
22
|
+
export interface LowerContext {
|
|
23
|
+
readonly lines: LineTable;
|
|
24
|
+
readonly maps: WeakMap<Block, readonly Segment[]>;
|
|
25
|
+
}
|
|
26
|
+
/** One segment: the text `[from, to)` is the source `[srcFrom, srcTo)`, written as such. */
|
|
27
|
+
export declare function identitySegment(from: number, to: number, srcFrom: number): Segment;
|
|
28
|
+
/**
|
|
29
|
+
* Builds the segments of one leaf block from the ranges of its inline nodes,
|
|
30
|
+
* which count from the leaf content's start (see `ContentSource`).
|
|
31
|
+
*/
|
|
32
|
+
export declare class SegmentBuilder {
|
|
33
|
+
private readonly content;
|
|
34
|
+
private readonly table;
|
|
35
|
+
readonly segments: Segment[];
|
|
36
|
+
/** How much of the block's text is recorded so far. */
|
|
37
|
+
private at;
|
|
38
|
+
constructor(content: ContentSource, table: LineTable);
|
|
39
|
+
/** Records where the next `length` characters of the block's text, lowered from `node`, were written. A node without a position (not from the parser) is syntax to the map. */
|
|
40
|
+
record(node: InlineNode, length: number): void;
|
|
41
|
+
/** The text from `modelFrom` was written as the content's `[from, to)`: one segment per source line, the line endings and tab padding atomic. */
|
|
42
|
+
linear(modelFrom: number, from: number, to: number): void;
|
|
43
|
+
/** The text `[modelFrom, modelTo)` stands for the content's `[from, to)` as a whole. */
|
|
44
|
+
atomic(modelFrom: number, modelTo: number, from: number, to: number): void;
|
|
45
|
+
/** The source offset of a content offset: a line ending maps to the end of its line. */
|
|
46
|
+
source(offset: number): number;
|
|
47
|
+
/** The index of the content line holding content offset `o`: the last one starting at or before it. */
|
|
48
|
+
private lineIndex;
|
|
49
|
+
private push;
|
|
50
|
+
}
|
|
51
|
+
/** The source ranges of the content's `[from, to)`, one per line it spans, without line endings or the lines' prefixes. */
|
|
52
|
+
export declare function sourcePieces(content: ContentSource, table: LineTable, from: number, to: number): Range[];
|
|
53
|
+
/**
|
|
54
|
+
* The source offset for an offset in the block's text. Between two segments
|
|
55
|
+
* (text whose nodes were made elsewhere than by the parser) the offset goes
|
|
56
|
+
* to the next segment's source; at the end of a segment it stays before any
|
|
57
|
+
* syntax that follows.
|
|
58
|
+
*/
|
|
59
|
+
export declare function toSource(segments: readonly Segment[], offset: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* The offset in the block's text for a source offset. On syntax, the offset
|
|
62
|
+
* goes to the text after it; inside something atomic, before it.
|
|
63
|
+
*/
|
|
64
|
+
export declare function fromSource(segments: readonly Segment[], offset: number): number;
|
|
65
|
+
/** The source range the block's text covers, from its first segment to its last; null for a block with no map. */
|
|
66
|
+
export declare function sourceSpan(segments: readonly Segment[] | null | undefined): Range | null;
|
|
@@ -2,7 +2,7 @@ import { Editor, type EditorOptions, type SuggestionState } from '@usememos/edit
|
|
|
2
2
|
import { type CSSProperties } from 'react';
|
|
3
3
|
export type { Editor, SuggestionState } from '@usememos/editor';
|
|
4
4
|
/** Constructor options that the React components take as props. */
|
|
5
|
-
type EditorProps = Pick<EditorOptions, 'markdown' | 'extensions' | 'placeholder' | 'mode' | 'smartLists' | 'editable' | 'taskToggle' | 'autofocus' | 'attributes' | 'resolveUrl' | 'referrerPolicy' | 'headingIds' | 'onLinkClick' | 'theme' | 'toolbar' | 'linkCard' | 'slashMenu' | 'slashHint' | 'labels' | 'icons'>;
|
|
5
|
+
type EditorProps = Pick<EditorOptions, 'markdown' | 'extensions' | 'placeholder' | 'mode' | 'smartLists' | 'sourceHighlighting' | 'editable' | 'taskToggle' | 'autofocus' | 'attributes' | 'resolveUrl' | 'referrerPolicy' | 'headingIds' | 'onLinkClick' | 'theme' | 'toolbar' | 'linkCard' | 'slashMenu' | 'slashHint' | 'labels' | 'icons'>;
|
|
6
6
|
export interface MarkdownEditorProps extends EditorProps {
|
|
7
7
|
/** Markdown. The editor updates only when this is not a value it emitted itself. */
|
|
8
8
|
value?: string;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type Decoration, type Decorator } from './decorations.js';
|
|
2
|
+
import type { Extension } from './extension.js';
|
|
3
|
+
import type { CodeBlock, Inline } from './model.js';
|
|
4
|
+
import { type Markup, type MarkupKind } from './markdown/markup.js';
|
|
5
|
+
import type { MarkdownOptions } from './markdown/options.js';
|
|
6
|
+
/** Up to this many characters, the decorations are computed on every keystroke; above, a keystroke shows the last ones moved through its edit, and the exact ones follow. */
|
|
7
|
+
export declare const SYNC_LIMIT = 20000;
|
|
8
|
+
/** What makes up the decorations of source mode's block. */
|
|
9
|
+
export interface SourceDecorationConfig {
|
|
10
|
+
/** The built-in highlighting (`sourceHighlighting`). */
|
|
11
|
+
readonly highlight: boolean;
|
|
12
|
+
/** The theme's class per kind of markup. */
|
|
13
|
+
readonly classes?: Readonly<Partial<Record<MarkupKind, string>>>;
|
|
14
|
+
/** The extensions' decorators, run over the text split at its syntax. */
|
|
15
|
+
readonly decorators: readonly Decorator[];
|
|
16
|
+
/** The extensions' code decorators, run over the code of each fence. */
|
|
17
|
+
readonly codeDecorators: readonly NonNullable<Extension['codeDecorations']>[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The decorations of source mode's block: the Markdown's own highlighting,
|
|
21
|
+
* then the decorators over the text split at its syntax, and the code
|
|
22
|
+
* decorators over each code block in it, their offsets moved and clipped to
|
|
23
|
+
* the code.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sourceDecorations(block: CodeBlock, text: string, markup: Markup, config: SourceDecorationConfig): Decoration[];
|
|
26
|
+
/**
|
|
27
|
+
* Source mode's decorations for one editor, computed once per text.
|
|
28
|
+
* Computing them parses the whole text and runs every decorator over it,
|
|
29
|
+
* about a quarter of a frame per 100 KB, so in a long text a keystroke gets
|
|
30
|
+
* the previous decorations moved through its edit (right where nothing
|
|
31
|
+
* changed; the edited range follows) and the exact ones are computed once
|
|
32
|
+
* typing pauses, when `onReady` has the block rendered again if they differ.
|
|
33
|
+
*/
|
|
34
|
+
export declare class SourceHighlighter {
|
|
35
|
+
private readonly options;
|
|
36
|
+
private readonly config;
|
|
37
|
+
private readonly onReady;
|
|
38
|
+
private last;
|
|
39
|
+
private markupFor;
|
|
40
|
+
private spansFor;
|
|
41
|
+
private pending;
|
|
42
|
+
constructor(options: MarkdownOptions | undefined, config: () => SourceDecorationConfig, onReady: () => void);
|
|
43
|
+
/** The markup of the text, read once per text: the decorations and autocomplete share it. */
|
|
44
|
+
markup(text: string): Markup;
|
|
45
|
+
/**
|
|
46
|
+
* The text as spans split at its syntax (`sourceInline`), for the
|
|
47
|
+
* suggestion controller, once per text. In a long text being typed in, the
|
|
48
|
+
* last markup read is moved through the edit, as the decorations are, so
|
|
49
|
+
* the popup's check costs no parse on a keystroke.
|
|
50
|
+
*/
|
|
51
|
+
spans(text: string): Inline;
|
|
52
|
+
decorations(block: CodeBlock): Decoration[];
|
|
53
|
+
/** Forgets the decorations, for when what they depend on changed (`redecorate`). */
|
|
54
|
+
reset(): void;
|
|
55
|
+
destroy(): void;
|
|
56
|
+
private read;
|
|
57
|
+
private schedule;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Ranges over `before` moved through the one edit that made `after`: ranges
|
|
61
|
+
* before the edit stay, ranges after it move by the edit's length change, a
|
|
62
|
+
* range around it grows or shrinks with it, and ranges inside it go. Null
|
|
63
|
+
* when the edit is too large for that to be worth it.
|
|
64
|
+
*/
|
|
65
|
+
export declare function shiftRanges<T extends {
|
|
66
|
+
readonly from: number;
|
|
67
|
+
readonly to: number;
|
|
68
|
+
}>(ranges: readonly T[], before: string, after: string): T[] | null;
|
|
69
|
+
/** The highlighting decorations: the built-in class per kind, and the theme's class for it. */
|
|
70
|
+
export declare function highlightDecorations(markup: Markup, classes?: Readonly<Partial<Record<MarkupKind, string>>>): Decoration[];
|
|
71
|
+
/**
|
|
72
|
+
* The source text as spans split at its syntax: markup, code, URLs, raw
|
|
73
|
+
* HTML, tokens and escapes are spans a decorator skips (`isExcluded`), and
|
|
74
|
+
* the prose between them plain spans, so `tags()` and `mentions()` see the
|
|
75
|
+
* same runs as in rich mode.
|
|
76
|
+
*/
|
|
77
|
+
export declare function sourceInline(text: string, markup: Markup): Inline;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type MarkdownSource } from './markdown/source.js';
|
|
2
|
+
import type { Range } from './markdown/ast.js';
|
|
3
|
+
import { type Segment } from './markdown/sourceMap.js';
|
|
2
4
|
import { type Doc, type EditorState, type Position, type Selection } from './model.js';
|
|
3
5
|
/** The ID of the one block that holds the text in source mode. */
|
|
4
6
|
export declare const SOURCE_ID = "mbe-source";
|
|
@@ -42,6 +44,12 @@ export declare function sourceEnter(state: EditorState): EditorState;
|
|
|
42
44
|
*/
|
|
43
45
|
export declare function sourceIndentList(state: EditorState, delta: 1 | -1): EditorState | null;
|
|
44
46
|
/** Where an offset in `before` moves to in `after`, when the change between them is one replaced range. */
|
|
47
|
+
/** The one range an edit replaced: `[from, toBefore)` of `before` became `[from, toAfter)` of `after`. */
|
|
48
|
+
export declare function changedRange(before: string, after: string): {
|
|
49
|
+
from: number;
|
|
50
|
+
toBefore: number;
|
|
51
|
+
toAfter: number;
|
|
52
|
+
};
|
|
45
53
|
export declare function mapOffset(offset: number, before: string, after: string): number;
|
|
46
54
|
interface Placed {
|
|
47
55
|
/** Where the block's text starts in the source, or its source's start. */
|
|
@@ -49,18 +57,27 @@ interface Placed {
|
|
|
49
57
|
/** Where the block's source ends. */
|
|
50
58
|
readonly end: number;
|
|
51
59
|
readonly text: string | null;
|
|
60
|
+
/** The block's source map and the source range its text covers, when loading made one (see `markdown/sourceMap.ts`). */
|
|
61
|
+
readonly segments: readonly Segment[] | null;
|
|
62
|
+
readonly span: Range | null;
|
|
52
63
|
}
|
|
53
64
|
/**
|
|
54
65
|
* The offset in `loaded`'s text for a position in a rich document whose
|
|
55
|
-
* blocks match `loaded`'s by index.
|
|
56
|
-
*
|
|
57
|
-
* to the
|
|
66
|
+
* blocks match `loaded`'s by index. With the block's source map, the caret
|
|
67
|
+
* keeps its place in the text, past any syntax before it (an offset on
|
|
68
|
+
* syntax goes to the text after it). Without one, a table or a section, it
|
|
69
|
+
* keeps its column when the block's text before it appears verbatim in the
|
|
70
|
+
* source; otherwise it goes to the start of the block's text.
|
|
58
71
|
*/
|
|
59
72
|
export declare function toSourceOffset(doc: Doc, pos: Position, loaded: {
|
|
60
73
|
doc: Doc;
|
|
61
74
|
source: MarkdownSource;
|
|
62
75
|
}, placed?: Placed[]): number;
|
|
63
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* The position in `loaded`'s document for an offset in its text: the block
|
|
78
|
+
* whose source holds it, at the same place in its text. An offset on syntax
|
|
79
|
+
* goes to the text after it, and one inside an image or an escape before it.
|
|
80
|
+
*/
|
|
64
81
|
export declare function fromSourceOffset(offset: number, loaded: {
|
|
65
82
|
doc: Doc;
|
|
66
83
|
source: MarkdownSource;
|
|
@@ -83,4 +100,41 @@ export declare function blockEndOffset(doc: Doc, blockId: string, loaded: {
|
|
|
83
100
|
doc: Doc;
|
|
84
101
|
source: MarkdownSource;
|
|
85
102
|
}): number;
|
|
103
|
+
export type WrapMarker = '**' | '*' | '`' | '~~';
|
|
104
|
+
/**
|
|
105
|
+
* Wraps the selection in `marker`, or unwraps it when it is already wrapped,
|
|
106
|
+
* whether or not the markers are selected. The wrapped range is trimmed of
|
|
107
|
+
* whitespace, since `** foo**` is not strong. With no selection, the word at
|
|
108
|
+
* the caret; on whitespace, the pair is inserted with the caret between the
|
|
109
|
+
* markers. Italic inside strong adds its own star, so `**bold**` becomes
|
|
110
|
+
* `***bold***`, and a run of three unwraps one. A code span's backticks are
|
|
111
|
+
* one more than any run inside it.
|
|
112
|
+
*/
|
|
113
|
+
export declare function sourceToggleWrap(state: EditorState, marker: WrapMarker): EditorState;
|
|
114
|
+
/**
|
|
115
|
+
* Sets the heading level of the selected lines, after any quote markers: the
|
|
116
|
+
* `#`s replace a list marker or another level, and a level every line already
|
|
117
|
+
* has, or level 0, removes them, as `setParagraph` does.
|
|
118
|
+
*/
|
|
119
|
+
export declare function sourceSetHeading(state: EditorState, level: number): EditorState | null;
|
|
120
|
+
export type SourceListKind = 'bullet' | 'ordered' | 'task';
|
|
121
|
+
/**
|
|
122
|
+
* Makes the selected lines list items of `kind`, after any quote markers,
|
|
123
|
+
* replacing another kind's marker or a heading's `#`s; when every line is one
|
|
124
|
+
* already, removes the markers. New ordered items are numbered from the
|
|
125
|
+
* list they join, or from 1.
|
|
126
|
+
*/
|
|
127
|
+
export declare function sourceToggleList(state: EditorState, kind: SourceListKind): EditorState | null;
|
|
128
|
+
/** Quotes the selected lines with `> `, or, when every line that has text is quoted, removes one quote marker from each. */
|
|
129
|
+
export declare function sourceToggleQuote(state: EditorState): EditorState | null;
|
|
130
|
+
/**
|
|
131
|
+
* Wraps the selected lines in a code fence, keeping their quote markers; a
|
|
132
|
+
* blank line becomes an empty fence with the caret in it. When the selection
|
|
133
|
+
* is in fenced code, removes that fence's lines.
|
|
134
|
+
*/
|
|
135
|
+
export declare function sourceToggleFence(state: EditorState): EditorState | null;
|
|
136
|
+
/** Makes the selected text a link to `url`: `[text](url)`, with the caret after it. Nothing without a selection. */
|
|
137
|
+
export declare function sourceLink(state: EditorState, url: string): EditorState | null;
|
|
138
|
+
/** True when the selection starts in fenced code, where a paste should stay the text as written. */
|
|
139
|
+
export declare function inFence(state: EditorState): boolean;
|
|
86
140
|
export {};
|
package/dist/types/theme.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { HeadingLevel } from './model.js';
|
|
2
|
+
import type { MarkupKind } from './markdown/markup.js';
|
|
2
3
|
/**
|
|
3
4
|
* Classes added to each rendered element of a type, next to the editor's own
|
|
4
5
|
* `mbe-*` classes, in editing and read-only alike. Keys use the names of
|
|
@@ -36,6 +37,8 @@ export interface ThemeClasses {
|
|
|
36
37
|
readonly link?: string;
|
|
37
38
|
/** Inline tokens of an extension syntax, as source or rendered, by syntax name, such as `math`. */
|
|
38
39
|
readonly token?: Readonly<Record<string, string>>;
|
|
40
|
+
/** Source mode's highlighting, by kind of markup (see `MarkupKind`): `marker`, `heading`, `strong`, `em`, `code`, `url` and the rest. */
|
|
41
|
+
readonly source?: Readonly<Partial<Record<MarkupKind, string>>>;
|
|
39
42
|
}
|
|
40
43
|
/** Colors for one color scheme. Any CSS color. */
|
|
41
44
|
export interface ThemeColors {
|
|
@@ -76,6 +79,8 @@ export interface ThemeTokens {
|
|
|
76
79
|
readonly dark?: ThemeColors;
|
|
77
80
|
readonly font?: string;
|
|
78
81
|
readonly mono?: string;
|
|
82
|
+
/** The font of source mode's text. Defaults to `font`; set it to `mono` for an all-monospace source view. */
|
|
83
|
+
readonly sourceFont?: string;
|
|
79
84
|
readonly fontSize?: string | number;
|
|
80
85
|
readonly lineHeight?: string | number;
|
|
81
86
|
readonly radius?: string | number;
|
package/dist/types/view/dom.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface RenderContext {
|
|
|
30
30
|
readonly decorate?: (block: Block, content: Inline, cell?: Cell) => readonly Decoration[];
|
|
31
31
|
/** Decorations for a code block's code, such as syntax highlighting. */
|
|
32
32
|
readonly decorateCode?: (block: CodeBlock) => readonly Decoration[];
|
|
33
|
+
/** Decorations for source mode's one block: the Markdown's highlighting, and the extensions' decorations over its text. */
|
|
34
|
+
readonly decorateSource?: (block: CodeBlock) => readonly Decoration[];
|
|
33
35
|
/** Syntaxes that have a view. */
|
|
34
36
|
readonly views?: ReadonlyMap<string, SyntaxView>;
|
|
35
37
|
/** A token or raw block of an extension syntax, rendered; null shows its source. */
|
|
@@ -108,6 +110,8 @@ export declare class BlockRenderer {
|
|
|
108
110
|
decorate?: RenderContext['decorate'];
|
|
109
111
|
/** Decorations for a code block's code. */
|
|
110
112
|
decorateCode?: RenderContext['decorateCode'];
|
|
113
|
+
/** Decorations for source mode's block. */
|
|
114
|
+
decorateSource?: RenderContext['decorateSource'];
|
|
111
115
|
/**
|
|
112
116
|
* Called when views that returned a promise settle, with the blocks that
|
|
113
117
|
* showed them. Those blocks must render again to show the finished view,
|
|
@@ -118,6 +122,14 @@ export declare class BlockRenderer {
|
|
|
118
122
|
widgetsAfter?: (blockId: string) => readonly HTMLElement[] | undefined;
|
|
119
123
|
/** Views of extension syntaxes, by syntax name. */
|
|
120
124
|
views?: ReadonlyMap<string, SyntaxView>;
|
|
125
|
+
/**
|
|
126
|
+
* Read-only: footnote definitions hide where they are written and the
|
|
127
|
+
* referenced ones show again, in order of first reference, in a section
|
|
128
|
+
* at the end of the document (see `footnoteSection`).
|
|
129
|
+
*/
|
|
130
|
+
collectFootnotes: boolean;
|
|
131
|
+
/** The last footnote section, kept while what it shows is the same. */
|
|
132
|
+
private footnotes?;
|
|
121
133
|
/** What a view gets besides its source: the editor. Set with `views`. */
|
|
122
134
|
viewContext?: ExtensionContext;
|
|
123
135
|
/** Block views of extensions, with the extension's name, in extension order. */
|
|
@@ -200,6 +212,14 @@ export declare class BlockRenderer {
|
|
|
200
212
|
* skip layout while off screen; use it only when loading a document.
|
|
201
213
|
*/
|
|
202
214
|
render(doc: Doc, force?: boolean, lazy?: boolean): void;
|
|
215
|
+
/**
|
|
216
|
+
* The footnotes section, as GitHub renders one: every definition that is
|
|
217
|
+
* referenced, in order of first reference, each rendered as its block is
|
|
218
|
+
* (see `renderFootnote`) with a backlink per reference. Definitions nothing
|
|
219
|
+
* references are left out. Null when there is nothing to show. The element
|
|
220
|
+
* is kept while the definitions and references are the same.
|
|
221
|
+
*/
|
|
222
|
+
private footnoteSection;
|
|
203
223
|
/** Marks blocks again after `collapsed` changed, without rendering any. */
|
|
204
224
|
updateSections(doc: Doc): void;
|
|
205
225
|
elementFor(id: string): HTMLElement | undefined;
|
|
@@ -211,6 +231,10 @@ export declare class BlockRenderer {
|
|
|
211
231
|
adopt(block: Block): void;
|
|
212
232
|
/** Forget a block's element so the next render rebuilds it, e.g. after the browser edited it during IME composition. */
|
|
213
233
|
invalidate(id: string): void;
|
|
234
|
+
/** Brings source mode's rendered block up to date with the block's text and decorations, in place. False when it has no content element. */
|
|
235
|
+
private patchSource;
|
|
236
|
+
/** Renders source mode's block again with the decorations as they are now, without touching the lines whose text and decorations are the same. */
|
|
237
|
+
refreshSource(block: CodeBlock): void;
|
|
214
238
|
}
|
|
215
239
|
export declare function contentElement(blockEl: HTMLElement, cell?: Cell): HTMLElement | null;
|
|
216
240
|
/** Index of a node among its parent's child nodes. */
|