MCPcopy
hub / github.com/markedjs/marked / table

Method table

src/Tokenizer.ts:515–576  ·  view source on GitHub ↗
(src: string)

Source from the content-addressed store, hash-verified

513 }
514
515 table(src: string): Tokens.Table | undefined {
516 const cap = this.rules.block.table.exec(src);
517 if (!cap) {
518 return;
519 }
520
521 if (!this.rules.other.tableDelimiter.test(cap[2])) {
522 // delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading
523 return;
524 }
525
526 const headers = splitCells(cap[1]);
527 const aligns = cap[2].replace(this.rules.other.tableAlignChars, '').split('|');
528 const rows = cap[3]?.trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, '').split('\n') : [];
529
530 const item: Tokens.Table = {
531 type: 'table',
532 raw: rtrim(cap[0], '\n'),
533 header: [],
534 align: [],
535 rows: [],
536 };
537
538 if (headers.length !== aligns.length) {
539 // header and align columns must be equal, rows can be different.
540 return;
541 }
542
543 for (const align of aligns) {
544 if (this.rules.other.tableAlignRight.test(align)) {
545 item.align.push('right');
546 } else if (this.rules.other.tableAlignCenter.test(align)) {
547 item.align.push('center');
548 } else if (this.rules.other.tableAlignLeft.test(align)) {
549 item.align.push('left');
550 } else {
551 item.align.push(null);
552 }
553 }
554
555 for (let i = 0; i < headers.length; i++) {
556 item.header.push({
557 text: headers[i],
558 tokens: this.lexer.inline(headers[i]),
559 header: true,
560 align: item.align[i],
561 });
562 }
563
564 for (const row of rows) {
565 item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
566 return {
567 text: cell,
568 tokens: this.lexer.inline(cell),
569 header: false,
570 align: item.align[i],
571 };
572 }));

Callers

nothing calls this directly

Calls 3

splitCellsFunction · 0.90
rtrimFunction · 0.90
inlineMethod · 0.80

Tested by

no test coverage detected