All files / roosterjs-content-model-markdown/lib/markdownToModel/appliers applySegmentFormatting.ts

92% Statements 23/25
83.33% Branches 5/6
100% Functions 1/1
90.91% Lines 20/22

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 40 41 42 43 441x 1x 1x 1x 1x 1x                   1x         216x       216x 247x 247x 247x 9x 9x   238x 12x   238x 238x 238x         216x    
import { adjustHeading } from '../utils/adjustHeading';
import { applyLink } from './applyLink';
import { applyTextFormatting } from './applyTextFormatting';
import { createBr, createText } from 'roosterjs-content-model-dom';
import { createImageSegment } from '../creators/createImageSegment';
import { splitParagraphSegments } from '../utils/splitParagraphSegments';
 
import type {
    ContentModelParagraph,
    ContentModelParagraphDecorator,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function applySegmentFormatting(
    text: string,
    paragraph: ContentModelParagraph,
    decorator?: ContentModelParagraphDecorator
): ContentModelParagraph | undefined {
    Iif (text.length === 0) {
        const br = createBr();
        paragraph.segments.push(br);
    } else {
        const textSegments = splitParagraphSegments(text);
        for (const segment of textSegments) {
            const formattedSegment = createText(segment.text);
            if (segment.type === 'image') {
                const image = createImageSegment(segment.text, segment.url);
                paragraph.segments.push(image);
            } else {
                if (segment.type === 'link') {
                    applyLink(formattedSegment, segment.text, segment.url);
                }
                adjustHeading(formattedSegment, decorator);
                const formattedSegments = applyTextFormatting(formattedSegment);
                paragraph.segments.push(...formattedSegments);
            }
        }
    }
 
    return paragraph;
}