@domternal/extension-markdown 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/LICENSE +21 -0
- package/README.md +71 -0
- package/dist/index.cjs +1123 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +201 -0
- package/dist/index.d.ts +201 -0
- package/dist/index.js +1102 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { CommandSpec, SetContentOptions, Extension, Editor } from '@domternal/core';
|
|
2
|
+
import { Node, Schema, Mark, NodeType, MarkType, Attrs } from '@domternal/pm/model';
|
|
3
|
+
import { Plugin, PluginKey } from '@domternal/pm/state';
|
|
4
|
+
|
|
5
|
+
interface MarkdownParser {
|
|
6
|
+
/** Parse a Markdown string into a document node of the schema. */
|
|
7
|
+
parse(markdown: string): Node;
|
|
8
|
+
}
|
|
9
|
+
declare function createMarkdownParser(schema: Schema): MarkdownParser;
|
|
10
|
+
/** One-shot convenience wrapper around {@link createMarkdownParser}. */
|
|
11
|
+
declare function parseMarkdown(markdown: string, schema: Schema): Node;
|
|
12
|
+
|
|
13
|
+
type MarkdownWarningCode = 'unsupported-node' | 'unsupported-mark' | 'lossy-attribute' | 'lossy-structure';
|
|
14
|
+
/** A non-fatal fidelity loss that happened during conversion. */
|
|
15
|
+
interface MarkdownWarning {
|
|
16
|
+
code: MarkdownWarningCode;
|
|
17
|
+
message: string;
|
|
18
|
+
/** The node or mark type the warning originates from, when known. */
|
|
19
|
+
nodeType?: string;
|
|
20
|
+
}
|
|
21
|
+
/** The serialized document plus everything that did not survive at full fidelity. */
|
|
22
|
+
interface SerializeMarkdownResult {
|
|
23
|
+
markdown: string;
|
|
24
|
+
warnings: MarkdownWarning[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Markdown serializer state machine. Same output semantics as
|
|
29
|
+
* prosemirror-markdown's serializer (delimiter stacking, tight lists, mark
|
|
30
|
+
* mixing, enclosing-whitespace expelling) but built on `@domternal/pm` so the
|
|
31
|
+
* package never pulls in a second prosemirror-model instance.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
type MarkdownNodeSerializer = (state: MarkdownSerializerState, node: Node, parent: Node, index: number) => void;
|
|
35
|
+
type MarkDelimiter = string | ((state: MarkdownSerializerState, mark: Mark, parent: Node, index: number) => string);
|
|
36
|
+
interface MarkdownMarkSpec {
|
|
37
|
+
open: MarkDelimiter;
|
|
38
|
+
close: MarkDelimiter;
|
|
39
|
+
/** Marks that can be reordered against other mixable marks to minimize delimiters. */
|
|
40
|
+
mixable?: boolean;
|
|
41
|
+
/** Move leading/trailing whitespace outside the delimiters (`**a ** b` is invalid emphasis). */
|
|
42
|
+
expelEnclosingWhitespace?: boolean;
|
|
43
|
+
/** Set false when the mark content is raw (inline code). */
|
|
44
|
+
escape?: boolean;
|
|
45
|
+
}
|
|
46
|
+
interface MarkdownSerializerSpecs {
|
|
47
|
+
nodes: Record<string, MarkdownNodeSerializer>;
|
|
48
|
+
marks: Record<string, MarkdownMarkSpec>;
|
|
49
|
+
}
|
|
50
|
+
interface MarkdownSerializerOptions {
|
|
51
|
+
/**
|
|
52
|
+
* Render lists without blank lines between items.
|
|
53
|
+
* @default true
|
|
54
|
+
*/
|
|
55
|
+
tightLists?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/** Longest backtick run inside inline code decides the fence length around it. */
|
|
58
|
+
declare function backticksFor(node: Node, side: -1 | 1): string;
|
|
59
|
+
declare class MarkdownSerializerState {
|
|
60
|
+
readonly nodes: Record<string, MarkdownNodeSerializer>;
|
|
61
|
+
readonly marks: Record<string, MarkdownMarkSpec>;
|
|
62
|
+
readonly warnings: MarkdownWarning[];
|
|
63
|
+
readonly tightLists: boolean;
|
|
64
|
+
private delim;
|
|
65
|
+
private out;
|
|
66
|
+
private closed;
|
|
67
|
+
private inTightList;
|
|
68
|
+
private atBlockStart;
|
|
69
|
+
constructor(specs: MarkdownSerializerSpecs, options?: MarkdownSerializerOptions);
|
|
70
|
+
warn(code: MarkdownWarningCode, message: string, nodeType?: string): void;
|
|
71
|
+
private flushClose;
|
|
72
|
+
/**
|
|
73
|
+
* Render a block wrapped in a line prefix: `firstDelim` on its first line,
|
|
74
|
+
* `delim` on every following line (blockquote `> `, list item indent).
|
|
75
|
+
*/
|
|
76
|
+
wrapBlock(delim: string, firstDelim: string | null, node: Node, f: () => void): void;
|
|
77
|
+
atBlank(): boolean;
|
|
78
|
+
ensureNewLine(): void;
|
|
79
|
+
write(content?: string): void;
|
|
80
|
+
/** Close the block: the next write emits the separating blank line. */
|
|
81
|
+
closeBlock(node: Node): void;
|
|
82
|
+
text(text: string, escape?: boolean): void;
|
|
83
|
+
render(node: Node, parent: Node, index: number): void;
|
|
84
|
+
renderContent(parent: Node): void;
|
|
85
|
+
renderInline(parent: Node, fromBlockStart?: boolean): void;
|
|
86
|
+
renderList(node: Node, delim: string, firstDelim: (index: number) => string): void;
|
|
87
|
+
/**
|
|
88
|
+
* Escape Markdown syntax in plain text. Beyond the CommonMark set this
|
|
89
|
+
* also escapes `|` (table cells), `$` (this package's own math rules), and
|
|
90
|
+
* `<` plus entity-like `&` (raw-HTML/autolink/entity ambiguity on external
|
|
91
|
+
* renderers): all render as the literal character everywhere.
|
|
92
|
+
*/
|
|
93
|
+
esc(str: string, startOfLine?: boolean): string;
|
|
94
|
+
markString(mark: Mark, open: boolean, parent: Node, index: number): string;
|
|
95
|
+
/** The accumulated output; call once after rendering the document. */
|
|
96
|
+
finish(): string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface MarkdownSerializerSpecOverrides {
|
|
100
|
+
/** Extra or replacement node serializers, merged over the defaults. */
|
|
101
|
+
nodes?: Record<string, MarkdownNodeSerializer>;
|
|
102
|
+
/** Extra or replacement mark specs, merged over the defaults. */
|
|
103
|
+
marks?: Record<string, MarkdownMarkSpec>;
|
|
104
|
+
}
|
|
105
|
+
interface MarkdownSerializer {
|
|
106
|
+
/** Serialize a document (or any block node) to Markdown. */
|
|
107
|
+
serialize(node: Node): SerializeMarkdownResult;
|
|
108
|
+
}
|
|
109
|
+
declare function createMarkdownSerializer(specs?: MarkdownSerializerSpecOverrides, options?: MarkdownSerializerOptions): MarkdownSerializer;
|
|
110
|
+
interface SerializeMarkdownOptions extends MarkdownSerializerOptions {
|
|
111
|
+
specs?: MarkdownSerializerSpecOverrides;
|
|
112
|
+
}
|
|
113
|
+
/** One-shot convenience wrapper around {@link createMarkdownSerializer}. */
|
|
114
|
+
declare function serializeMarkdown(node: Node, options?: SerializeMarkdownOptions): SerializeMarkdownResult;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Markdown extension: bidirectional Markdown for the editor. Parsing and
|
|
118
|
+
* serialization are also available headlessly through createMarkdownParser
|
|
119
|
+
* and createMarkdownSerializer; the extension wires them to commands, paste,
|
|
120
|
+
* and a shared storage other packages can reuse.
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
declare module '@domternal/core' {
|
|
124
|
+
interface RawCommands {
|
|
125
|
+
insertMarkdown: CommandSpec<[markdown: string]>;
|
|
126
|
+
setMarkdownContent: CommandSpec<[markdown: string, options?: SetContentOptions]>;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
interface MarkdownOptions {
|
|
130
|
+
/**
|
|
131
|
+
* Convert Markdown-looking plain-text pastes into rich content. Pastes
|
|
132
|
+
* that carry an HTML flavor, plain prose, and pastes into code blocks are
|
|
133
|
+
* never touched.
|
|
134
|
+
* @default true
|
|
135
|
+
*/
|
|
136
|
+
paste: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Serialize lists without blank lines between items.
|
|
139
|
+
* @default true
|
|
140
|
+
*/
|
|
141
|
+
tightLists: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Extra or replacement Markdown mappings for custom nodes and marks,
|
|
144
|
+
* merged over the built-in ones.
|
|
145
|
+
* @default null
|
|
146
|
+
*/
|
|
147
|
+
specs: MarkdownSerializerSpecOverrides | null;
|
|
148
|
+
}
|
|
149
|
+
interface MarkdownStorage {
|
|
150
|
+
/** Shared parser instance; built on editor create, reusable by other packages. */
|
|
151
|
+
parser: MarkdownParser | null;
|
|
152
|
+
/** Shared serializer instance; built on editor create, reusable by other packages. */
|
|
153
|
+
serializer: MarkdownSerializer | null;
|
|
154
|
+
}
|
|
155
|
+
/** Serialize the editor's current document to Markdown. */
|
|
156
|
+
declare function getMarkdown(editor: Editor): SerializeMarkdownResult;
|
|
157
|
+
declare const Markdown: Extension<MarkdownOptions, MarkdownStorage>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Serialize the document and hand it to the browser as a .md download.
|
|
161
|
+
* Returns the serialization result so callers can surface the warnings.
|
|
162
|
+
*/
|
|
163
|
+
declare function downloadMarkdown(editor: Editor, fileName?: string): SerializeMarkdownResult;
|
|
164
|
+
|
|
165
|
+
declare const markdownPastePluginKey: PluginKey<any>;
|
|
166
|
+
declare function looksLikeMarkdown(text: string): boolean;
|
|
167
|
+
declare function markdownPastePlugin(getParser: () => MarkdownParser): Plugin;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Markdown parse state: token stream in, ProseMirror nodes out. Same shape as
|
|
171
|
+
* prosemirror-markdown's parse state, built on `@domternal/pm` types.
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
declare class MarkdownParseState {
|
|
175
|
+
readonly schema: Schema;
|
|
176
|
+
private readonly stack;
|
|
177
|
+
private marks;
|
|
178
|
+
constructor(schema: Schema);
|
|
179
|
+
private top;
|
|
180
|
+
/** The node type currently being built (for split decisions). */
|
|
181
|
+
topType(): NodeType;
|
|
182
|
+
push(node: Node): void;
|
|
183
|
+
addText(text: string): void;
|
|
184
|
+
openMark(mark: Mark): void;
|
|
185
|
+
closeMark(type: MarkType): void;
|
|
186
|
+
addNode(type: NodeType, attrs: Attrs | null, content?: Node[]): Node | null;
|
|
187
|
+
openNode(type: NodeType, attrs?: Attrs | null, discardWhenEmpty?: boolean): void;
|
|
188
|
+
closeNode(): Node | null;
|
|
189
|
+
/**
|
|
190
|
+
* Close the innermost frame and reopen the same type after inserting
|
|
191
|
+
* `node` as a sibling: how a block-level image escapes its paragraph.
|
|
192
|
+
*/
|
|
193
|
+
splitAround(node: Node): void;
|
|
194
|
+
/** Build the final document; called once after all tokens are consumed. */
|
|
195
|
+
finish(): Node;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
declare const defaultNodeSerializers: Record<string, MarkdownNodeSerializer>;
|
|
199
|
+
declare const defaultMarkSpecs: Record<string, MarkdownMarkSpec>;
|
|
200
|
+
|
|
201
|
+
export { Markdown, type MarkdownMarkSpec, type MarkdownNodeSerializer, type MarkdownOptions, MarkdownParseState, type MarkdownParser, type MarkdownSerializer, type MarkdownSerializerOptions, type MarkdownSerializerSpecOverrides, type MarkdownSerializerSpecs, MarkdownSerializerState, type MarkdownStorage, type MarkdownWarning, type MarkdownWarningCode, type SerializeMarkdownOptions, type SerializeMarkdownResult, backticksFor, createMarkdownParser, createMarkdownSerializer, Markdown as default, defaultMarkSpecs, defaultNodeSerializers, downloadMarkdown, getMarkdown, looksLikeMarkdown, markdownPastePlugin, markdownPastePluginKey, parseMarkdown, serializeMarkdown };
|