All files / roosterjs-content-model-markdown/lib/markdownToModel/creators createTableFromMarkdown.ts

100% Statements 39/39
83.33% Branches 15/18
100% Functions 5/5
100% Lines 32/32

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901x   1x                     1x       45x 11x 11x             25x 25x   11x     11x                 25x 25x 25x   25x 25x     25x                 25x   25x 55x 55x 55x             55x 55x 55x   55x 55x   25x       55x 6x   49x 8x   41x    
import { createParagraphFromMarkdown } from './createParagraphFromMarkdown';
import type { ContentModelTable } from 'roosterjs-content-model-types';
import {
    applyTableFormat,
    createTable,
    createTableCell,
    createTableRow,
} from 'roosterjs-content-model-dom';
import type { MarkdownToModelOptions } from '../types/MarkdownToModelOptions';
 
/**
 * @internal
 */
export function createTableFromMarkdown(
    tableLines: string[],
    options: MarkdownToModelOptions
): ContentModelTable {
    const tableDivider = tableLines[1].split('|').filter(content => content.trim() !== '');
    tableLines.splice(1, 1);
    const table = createTable(
        0,
        options.direction
            ? { borderCollapse: true, direction: options.direction }
            : { borderCollapse: true }
    );
 
    for (const line of tableLines) {
        createTableModel(line, table, tableDivider, options);
    }
    applyTableFormat(table, {
        hasHeaderRow: true,
    });
    return table;
}
 
function createTableModel(
    markdown: string,
    table: ContentModelTable,
    tableDivider: string[],
    options: MarkdownToModelOptions
) {
    const contents = markdown.split('|');
    Eif (contents[0].trim() === '') {
        contents.shift();
    }
    Eif (contents[contents.length - 1].trim() === '') {
        contents.pop();
    }
 
    addTableRow(table, contents, tableDivider, options);
}
 
function addTableRow(
    table: ContentModelTable,
    contents: string[],
    tableDivider: string[],
    options: MarkdownToModelOptions
) {
    const row = createTableRow(options.direction ? { direction: options.direction } : undefined);
 
    let index = 0;
    for (const content of contents) {
        const paragraph = createParagraphFromMarkdown(content, options);
        const cell = createTableCell(
            undefined /* spanLeftOrColSpan */,
            undefined /* spanAboveOrRowSpan */,
            undefined /* isHeader */,
            options.direction ? { direction: options.direction } : undefined
        );
 
        cell.blocks.push(paragraph);
        Eif (tableDivider[index]) {
            cell.format.textAlign = getCellAlignment(tableDivider[index]);
        }
        row.cells.push(cell);
        index++;
    }
    table.rows.push(row);
}
 
function getCellAlignment(content: string) {
    if (content.startsWith(':') && content.endsWith(':')) {
        return 'center';
    }
    if (content.endsWith(':')) {
        return 'end';
    }
    return 'start';
}