Skip to content

Commit

Permalink
docs: format tables in generated docs (#5776)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jun 20, 2024
1 parent 70faccf commit 02e0359
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 257 deletions.
38 changes: 33 additions & 5 deletions website/_scripts/extract-properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ function schemaObjectEntry(schemaTypeObject, nameOfType) {
}

// Object Fields as a table
lines.push('| Field | Type | Description |', '| --- | --- | --- |');
for (const [key, entry] of Object.entries(properties)) {
lines.push(formatPropertyForOverview(key, entry, nameOfType));
}
lines.push(
createTable(
['Field', 'Type', 'Description'],
Object.entries(properties).map(([key, entry]) => formatPropertyForOverview(key, entry, nameOfType)),
),
);

// Add Object Fields Expanded.

Expand Down Expand Up @@ -82,7 +84,7 @@ function toId(nameOfParentType, header) {
}

function formatPropertyForOverview(key, entry, section) {
return `| ${linkToHeader(key, section)} | ${formatEntryType(entry)} | ${formatEntryDescriptionShort(entry)} | `;
return [linkToHeader(key, section), formatEntryType(entry), formatEntryDescriptionShort(entry)];
}

function formatPropertyToDisplay(key, entry, nameOfParentType) {
Expand Down Expand Up @@ -255,6 +257,32 @@ function inject(template, ...values) {
return unindent(String.raw({ raw: strings }, ...adjValues));
}

/**
*
* @param {string[]} headers
* @param {string[][]} rows
* @returns
*/
function createTable(headers, rows) {
const colWidths = [];

for (const row of [headers, ...rows]) {
row.forEach((col, i) => {
colWidths[i] = Math.max(colWidths[i] || 0, [...col].length);
});
}

const rowPlaceholders = colWidths.map(() => '');
const sep = headers.map((_, i) => '---'.padEnd(colWidths[i], '-'));
const table = [headers, sep, ...rows];

return table
.map((row) => [...row, ...rowPlaceholders.slice(row.length)])
.map((row) => row.map((col, i) => col.padEnd(colWidths[i])))
.map((row) => `| ${row.join(' | ')} |`)
.join('\n');
}

/**
* Calculate the padding at the start of the string.
* @param {string} s
Expand Down
1 change: 1 addition & 0 deletions website/_scripts/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": ["ES2022"]
},
Expand Down
Loading

0 comments on commit 02e0359

Please sign in to comment.