Skip to content

Commit

Permalink
add ref handling and sidebar update
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJuanAndOnly99 committed Feb 13, 2024
1 parent 9eae468 commit c449b9e
Showing 1 changed file with 90 additions and 24 deletions.
114 changes: 90 additions & 24 deletions schema2Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ function processProperty(propertyName, propertyDetails, schemaExamples) {
} else {
markdownContent += `### ${propertyDetails.title || propertyName}\n\n`;
markdownContent += `\`${propertyName}\`\n\n`;
markdownContent += `${propertyDetails.description}\n\n`;

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

if (propertyDetails.type) {
markdownContent += `**Type**: ${propertyDetails.type}\n\n`;
} else {
const contextRef = propertyDetails.properties?.context?.$ref || propertyDetails.$ref;

if (contextRef) {
const reference = contextRef.toLowerCase().replace(/\s+/g, '-');
markdownContent += `**Reference**: [${contextRef}](${reference}.md)\n\n`;
markdownContent += renderRef(contextRef);
}
}

Expand All @@ -28,19 +31,48 @@ function processProperty(propertyName, propertyDetails, schemaExamples) {
}

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

if (schemaExamples) {
const example = schemaExamples[0];
if (example[propertyName]) {

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

function renderRef(contextRef) {
const filePath = contextRef.split('#')[0]; // ../api/api.schema.json
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

const objectPath = contextRef.split('#')[1]; // /definitions/AppIdentifier
let objectName = objectType;
if (objectPath) {
objectName = objectPath.split('/').pop(); // AppIdentifier
}

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

console.log('from contextRef:', contextRef, 'to objectRef:', objectRef);

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

function hasAllOf(allOfArray) {
return Array.isArray(allOfArray) &&
allOfArray.length > 0 &&
Expand All @@ -53,21 +85,18 @@ function hasProperties(schema) {
}

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

// if (schemaFolderName === 'api') {
// console.log('schema:', schema);
// }

const fileName = path.basename(schemaFile);
const objectName = schema.title

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

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

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

console.log('hasAllOf/hasProperties', hasAllOf(schema.allOf), hasProperties(schema));
Expand Down Expand Up @@ -99,22 +128,35 @@ function generateObjectMD(schema, title, schemaFile, schemaFolderName) {
}

if (ref) {
markdownContent += `ref: ${schema.allOf[1].$ref}\n\n`;
markdownContent += `ref: ${ref}\n\n`;
}

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

const frontMatter = generateFrontMatter(fileName.replace('.schema.json', '.md'), schema.description);
const frontMatter = generateFrontMatter(objectName, schema.description);
console.log('frontMatter:', frontMatter);

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

fs.outputFileSync(outputFileName, `---\n${yaml.dump(frontMatter)}\n---\n\n${markdownContent}`);
console.log(`Saved ${outputFileName}`);

// 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;
}

function generateFrontMatter(title, description) {
Expand All @@ -130,16 +172,18 @@ function processSchemaFile(schemaFile, schemaFolderName) {

// if there is allOf, then it is an object
const allOfArray = schemaData.allOf;
let sidebarItems = [];
if (Array.isArray(allOfArray) && allOfArray.length > 0) {
generateObjectMD(schemaData, null, schemaFile, schemaFolderName);
console.log(`Generated ${schemaFile} schema`);
sidebarItems.push(generateObjectMD(schemaData, null, schemaFolderName));
}
if (schemaData.definitions) {
for (const [objectName, objectDetails] of Object.entries(schemaData.definitions)) {
generateObjectMD(objectDetails, objectName, schemaFile, schemaFolderName) + "\n\n";
console.log(`Generated ${schemaFolderName}/${objectName} schema`);
sidebarItems.push(generateObjectMD(objectDetails, objectName, schemaFolderName));
}
}

// return sidebarItems.flat().filter(item => !item.endsWith('undefined'));
return sidebarItems;
}

function parseSchemaFolder(schemaFolderName) {
Expand All @@ -149,15 +193,37 @@ function parseSchemaFolder(schemaFolderName) {
.map(file => path.join("./schemas/"+schemaFolderName, file));

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

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

function main() {
// Folder containing JSON schema files
parseSchemaFolder('api');
parseSchemaFolder('context');
let sidebarObject = require('./website/versioned_sidebars/version-2.1-sidebars.json')

let sidebarContextObject = {
"type": "category",
"label": "Context Schemas Part",
"items": []
}

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

sidebarApiObject.items = parseSchemaFolder('api');
sidebarContextObject.items = parseSchemaFolder('context');

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

fs.outputJSONSync('./website/versioned_sidebars/version-2.1-sidebars.json', sidebarObject, { spaces: 2 });
}

if (require.main === module) {
Expand Down

0 comments on commit c449b9e

Please sign in to comment.