All files / roosterjs-content-model-markdown/lib/modelToMarkdown/processor modelProcessor.ts

100% Statements 17/17
100% Branches 9/9
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x         1x               1x   20x   18x 18x       26x 26x 26x 2x 2x 2x     26x             18x    
import { createMarkdownBlock } from '../creators/createMarkdownBlock';
import type { MarkdownLineBreaks } from '../../constants/markdownLineBreaks';
import type { ListCounter } from '../creators/createMarkdownBlockGroup';
import type { ContentModelDocument } from 'roosterjs-content-model-types';
 
const DEFAULT_NEW_LINE: MarkdownLineBreaks = {
    lineBreak: '\n\n',
    newLine: '\n',
};
 
/**
 * @internal
 */
export function modelProcessor(
    model: ContentModelDocument,
    newLine: MarkdownLineBreaks = DEFAULT_NEW_LINE
): string {
    let markdown = '';
    const listCounter: ListCounter = {
        listItemCount: 0,
        subListItemCount: 0,
    };
    for (const block of model.blocks) {
        const isListItem = block.blockType === 'BlockGroup' && block.blockGroupType === 'ListItem';
        if (!isListItem && (listCounter.listItemCount > 0 || listCounter.subListItemCount > 0)) {
            listCounter.listItemCount = 0;
            listCounter.subListItemCount = 0;
            markdown += newLine.newLine;
        }
 
        markdown += createMarkdownBlock(block, newLine, listCounter, {
            table: newLine.newLine,
            paragraph: newLine.lineBreak,
            divider: newLine.lineBreak,
        });
    }
 
    return markdown;
}