Skip to content

Commit e642c51

Browse files
authored
feat(core): markdown-parser can parse an existing stream of md tokens (#806)
1 parent ce0c9e0 commit e642c51

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/bundle/config/dynamicModifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function createDynamicModifiers(markupManager: MarkupManager): DynamicMod
1919
process: (token, _, rawMarkup) => {
2020
const {map} = token;
2121

22-
if (map) {
22+
if (map && rawMarkup !== null) {
2323
const content = rawMarkup.split('\n').slice(map[0], map[1]).join('\n').trim();
2424
const tokenId = v5(content, markupManager.getNamespace());
2525

src/core/markdown/MarkdownParser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,22 @@ export class MarkdownParser implements Parser {
7777
return this.tokenizer.linkify.match(text);
7878
}
7979

80-
parse(text: string) {
80+
parse(src: string): Node;
81+
parse(tokens: Token[]): Node;
82+
parse(src: string | Token[]) {
8183
const time = Date.now();
8284

8385
try {
8486
this.stack = [{type: this.schema.topNodeType, content: []}];
8587

8688
let mdItTokens;
8789
try {
88-
mdItTokens = this.tokenizer.parse(text, {});
90+
mdItTokens = typeof src === 'string' ? this.tokenizer.parse(src, {}) : src;
8991
if (this.dynamicModifier) {
90-
mdItTokens = this.dynamicModifier.processTokens(mdItTokens, text);
92+
mdItTokens = this.dynamicModifier.processTokens(
93+
mdItTokens,
94+
typeof src === 'string' ? src : null,
95+
);
9196
}
9297
} catch (err) {
9398
const e = err as Error;
@@ -377,7 +382,7 @@ function withoutTrailingNewline(str: string) {
377382
export type ProcessToken = (
378383
token: Token,
379384
index: number,
380-
rawMarkup: string,
385+
rawMarkup: string | null,
381386
allowedAttrs?: string[],
382387
) => Token;
383388
export type ProcessNodeAttrs = (
@@ -455,7 +460,7 @@ export class MarkdownParserDynamicModifier {
455460
this.elementProcessors = new Map(Object.entries(config));
456461
}
457462

458-
processTokens(tokens: Token[], rawMarkup: string): Token[] {
463+
processTokens(tokens: Token[], rawMarkup: string | null): Token[] {
459464
return tokens.map((token, index) => {
460465
const processor = this.elementProcessors.get(cropNodeName(token.type, openSuffix, ''));
461466
if (!processor || !processor.processToken || processor.processToken.length === 0) {

src/core/types/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {Node} from 'prosemirror-model';
55
export interface Parser {
66
/** Parse raw markup to prosemirror's root node */
77
parse(markup: string): Node;
8+
/** Parse markdown-it tokens stream to prosemirror's root node */
9+
parse(tokens: Token[]): Node;
810
validateLink(url: string): boolean;
911
normalizeLink(url: string): string;
1012
normalizeLinkText(url: string): string;

0 commit comments

Comments
 (0)