Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: generate docs #5723

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 122 additions & 119 deletions website/_scripts/extract-properties.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// @ts-check
import { promises as fs } from 'node:fs';

const schemaFile = new URL('../cspell.schema.json', import.meta.url);
const schemaFile = new URL('../../cspell.schema.json', import.meta.url);
const schemaFileOut = new URL('../docs/Configuration/auto_properties.md', import.meta.url);

async function run() {
const schema = await loadSchema();
const schema = await loadSchema();

const header = `\
const header = `\
---
# AUTO-GENERATED ALL CHANGES WILL BE LOST
# See \`_scripts/extract-properties.js\`
Expand All @@ -20,148 +21,150 @@ async function run() {

`;

const sections = [removeLeftPad(header), '', schemaEntry(schema, 'Settings'), '', formatDefinitions(schema)];
const sections = [removeLeftPad(header), '', schemaEntry(schema, 'Settings'), '', formatDefinitions(schema)];

const doc = sections.join('\n').replace(/\u200B/g, ''); // remove zero width spaces
const doc = sections.join('\n').replace(/\u200B/g, ''); // remove zero width spaces

console.log(doc);
await fs.writeFile(schemaFileOut, doc, 'utf8');
}

function schemaEntry(entry, name) {
if (entry.type === 'object') {
return schemaObjectEntry(entry, name);
}
return `## ${name}\n**Not Handled:** ${entry.type}\n`;
if (entry.type === 'object') {
return schemaObjectEntry(entry, name);
}
return `## ${name}\n**Not Handled:** ${entry.type}\n`;
}

function schemaObjectEntry(schemaTypeObject, nameOfType) {
const properties = schemaTypeObject.properties || {};
// console.error('Object Type %s\n%o', 'Properties:', properties);
/** @type {string[]} */
const lines = [];
const properties = schemaTypeObject.properties || {};
// console.error('Object Type %s\n%o', 'Properties:', properties);
/** @type {string[]} */
const lines = [];

if (nameOfType) {
lines.push(`## ${nameOfType}\n`);
}
if (nameOfType) {
lines.push(`## ${nameOfType}\n`);
}

// Object Fields as a table
lines.push('| Field | Type | Description |', '| --- | --- | --- |');
for (const [key, entry] of Object.entries(properties)) {
lines.push(formatPropertyForOverview(key, entry, 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));
}

// Add Object Fields Expanded.
// Add Object Fields Expanded.

lines.push(`\n\n### ${nameOfType} Fields`);
lines.push(`\n\n### ${nameOfType} Fields`);

for (const [key, entry] of Object.entries(properties)) {
lines.push(formatPropertyToDisplay(key, entry, nameOfType));
}
for (const [key, entry] of Object.entries(properties)) {
lines.push(formatPropertyToDisplay(key, entry, nameOfType));
}

return lines.join('\n');
return lines.join('\n');
}

/**
* @param {string} name - name of heading
* @param {string} section - the containing entry name
*/
function linkToHeader(name, section) {
const id = toId(section, name);
return `[${name}](#${id})`;
const id = toId(section, name);
return `[${name}](#${id})`;
}


function toId(nameOfParentType, header) {
return `${nameOfParentType}-${header}`.toLowerCase().replaceAll(/\W/g, '-');
return `${nameOfParentType}-${header}`.toLowerCase().replaceAll(/\W/g, '-');
}

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) {
return removeLeftPad(`
#### \`${key}\` {#${toId(nameOfParentType, key)}}

<dl>
<dt>Name</dt>
<dd>
\`${key}\`
</dd>
</dl>

<dl>
<dt>Type</dt>
<dd>
\`boolean\`
</dd>
</dl>

<dl>
<dt>Description</dt>
<dd>
${formatEntryDescription(entry, ' ')}
</dd>
</dl>
`);
return removeLeftPad(`

---

#### \`${key}\` {#${toId(nameOfParentType, key)}}

<dl>
<dt>Name</dt>
<dd>
\`${key}\`
</dd>
</dl>

<dl>
<dt>Type</dt>
<dd>
${formatEntryType(entry)}
</dd>
</dl>

<dl>
<dt>Description</dt>
<dd>
${formatEntryDescription(entry, ' ')}
</dd>
</dl>
`);
}

/**
* @param {object} entry
*/
function formatEntryType(entry, addFix = '`') {
function fix(value) {
return addFix + value + addFix;
}

if (entry.type === 'array' && entry.items) {
return fix(`${formatEntryType(entry.items, '')}[]`);
}
if (entry.type) {
return fix(entry.type);
}
if (entry.$ref) {
return fix(entry.$ref.split('/').slice(-1).join(''));
}
if (entry.anyOf) {
return entry.anyOf.map((entry) => formatEntryType(entry)).join('<br />');
}
return fix('Unknown');
function fix(value) {
return addFix + value + addFix;
}

if (entry.type === 'array' && entry.items) {
return fix(`${formatEntryType(entry.items, '')}[]`);
}
if (entry.type) {
return fix(entry.type);
}
if (entry.$ref) {
return fix(entry.$ref.split('/').slice(-1).join(''));
}
if (entry.anyOf) {
return entry.anyOf.map((entry) => formatEntryType(entry)).join('<br />');
}
return fix('Unknown');
}

function formatDefinitions(schema) {
return Object.entries(schema.definitions || {})
.map(([key, entry]) => schemaEntry(entry, key))
.join('\n\n');
return Object.entries(schema.definitions || {})
.map(([key, entry]) => schemaEntry(entry, key))
.join('\n\n');
}

function formatEntryDescription(entry, padding) {
let description = entry.markdownDescription || entry.description || '';
if (typeof description !== 'string') {
console.error('%o', description);
return '**`Strange Object`**';
}

description = replaceLinks(description);
description = padLines(description, padding);
return description;
let description = entry.markdownDescription || entry.description || '';
if (typeof description !== 'string') {
console.error('%o', description);
return '**`Strange Object`**';
}

description = replaceLinks(description);
description = padLines(description, padding);
return description;
}

function formatEntryDescriptionShort(entry) {
const description = entry.markdownDescription || entry.description || '';
if (typeof description !== 'string') {
console.error('%o', description);
return '**`Strange Object`**';
}
let short = description.split('\n').slice(0, 1).join('');
short = replaceLinks(short);
return short;
const description = entry.markdownDescription || entry.description || '';
if (typeof description !== 'string') {
console.error('%o', description);
return '**`Strange Object`**';
}
let short = description.split('\n').slice(0, 1).join('');
short = replaceLinks(short);
return short;
}

async function loadSchema() {
const schema = JSON.parse(await fs.readFile(schemaFile, 'utf8'));
const schema = JSON.parse(await fs.readFile(schemaFile, 'utf8'));

return schema;
return schema;
}

const regExpMatchLink = /\{@link (.*?)\}/g;
Expand All @@ -172,12 +175,12 @@ const regExpMatchLink = /\{@link (.*?)\}/g;
* @return {string}
*/
function replaceLinks(markdown) {
markdown = markdown.replaceAll(regExpMatchLink, (_match, p1) => {
p1 = p1.trim();
const link = p1 && `[${p1}](#${p1.toLowerCase().replaceAll(/\W/g, '-')})` || '';
return link;
})
return markdown;
markdown = markdown.replaceAll(regExpMatchLink, (_match, p1) => {
p1 = p1.trim();
const link = (p1 && `[${p1}](#${p1.toLowerCase().replaceAll(/\W/g, '-')})`) || '';
return link;
});
return markdown;
}

/**
Expand All @@ -186,26 +189,26 @@ function replaceLinks(markdown) {
* @param {string} [firstLinePadding] - optional padding of first line.
*/
function padLines(str, padding, firstLinePadding = '') {
let pad = firstLinePadding;
const lines = [];
for (const line of str.split('\n')) {
lines.push(pad + line);
pad = padding;
}

return lines.join('\n');
let pad = firstLinePadding;
const lines = [];
for (const line of str.split('\n')) {
lines.push(pad + line);
pad = padding;
}

return lines.join('\n');
}

function removeLeftPad(str) {
const lines = str.split('\n');
let curPad = str.length;
for (const line of lines) {
if (!line.trim()) continue;
const pad = line.length - line.trimStart().length;
curPad = Math.min(curPad, pad);
}

return lines.map((line) => line.slice(curPad)).join('\n');
const lines = str.split('\n');
let curPad = str.length;
for (const line of lines) {
if (!line.trim()) continue;
const pad = line.length - line.trimStart().length;
curPad = Math.min(curPad, pad);
}

return lines.map((line) => line.slice(curPad)).join('\n');
}

run();
3 changes: 2 additions & 1 deletion website/cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ dictionaryDefinitions:
ignorePaths:
- "*.svg"
- /build
- cspell.json
- cspell.*
- auto_*.md
languageSettings:
- languageId: typescript,typescriptreact,json,css,yaml
words:
Expand Down
Loading
Loading