Skip to content

Commit e985d0a

Browse files
committed
fix: render integration doc tables as html
1 parent 07d459d commit e985d0a

2 files changed

Lines changed: 92 additions & 12 deletions

File tree

‎integration-docs/scripts/build.mjs‎

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,74 @@ function normalizeMarkdownIndentation(content) {
168168
return result.join('\n');
169169
}
170170

171-
function normalizeMarkdownTables(content) {
171+
function splitTableRow(line) {
172+
return line.trim().replace(/^\|/, '').replace(/\|$/, '').split('|').map((cell) => cell.trim());
173+
}
174+
175+
function isTableSeparator(line) {
176+
const cells = splitTableRow(line);
177+
return cells.length >= 2 && cells.every((cell) => /^:?-{3,}:?$/.test(cell));
178+
}
179+
180+
function isTableRow(line) {
181+
const trimmed = line.trim();
182+
return trimmed.includes('|') && !trimmed.startsWith('```');
183+
}
184+
185+
function escapeHtml(value) {
186+
return value
187+
.replace(/&/g, '&')
188+
.replace(/</g, '&lt;')
189+
.replace(/>/g, '&gt;')
190+
.replace(/"/g, '&quot;');
191+
}
192+
193+
function renderTableCell(value) {
194+
return escapeHtml(value)
195+
.replace(/`([^`]+)`/g, '<code>$1</code>')
196+
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
197+
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
198+
}
199+
200+
function markdownTableToHtml(rows) {
201+
const header = splitTableRow(rows[0]);
202+
const body = rows.slice(2).map(splitTableRow);
203+
const headHtml = header.map((cell) => `<th>${renderTableCell(cell)}</th>`).join('');
204+
const bodyHtml = body
205+
.map((row) => `<tr>${row.map((cell) => `<td>${renderTableCell(cell)}</td>`).join('')}</tr>`)
206+
.join('\n');
207+
return `<table>\n<thead>\n<tr>${headHtml}</tr>\n</thead>\n<tbody>\n${bodyHtml}\n</tbody>\n</table>`;
208+
}
209+
210+
function convertMarkdownTables(content) {
172211
const lines = content.split('\n');
173-
return lines.map((line, index) => {
174-
const trimmed = line.trim();
175-
const prev = lines[index - 1]?.trim() || '';
176-
const next = lines[index + 1]?.trim() || '';
177-
if (!trimmed.includes('|') || !prev.includes('|') || !next.includes('|')) return line;
178-
179-
const cells = trimmed.replace(/^\|/, '').replace(/\|$/, '').split('|');
180-
if (cells.length < 2 || !cells.every((cell) => /^\s*:?-{3,}:?\s*$/.test(cell))) return line;
181-
return `| ${cells.map(() => '---').join(' | ')} |`;
182-
}).join('\n');
212+
const result = [];
213+
let inFence = false;
214+
215+
for (let index = 0; index < lines.length; index += 1) {
216+
const line = lines[index];
217+
if (line.trimStart().startsWith('```')) {
218+
inFence = !inFence;
219+
result.push(line);
220+
continue;
221+
}
222+
223+
if (!inFence && isTableRow(line) && isTableSeparator(lines[index + 1] || '')) {
224+
const tableRows = [line, lines[index + 1]];
225+
index += 2;
226+
while (index < lines.length && isTableRow(lines[index]) && lines[index].trim()) {
227+
tableRows.push(lines[index]);
228+
index += 1;
229+
}
230+
index -= 1;
231+
result.push(markdownTableToHtml(tableRows));
232+
continue;
233+
}
234+
235+
result.push(line);
236+
}
237+
238+
return result.join('\n');
183239
}
184240

185241
function shouldPrefixDescription(output, description) {
@@ -220,7 +276,7 @@ function mdxToMarkdown(content) {
220276
.replace(/<\/(Note|Tip|Warning|Info|Check|Warning)>/g, '\n')
221277
.replace(/<\/?(Steps|Tabs|AccordionGroup|CardGroup|CodeGroup|Frame)\b[^>]*>/g, '\n');
222278

223-
output = normalizeMarkdownTables(normalizeMarkdownIndentation(output))
279+
output = convertMarkdownTables(normalizeMarkdownIndentation(output))
224280
.replace(/<img\b([^>]*)>/g, (_tag, attrs) => {
225281
const alt = getAttr(attrs, 'alt') || 'image';
226282
const src = getAttr(attrs, 'src');

‎integration-docs/scripts/check.mjs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ function findIndentedMarkdown(value) {
7272
return offenders;
7373
}
7474

75+
function findMarkdownTables(value) {
76+
const lines = value.split('\n');
77+
const offenders = [];
78+
let inFence = false;
79+
80+
lines.forEach((line, index) => {
81+
if (line.trimStart().startsWith('```')) {
82+
inFence = !inFence;
83+
return;
84+
}
85+
const next = lines[index + 1]?.trim() || '';
86+
if (!inFence && line.trim().includes('|') && /^(\|?\s*:?-{3,}:?\s*){2,}\|?$/.test(next)) {
87+
offenders.push(index + 1);
88+
}
89+
});
90+
91+
return offenders;
92+
}
93+
7594
function checkLocale(locale, docs) {
7695
const errors = [];
7796
const keys = Object.keys(docs);
@@ -98,6 +117,11 @@ function checkLocale(locale, docs) {
98117
if (indentedLines.length > 0) {
99118
errors.push(`${locale}: ${key} has indented Markdown outside code fences at lines ${indentedLines.slice(0, 5).join(', ')}`);
100119
}
120+
121+
const tableLines = findMarkdownTables(value);
122+
if (tableLines.length > 0) {
123+
errors.push(`${locale}: ${key} still has Markdown tables at lines ${tableLines.slice(0, 5).join(', ')}`);
124+
}
101125
}
102126
return errors;
103127
}

0 commit comments

Comments
 (0)