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

100% Statements 26/26
100% Branches 8/8
100% Functions 1/1
100% Lines 23/23

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 44 45 461x 1x 1x 1x 1x 1x                   1x         192x 19x 19x   173x 197x 197x 197x 8x 8x   189x 10x   189x 189x 188x 188x           192x    
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 {
    if (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);
                }
                const segmentWithAdjustedHeading = adjustHeading(formattedSegment, decorator);
                if (segmentWithAdjustedHeading) {
                    const formattedSegments = applyTextFormatting(formattedSegment);
                    paragraph.segments.push(...formattedSegments);
                }
            }
        }
    }
 
    return paragraph;
}