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

1068 Generating context docs from schema files #1151

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
69aeded
add schema-2-markdown script
TheJuanAndOnly99 Feb 7, 2024
8b8960c
change file names
TheJuanAndOnly99 Feb 8, 2024
9eae468
refactor
TheJuanAndOnly99 Feb 8, 2024
c449b9e
add ref handling and sidebar update
TheJuanAndOnly99 Feb 13, 2024
3ebd43a
Merge pull request #1 from TheJuanAndOnly99/issue-1068
TheJuanAndOnly99 Feb 13, 2024
c9c2b1d
run schema2markdown across all FDC3 versions
TheJuanAndOnly99 Feb 13, 2024
6dc4331
rename schema2markdown in package.json
TheJuanAndOnly99 Feb 13, 2024
814fd93
add type and enum to object definitions
TheJuanAndOnly99 Feb 13, 2024
1a85911
small formatting issues
TheJuanAndOnly99 Feb 13, 2024
d5464a7
fixed issue on enum rendering
TheJuanAndOnly99 Feb 13, 2024
0e0c72b
Update schema2Markdown.js
TheJuanAndOnly99 Mar 7, 2024
18cee47
Update schema2Markdown.js
TheJuanAndOnly99 Mar 7, 2024
b052b86
remove console logs, commented code, change fs to fse
TheJuanAndOnly99 Mar 7, 2024
6060696
Merge branch 'issue-1068' of github.com:TheJuanAndOnly99/FDC3 into is…
TheJuanAndOnly99 Mar 7, 2024
6bf17da
remove use of require, remove hardcoded versions, check for multiple …
TheJuanAndOnly99 Mar 7, 2024
3d38f83
add a link to the json.schema file, fix parsing of versions
TheJuanAndOnly99 Mar 7, 2024
901d72a
Update schema2Markdown.js
TheJuanAndOnly99 Mar 7, 2024
44e0c66
remove else
TheJuanAndOnly99 Mar 7, 2024
5423041
Merge branch 'issue-1068' of github.com:TheJuanAndOnly99/FDC3 into is…
TheJuanAndOnly99 Mar 7, 2024
11ad90f
move generated from text outside of code example block
TheJuanAndOnly99 Mar 7, 2024
0c83f6a
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
f4a80aa
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
fe2e40e
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
816c3a8
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
a2b12f7
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
86d994a
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
9cb323a
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
0b13c7a
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
2f8bd00
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
1950f73
remove api schemas and render type at top level and skip under proper…
TheJuanAndOnly99 Apr 11, 2024
a0216b6
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
4c49bb5
Update schema2Markdown.js
TheJuanAndOnly99 Apr 11, 2024
b27c3f2
remove extra back-tick
TheJuanAndOnly99 Apr 11, 2024
490af28
add parsing of context schemas
TheJuanAndOnly99 Apr 11, 2024
f74e8b0
rendering out sub properties
maoo Apr 11, 2024
a956cec
fixed indentation on schema2markdown
maoo Apr 11, 2024
5b91507
fix reference labels in schema2Markdown
maoo Apr 11, 2024
1baf110
Merge pull request #2 from finos/main
TheJuanAndOnly99 Apr 11, 2024
3a76695
indentation
maoo Apr 11, 2024
b8ab9c5
Merge branch 'issue-1068' of github.com:TheJuanAndOnly99/FDC3 into is…
maoo Apr 11, 2024
297539c
Merge branch 'main' of github.com:finos/FDC3 into issue-1068
maoo Apr 12, 2024
f90bdce
chore: indentation issue
maoo Apr 12, 2024
54477ae
add a new line before 'Example Value:'
TheJuanAndOnly99 Apr 25, 2024
c51fe3f
sync with upstream
TheJuanAndOnly99 May 2, 2024
eac8599
sync with upstream
TheJuanAndOnly99 May 2, 2024
47df2e3
sync with upstream
TheJuanAndOnly99 May 2, 2024
fee515f
ignore path.join warning
TheJuanAndOnly99 May 2, 2024
ce8787a
sync with upstream
TheJuanAndOnly99 May 2, 2024
eed9602
fix build issues
TheJuanAndOnly99 May 2, 2024
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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"preprepare": "npm run typegen && npm run typegen-bridging",
"prepare": "tsdx build",
"typegen": "node s2tQuicktypeUtil.js schemas/context src/context/ContextTypes.ts && tsdx lint src/context/ --fix",
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts && tsdx lint src/bridging/ --fix"
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts && tsdx lint src/bridging/ --fix",
"schema2markdown": "node schema2Markdown.js"
},
"peerDependencies": {},
"husky": {
Expand All @@ -51,6 +52,8 @@
"quicktype": "23.0.78",
"tsdx": "^0.14.1",
"tslib": "^2.0.1",
"typescript": "^4.0.3"
"typescript": "^4.0.3",
"fs-extra": "^11.2.0",
"js-yaml": "^4.1.0"
}
}
254 changes: 254 additions & 0 deletions schema2Markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
const fse = require('fs-extra');
const yaml = require('js-yaml');
const path = require('path');

function processProperty(propertyName, propertyDetails, schemaExamples) {
let markdownContent = '';

if (propertyName === 'type') {
markdownContent += `### Type\n\n`;
markdownContent += `\`${propertyDetails.const}\`\n\n`;
} else {
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
markdownContent += `### ${propertyDetails.title || propertyName}\n\n`;
markdownContent += `\`${propertyName}\`\n\n`;

if (propertyDetails.description != null) {
markdownContent += `${escape(propertyDetails.description)}\n\n`;
}

if (propertyDetails.type) {
markdownContent += renderType(propertyDetails.type);
} else {
const contextRef = propertyDetails.properties?.context?.$ref || propertyDetails.$ref;

if (contextRef) {
markdownContent += renderRef(contextRef);
}
}

if (propertyDetails.enum) {
markdownContent += renderEnum(propertyDetails.enum);
}

if (propertyDetails.allOf) {
markdownContent += `${propertyDetails.allOf.map((item) => renderRef(item.$ref)).join(', ')}\n\n`;
}

if (schemaExamples) {
schemaExamples.forEach((example) => {
markdownContent += `**Example Value**: \n`;
if (typeof example[propertyName] === 'object') {
markdownContent += `\`\`\`json\n${JSON.stringify(example[propertyName], null, 2)}\n\`\`\`\n\n`;
} else if (example[propertyName]) {
markdownContent += `\`${example[propertyName]}\`\n\n`;
}
});
}
}
return markdownContent;
}

function renderType(ref) {
return `**Type**: \`${ref}\`\n\n`;
}

function renderEnum(ref) {
// for each item in ref, wrap it in backticks and join with a comma
return `**Possible values**: ${ref.map((item) => `\`${item}\``).join(', ')}\n\n`;
}

function renderRef(contextRef) {
const [filePath, objectPath] = contextRef.split('#'); // ../api/api.schema.json, /definitions/AppIdentifier
const objectType = filePath.split('/').pop().split('.')[0]; // api

// FROM ../api/api.schema.json#/definitions/AppIdentifier
// TO ../api/schemas/Appidentifier

// FROM timerange.schema.json#
// TO timerange/schemas/timerange
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

let objectName = objectType;
if (objectPath) {
objectName = objectPath.split('/').pop(); // AppIdentifier
}

let objectRef = objectName;
if (filePath.startsWith('../')) {
objectRef = "../../" + objectType + "/schemas/" + objectName;
}

return `**Reference**: [${objectType}/${objectName}](${objectRef})\n\n`;
}

function hasAllOf(allOfArray) {
return Array.isArray(allOfArray) &&
allOfArray.length > 0 &&
allOfArray[0] != null &&
allOfArray[0].properties != null
}

function hasProperties(schema) {
return schema.properties != null;
}

// Function to generate Markdown content from JSON schema
function generateObjectMD(schema, title, schemaFolderName, version) {

const objectName = schema.title

if (schema.title != null) {
title = schema.title;
}

let markdownContent = `# ${title}\n\n`;

if (schema.description != null) {
markdownContent += `${escape(schema.description)}\n\n`;
}

if (schema.type) {
markdownContent += renderType(schema.type);
}
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
if (schema.enum) {
markdownContent += renderEnum(schema.enum);
}

TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
if (hasAllOf(schema.allOf) || hasProperties(schema)) {
// Extract properties, required fields, and $ref from the first allOf object
let root = schema;
if (hasAllOf(schema.allOf)) {
root = schema.allOf[0];
}

const properties = root.properties;
const required = root.required;
const ref = root.$ref;

markdownContent += `## Properties\n\n`;

for (const [propertyName, propertyDetails] of Object.entries(properties)) {
markdownContent += processProperty(propertyName, propertyDetails, schema.examples);
}
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

// show required properties
if (required && required.length > 0) {
markdownContent += `### Required Properties:\n\n`;
// for each required property show the name
required.forEach((propertyName) => {
markdownContent += `* \`${propertyName}\`\n`;
});
markdownContent += '\n';
}
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

if (ref) {
markdownContent += `ref: ${ref}\n\n`;
}

if (schema.examples) {
markdownContent += `## Examples\n\n`;
markdownContent += '```json\n';
markdownContent += JSON.stringify(schema.examples, null, 2);
markdownContent += '\n```';
}
}

const frontMatter = generateFrontMatter(objectName, schema.description);

const outputFileName = `./website/versioned_docs/version-${version}/${schemaFolderName}/schemas/${title.replace(/\s+/g, '')}.md`;

fse.outputFileSync(outputFileName, `---\n${yaml.dump(frontMatter)}\n---\n\n${markdownContent}`);

// objectName must not contain any spaces
if (objectName != null) {
return schemaFolderName + '/schemas/' + objectName.replace(/\s+/g, '');
}
}

function escape(text) {
let output = text;
output = output.replace(/>/g, '\\>');

return output;
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
}

function generateFrontMatter(title, description) {
return {
title: `${title} Schema`,
description: description,
sidebar_label: title + ' Schema',
};
}
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

function processSchemaFile(schemaFile, schemaFolderName, version) {
const schemaData = fse.readJSONSync(schemaFile);

// if there is allOf, then it is an object
const allOfArray = schemaData.allOf;
let sidebarItems = [];
if (Array.isArray(allOfArray) && allOfArray.length > 0) {
sidebarItems.push(generateObjectMD(schemaData, null, schemaFolderName, version));
}
if (schemaData.definitions) {
for (const [objectName, objectDetails] of Object.entries(schemaData.definitions)) {
sidebarItems.push(generateObjectMD(objectDetails, objectName, schemaFolderName, version));
}
}

return sidebarItems;
}

function parseSchemaFolder(schemaFolderName, version) {
// Read all files in the schema folder
const schemaFiles = fse.readdirSync("./schemas/"+schemaFolderName)
.filter(file => file.endsWith('.json'))
.map(file => path.join("./schemas/"+schemaFolderName, file));
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

// Process each schema file
let sidebarItems = [];
for (const schemaFile of schemaFiles) {
sidebarItems.push(processSchemaFile(schemaFile, schemaFolderName, version));
}

// filter out null values
return sidebarItems.flat().filter(item => item);
}

function main() {

const versions = fse.readdirSync('./website/versioned_docs').map(version => version.split('-')[1]);

versions.forEach(version => {

let sidebarObject = fse.readJsonSync(`./website/versioned_sidebars/version-${version}-sidebars.json`)

let sidebarContextObject = {
"type": "category",
"label": "Context Schemas Part",
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
"items": []
}

let sidebarApiObject = {
"type": "category",
"label": "API Schemas Part",
"items": []
}

sidebarApiObject.items = parseSchemaFolder('api', version);
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
sidebarContextObject.items = parseSchemaFolder('context', version);

if (sidebarObject.docs["FDC3 Standard"] == null) {
sidebarObject.docs["FDC3 Standard"] = [];
}

sidebarObject.docs["FDC3 Standard"].push(sidebarContextObject)
sidebarObject.docs["FDC3 Standard"].push(sidebarApiObject)
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

fse.outputJSONSync(
`./website/versioned_sidebars/version-${version}-sidebars.json`,
sidebarObject, { spaces: 2 });
});
}

if (require.main === module) {
main();
}
Loading