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 all 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
50 changes: 48 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint": "eslint src/ test/ --ext .ts --fix",
"prepack": "npm run build",
"typegen": "node s2tQuicktypeUtil.js schemas/context src/context/ContextTypes.ts",
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts"
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts",
"schema-2-markdown": "node schema2Markdown.js"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -65,7 +66,9 @@
"rollup": "4.12.1",
"ts-jest": "29.1.2",
"tslib": "^2.0.1",
"typescript": "^4.0.3"
"typescript": "^4.0.3",
"fs-extra": "^11.2.0",
"js-yaml": "^4.1.0"
},
"optionalDependencies": {
"@rollup/rollup-linux-x64-gnu": "4.14.1"
Expand Down
261 changes: 261 additions & 0 deletions schema2Markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
const fse = require('fs-extra');
const yaml = require('js-yaml');
const path = require('path');

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

if (propertyName === 'type') {
//skip rendering the type property as it should be rendered at the top level
return markdownContent;
}
markdownContent += `### \`${propertyName}\`\n\n`;
if (required) { markdownContent += `**(required)**\n`; }

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

if (propertyDetails.type) {
markdownContent += renderType(propertyDetails.type);
} else {
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
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 (propertyDetails.properties && Object.entries(propertyDetails.properties).length > 0) {
// console.log("Sub props for "+propertyName+" - ",propertyDetails.properties.entries())
markdownContent += '#### Subproperties\n';
for (const [subpropertyName, subpropertyDetails] of Object.entries(propertyDetails.properties)) {
markdownContent += `##### ${subpropertyName}\n`;
markdownContent += `- Type: \`${subpropertyDetails.type}\`\n`;
markdownContent += `- Description: \`${subpropertyDetails.description}\`\n\n`;
};
}

if (schemaExamples) {
schemaExamples.forEach((example) => {
markdownContent += `\n**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;
}

let refLabel = `${objectType}/${objectName}`;
if (objectType == objectName) {
refLabel = objectName;
}
// We need to prepend ../ since Docusaurus forces a trailing slash at the end of each URL
return `**Reference**: [${refLabel}](../${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, filePath, 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.enum) {
markdownContent += renderEnum(schema.enum);
}

TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved
const url = filePath.replace("schemas/", `https://github.com/finos/FDC3/tree/main/schemas/`);
markdownContent += `## Schema\n\n<${url}>\n\n`;

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 typeString = properties?.type?.const;
const ref = root.$ref;

markdownContent += `## Type\n\n`;
markdownContent += `\`${typeString}\`\n\n`;
markdownContent += `## Properties\n\n`;

for (const [propertyName, propertyDetails] of Object.entries(properties)) {
if (propertyName != "type"){
markdownContent += processProperty(propertyName, propertyDetails, schema.examples);
}
}

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

if (schema.examples && schema.examples.length > 0) {
if (schema.examples.length > 1) {
markdownContent += `## Examples\n\n`;
} else {
markdownContent += `## Example\n\n`;
}

schema.examples.forEach((example) => {
markdownContent += '```json\n';
markdownContent += JSON.stringify(example, null, 2);
markdownContent += '\n```\n\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) {
return text.replace(/>/g, '\\>').replace(/@experimental/g, '[@experiemental](/docs/fdc3-compliance#experimental-features)');
}

function generateFrontMatter(title, description) {
return {
title: title,
description: description,
sidebar_label: title,
};
}

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, schemaFile, version));
}
if (schemaData.definitions) {
for (const [objectName, objectDetails] of Object.entries(schemaData.definitions)) {
sidebarItems.push(generateObjectMD(objectDetails, objectName, schemaFolderName, schemaFile, version));
}
}
TheJuanAndOnly99 marked this conversation as resolved.
Show resolved Hide resolved

return sidebarItems;
}

function parseSchemaFolder(schemaFolderName, version) {
// Read all files in the schema folder
const schemaFiles = fse.readdirSync("./schemas/"+schemaFolderName)
.filter(file => file.endsWith('.json'))
// nosemgrep
.map(file => path.join("./schemas/"+schemaFolderName, file));

// 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 Data Part",
"items": []
}

sidebarContextObject.items = parseSchemaFolder('context', version);

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

sidebarObject.docs["FDC3 Standard"].push(sidebarContextObject)

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

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