@tiptap/markdown 3.20.3 → 3.20.4
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/index.cjs +1110 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +290 -0
- package/dist/index.d.ts +290 -0
- package/dist/index.js +1110 -0
- package/dist/index.js.map +1 -0
- package/package.json +5 -5
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { AnyExtension, JSONContent, Extension, MarkdownToken, Content } from '@tiptap/core';
|
|
2
|
+
import { marked } from 'marked';
|
|
3
|
+
import { Fragment, Node } from '@tiptap/pm/model';
|
|
4
|
+
|
|
5
|
+
declare class MarkdownManager {
|
|
6
|
+
private markedInstance;
|
|
7
|
+
private lexer;
|
|
8
|
+
private registry;
|
|
9
|
+
private nodeTypeRegistry;
|
|
10
|
+
private indentStyle;
|
|
11
|
+
private indentSize;
|
|
12
|
+
private baseExtensions;
|
|
13
|
+
private extensions;
|
|
14
|
+
/**
|
|
15
|
+
* Create a MarkdownManager.
|
|
16
|
+
* @param options.marked Optional marked instance to use (injected).
|
|
17
|
+
* @param options.markedOptions Optional options to pass to marked.setOptions
|
|
18
|
+
* @param options.indentation Indentation settings (style and size).
|
|
19
|
+
* @param options.extensions An array of Tiptap extensions to register for markdown parsing and rendering.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: {
|
|
22
|
+
marked?: typeof marked;
|
|
23
|
+
markedOptions?: Parameters<typeof marked.setOptions>[0];
|
|
24
|
+
indentation?: {
|
|
25
|
+
style?: 'space' | 'tab';
|
|
26
|
+
size?: number;
|
|
27
|
+
};
|
|
28
|
+
extensions: AnyExtension[];
|
|
29
|
+
});
|
|
30
|
+
/** Returns the underlying marked instance. */
|
|
31
|
+
get instance(): typeof marked;
|
|
32
|
+
/** Returns the correct indentCharacter (space or tab) */
|
|
33
|
+
get indentCharacter(): string;
|
|
34
|
+
/** Returns the correct indentString repeated X times */
|
|
35
|
+
get indentString(): string;
|
|
36
|
+
/** Helper to quickly check whether a marked instance is available. */
|
|
37
|
+
hasMarked(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Register a Tiptap extension (Node/Mark/Extension). This will read
|
|
40
|
+
* `markdownName`, `parseMarkdown`, `renderMarkdown` and `priority` from the
|
|
41
|
+
* extension config (using the same resolution used across the codebase).
|
|
42
|
+
*/
|
|
43
|
+
registerExtension(extension: AnyExtension, recreateLexer?: boolean): void;
|
|
44
|
+
/**
|
|
45
|
+
* Register a custom tokenizer with marked.js for parsing non-standard markdown syntax.
|
|
46
|
+
*/
|
|
47
|
+
private registerTokenizer;
|
|
48
|
+
/** Get registered handlers for a token type and try each until one succeeds. */
|
|
49
|
+
private getHandlersForToken;
|
|
50
|
+
/** Get the first handler for a token type (for backwards compatibility). */
|
|
51
|
+
private getHandlerForToken;
|
|
52
|
+
/** Get registered handlers for a node type (for rendering). */
|
|
53
|
+
private getHandlersForNodeType;
|
|
54
|
+
/**
|
|
55
|
+
* Serialize a ProseMirror-like JSON document (or node array) to a Markdown string
|
|
56
|
+
* using registered renderers and fallback renderers.
|
|
57
|
+
*/
|
|
58
|
+
serialize(docOrContent: JSONContent): string;
|
|
59
|
+
/**
|
|
60
|
+
* Check if the markdown output represents an empty document.
|
|
61
|
+
* Empty documents may contain only entities or non-breaking space characters
|
|
62
|
+
* which are used by the Paragraph extension to preserve blank lines.
|
|
63
|
+
*/
|
|
64
|
+
private isEmptyOutput;
|
|
65
|
+
/**
|
|
66
|
+
* Parse markdown string into Tiptap JSON document using registered extension handlers.
|
|
67
|
+
*/
|
|
68
|
+
parse(markdown: string): JSONContent;
|
|
69
|
+
/**
|
|
70
|
+
* Convert an array of marked tokens into Tiptap JSON nodes using registered extension handlers.
|
|
71
|
+
*/
|
|
72
|
+
private parseTokens;
|
|
73
|
+
private createImplicitEmptyParagraphsFromSpace;
|
|
74
|
+
private countParagraphSeparators;
|
|
75
|
+
/**
|
|
76
|
+
* Parse a single token into Tiptap JSON using the appropriate registered handler.
|
|
77
|
+
*/
|
|
78
|
+
private parseToken;
|
|
79
|
+
private lastParseResult;
|
|
80
|
+
/**
|
|
81
|
+
* Parse a list token, handling mixed bullet and task list items by splitting them into separate lists.
|
|
82
|
+
* This ensures that consecutive task items and bullet items are grouped and parsed as separate list nodes.
|
|
83
|
+
*
|
|
84
|
+
* @param token The list token to parse
|
|
85
|
+
* @returns Array of parsed list nodes, or null if parsing fails
|
|
86
|
+
*/
|
|
87
|
+
private parseListToken;
|
|
88
|
+
/**
|
|
89
|
+
* Parse a token using registered handlers (extracted for reuse).
|
|
90
|
+
*/
|
|
91
|
+
private parseTokenWithHandlers;
|
|
92
|
+
/**
|
|
93
|
+
* Creates helper functions for parsing markdown tokens.
|
|
94
|
+
* @returns An object containing helper functions for parsing.
|
|
95
|
+
*/
|
|
96
|
+
private createParseHelpers;
|
|
97
|
+
/**
|
|
98
|
+
* Escape special regex characters in a string.
|
|
99
|
+
*/
|
|
100
|
+
private escapeRegex;
|
|
101
|
+
/**
|
|
102
|
+
* Parse inline tokens (bold, italic, links, etc.) into text nodes with marks.
|
|
103
|
+
* This is the complex part that handles mark nesting and boundaries.
|
|
104
|
+
*/
|
|
105
|
+
private parseInlineTokens;
|
|
106
|
+
/**
|
|
107
|
+
* Apply a mark to content nodes.
|
|
108
|
+
*/
|
|
109
|
+
private applyMarkToContent; /**
|
|
110
|
+
* Check if a parse result represents a mark to be applied.
|
|
111
|
+
*/
|
|
112
|
+
private isMarkResult;
|
|
113
|
+
/**
|
|
114
|
+
* Normalize parse results to ensure they're valid JSONContent.
|
|
115
|
+
*/
|
|
116
|
+
private normalizeParseResult;
|
|
117
|
+
/**
|
|
118
|
+
* Fallback parsing for common tokens when no specific handler is registered.
|
|
119
|
+
*/
|
|
120
|
+
private parseFallbackToken;
|
|
121
|
+
/**
|
|
122
|
+
* Parse HTML tokens using extensions' parseHTML methods.
|
|
123
|
+
* This allows HTML within markdown to be parsed according to extension rules.
|
|
124
|
+
*/
|
|
125
|
+
private parseHTMLToken;
|
|
126
|
+
renderNodeToMarkdown(node: JSONContent, parentNode?: JSONContent, index?: number, level?: number, meta?: Record<string, any>): string;
|
|
127
|
+
/**
|
|
128
|
+
* Render a node or an array of nodes. Parent type controls how children
|
|
129
|
+
* are joined (which determines newline insertion between children).
|
|
130
|
+
*/
|
|
131
|
+
renderNodes(nodeOrNodes: JSONContent | JSONContent[], parentNode?: JSONContent, separator?: string, index?: number, level?: number): string;
|
|
132
|
+
/**
|
|
133
|
+
* Render an array of nodes while properly tracking mark boundaries.
|
|
134
|
+
* This handles cases where marks span across multiple text nodes.
|
|
135
|
+
*/
|
|
136
|
+
private renderNodesWithMarkBoundaries;
|
|
137
|
+
/**
|
|
138
|
+
* Get the opening markdown syntax for a mark type.
|
|
139
|
+
*/
|
|
140
|
+
private getMarkOpening;
|
|
141
|
+
/**
|
|
142
|
+
* Get the closing markdown syntax for a mark type.
|
|
143
|
+
*/
|
|
144
|
+
private getMarkClosing;
|
|
145
|
+
/**
|
|
146
|
+
* Returns the inline HTML tags an extension exposes for overlap-boundary
|
|
147
|
+
* reopen handling, if that mark explicitly opted into HTML reopen mode.
|
|
148
|
+
*/
|
|
149
|
+
private getHtmlReopenTags;
|
|
150
|
+
/**
|
|
151
|
+
* Check if two mark sets are equal.
|
|
152
|
+
*/
|
|
153
|
+
private markSetsEqual;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
type ContentType = 'json' | 'html' | 'markdown';
|
|
157
|
+
|
|
158
|
+
declare module '@tiptap/core' {
|
|
159
|
+
interface Editor {
|
|
160
|
+
/**
|
|
161
|
+
* Get the content of the editor as markdown.
|
|
162
|
+
*/
|
|
163
|
+
getMarkdown: () => string;
|
|
164
|
+
/**
|
|
165
|
+
* The markdown manager instance.
|
|
166
|
+
*/
|
|
167
|
+
markdown?: MarkdownManager;
|
|
168
|
+
}
|
|
169
|
+
interface EditorOptions {
|
|
170
|
+
/**
|
|
171
|
+
* The content type the content is provided as.
|
|
172
|
+
*
|
|
173
|
+
* @default 'json'
|
|
174
|
+
*/
|
|
175
|
+
contentType?: ContentType;
|
|
176
|
+
}
|
|
177
|
+
interface Storage {
|
|
178
|
+
markdown: MarkdownExtensionStorage;
|
|
179
|
+
}
|
|
180
|
+
interface InsertContentOptions {
|
|
181
|
+
/**
|
|
182
|
+
* The content type the content is provided as.
|
|
183
|
+
*
|
|
184
|
+
* @default 'json'
|
|
185
|
+
*/
|
|
186
|
+
contentType?: ContentType;
|
|
187
|
+
}
|
|
188
|
+
interface InsertContentAtOptions {
|
|
189
|
+
/**
|
|
190
|
+
* The content type the content is provided as.
|
|
191
|
+
*
|
|
192
|
+
* @default 'json'
|
|
193
|
+
*/
|
|
194
|
+
contentType?: ContentType;
|
|
195
|
+
}
|
|
196
|
+
interface SetContentOptions {
|
|
197
|
+
/**
|
|
198
|
+
* The content type the content is provided as.
|
|
199
|
+
*
|
|
200
|
+
* @default 'json'
|
|
201
|
+
*/
|
|
202
|
+
contentType?: ContentType;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
type MarkdownExtensionOptions = {
|
|
206
|
+
/**
|
|
207
|
+
* Configure the indentation style and size for lists and code blocks.
|
|
208
|
+
* - `style`: Choose between spaces or tabs. Default is 'space'.
|
|
209
|
+
* - `size`: Number of spaces or tabs for indentation. Default is 2.
|
|
210
|
+
*/
|
|
211
|
+
indentation?: {
|
|
212
|
+
style?: 'space' | 'tab';
|
|
213
|
+
size?: number;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Use a custom version of `marked` for markdown parsing and serialization.
|
|
217
|
+
* If not provided, the default `marked` instance will be used.
|
|
218
|
+
*/
|
|
219
|
+
marked?: typeof marked;
|
|
220
|
+
/**
|
|
221
|
+
* Options to pass to `marked.setOptions()`.
|
|
222
|
+
* See the [marked documentation](https://marked.js.org/using_advanced#options) for available options.
|
|
223
|
+
*/
|
|
224
|
+
markedOptions?: Parameters<typeof marked.setOptions>[0];
|
|
225
|
+
};
|
|
226
|
+
type MarkdownExtensionStorage = {
|
|
227
|
+
manager: MarkdownManager;
|
|
228
|
+
};
|
|
229
|
+
declare const Markdown: Extension<MarkdownExtensionOptions, MarkdownExtensionStorage>;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Wraps each line of the content with the given prefix.
|
|
233
|
+
* @param prefix The prefix to wrap each line with.
|
|
234
|
+
* @param content The content to wrap.
|
|
235
|
+
* @returns The content with each line wrapped with the prefix.
|
|
236
|
+
*/
|
|
237
|
+
declare function wrapInMarkdownBlock(prefix: string, content: string): string;
|
|
238
|
+
/**
|
|
239
|
+
* Identifies marks that need to be closed, based on the marks in the next node.
|
|
240
|
+
*/
|
|
241
|
+
declare function findMarksToClose(currentMarks: Map<string, any>, nextNode: any): string[];
|
|
242
|
+
/**
|
|
243
|
+
* Identifies marks that need to be opened (in current node but not active).
|
|
244
|
+
*/
|
|
245
|
+
declare function findMarksToOpen(activeMarks: Map<string, any>, currentMarks: Map<string, any>): Array<{
|
|
246
|
+
type: string;
|
|
247
|
+
mark: any;
|
|
248
|
+
}>;
|
|
249
|
+
/**
|
|
250
|
+
* Determines which marks need to be closed at the end of the current text node.
|
|
251
|
+
* This handles cases where marks end at node boundaries or when transitioning
|
|
252
|
+
* to nodes with different mark sets.
|
|
253
|
+
*/
|
|
254
|
+
declare function findMarksToCloseAtEnd(activeMarks: Map<string, any>, currentMarks: Map<string, any>, nextNode: any, markSetsEqual: (a: Map<string, any>, b: Map<string, any>) => boolean): string[];
|
|
255
|
+
/**
|
|
256
|
+
* Closes active marks before rendering a non-text node.
|
|
257
|
+
* Returns the closing markdown syntax and clears the active marks.
|
|
258
|
+
*/
|
|
259
|
+
declare function closeMarksBeforeNode(activeMarks: Map<string, any>, getMarkClosing: (markType: string, mark: any) => string): string;
|
|
260
|
+
/**
|
|
261
|
+
* Reopens marks after rendering a non-text node.
|
|
262
|
+
* Returns the opening markdown syntax and updates the active marks.
|
|
263
|
+
*/
|
|
264
|
+
declare function reopenMarksAfterNode(marksToReopen: Map<string, any>, activeMarks: Map<string, any>, getMarkOpening: (markType: string, mark: any) => string): string;
|
|
265
|
+
/**
|
|
266
|
+
* Check if a markdown list item token is a task item and extract its state.
|
|
267
|
+
*
|
|
268
|
+
* @param item The list item token to check
|
|
269
|
+
* @returns Object containing isTask flag, checked state, and indentation level
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* isTaskItem({ raw: '- [ ] Task' }) // { isTask: true, checked: false, indentLevel: 0 }
|
|
274
|
+
* isTaskItem({ raw: ' - [x] Done' }) // { isTask: true, checked: true, indentLevel: 2 }
|
|
275
|
+
* isTaskItem({ raw: '- Regular' }) // { isTask: false, indentLevel: 0 }
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
declare function isTaskItem(item: MarkdownToken): {
|
|
279
|
+
isTask: boolean;
|
|
280
|
+
checked?: boolean;
|
|
281
|
+
indentLevel: number;
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* Assumes the content type based off the content.
|
|
285
|
+
* @param content The content to assume the type for.
|
|
286
|
+
* @param contentType The content type that should be prioritized.
|
|
287
|
+
*/
|
|
288
|
+
declare function assumeContentType(content: (Content | Fragment | Node) | string, contentType: ContentType): ContentType;
|
|
289
|
+
|
|
290
|
+
export { Markdown, type MarkdownExtensionOptions, type MarkdownExtensionStorage, MarkdownManager, assumeContentType, closeMarksBeforeNode, findMarksToClose, findMarksToCloseAtEnd, findMarksToOpen, isTaskItem, reopenMarksAfterNode, wrapInMarkdownBlock };
|