MCPcopy
hub / github.com/codeaashu/claude-code / MarkdownTable

Function MarkdownTable

src/components/MarkdownTable.tsx:72–321  ·  view source on GitHub ↗
({
  token,
  highlight,
  forceWidth
}: Props)

Source from the content-addressed store, hash-verified

70 * 4. Properly aligning multi-line rows with borders
71 */
72export function MarkdownTable({
73 token,
74 highlight,
75 forceWidth
76}: Props): React.ReactNode {
77 const [theme] = useTheme();
78 const {
79 columns: actualTerminalWidth
80 } = useTerminalSize();
81 const terminalWidth = forceWidth ?? actualTerminalWidth;
82
83 // Format cell content to ANSI string
84 function formatCell(tokens: Token[] | undefined): string {
85 return tokens?.map(_ => formatToken(_, theme, 0, null, null, highlight)).join('') ?? '';
86 }
87
88 // Get plain text (stripped of ANSI codes)
89 function getPlainText(tokens_0: Token[] | undefined): string {
90 return stripAnsi(formatCell(tokens_0));
91 }
92
93 // Get the longest word width in a cell (minimum width to avoid breaking words)
94 function getMinWidth(tokens_1: Token[] | undefined): number {
95 const text = getPlainText(tokens_1);
96 const words = text.split(/\s+/).filter(w => w.length > 0);
97 if (words.length === 0) return MIN_COLUMN_WIDTH;
98 return Math.max(...words.map(w_0 => stringWidth(w_0)), MIN_COLUMN_WIDTH);
99 }
100
101 // Get ideal width (full content without wrapping)
102 function getIdealWidth(tokens_2: Token[] | undefined): number {
103 return Math.max(stringWidth(getPlainText(tokens_2)), MIN_COLUMN_WIDTH);
104 }
105
106 // Calculate column widths
107 // Step 1: Get minimum (longest word) and ideal (full content) widths
108 const minWidths = token.header.map((header, colIndex) => {
109 let maxMinWidth = getMinWidth(header.tokens);
110 for (const row of token.rows) {
111 maxMinWidth = Math.max(maxMinWidth, getMinWidth(row[colIndex]?.tokens));
112 }
113 return maxMinWidth;
114 });
115 const idealWidths = token.header.map((header_0, colIndex_0) => {
116 let maxIdeal = getIdealWidth(header_0.tokens);
117 for (const row_0 of token.rows) {
118 maxIdeal = Math.max(maxIdeal, getIdealWidth(row_0[colIndex_0]?.tokens));
119 }
120 return maxIdeal;
121 });
122
123 // Step 2: Calculate available space
124 // Border overhead: │ content │ content │ = 1 + (width + 3) per column
125 const numCols = token.header.length;
126 const borderOverhead = 1 + numCols * 3; // │ + (2 padding + 1 border) per col
127 // Account for SAFETY_MARGIN to avoid triggering the fallback safety check
128 const availableWidth = Math.max(terminalWidth - borderOverhead - SAFETY_MARGIN, numCols * MIN_COLUMN_WIDTH);
129

Callers

nothing calls this directly

Calls 12

useTerminalSizeFunction · 0.85
getMinWidthFunction · 0.85
getIdealWidthFunction · 0.85
calculateMaxRowLinesFunction · 0.85
renderVerticalFormatFunction · 0.85
renderBorderLineFunction · 0.85
renderRowLinesFunction · 0.85
stripAnsiFunction · 0.85
maxMethod · 0.80
forEachMethod · 0.80
useThemeFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected