Skip to content

Commit

Permalink
Merge pull request #21 from AleksiKaa/ZakarFin-docs-generator
Browse files Browse the repository at this point in the history
Add linking from requests/events to bundles
  • Loading branch information
testomuli authored Dec 20, 2023
2 parents 882ea07 + 33edadc commit 210d759
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions scripts/generateDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ for (const [sectionTitle, frontendPath] of Object.entries(filesToHandle)) {
* filename: the filename to be generated
* fileHeading: the section number of the file, i.e. 2.3, 2.4 etc.
*/
const generateFlattenedFile = (filter, filename, fileHeading) => {
const generateFlattenedFile = (filter, filename, fileHeading, includeLinks) => {
// Get wanted filenames by reading all files in frontend/api and applying filter to result
const files = fs.readdirSync(
path.join(pathToFrontendRepository, "api/"),
Expand All @@ -97,15 +97,46 @@ const generateFlattenedFile = (filter, filename, fileHeading) => {
),
'utf-8'
);

const headingEnd = fileContent.indexOf('\n');
// Write bundle heading
fs.appendFileSync(
flattenedFileName,
`# ${fileHeading}.${sectionHeadingNumber}${fileContent.slice(1, fileContent.length)}\n`
`# ${fileHeading}.${sectionHeadingNumber}${fileContent.slice(1, headingEnd)}`
);

const searchKey = "## Description"
let descriptionStart = fileContent.indexOf(searchKey);
let descriptionEnd = 0;

// If file does not contain the description subheading, append the whole file except the heading
if (descriptionStart == -1) {
descriptionStart = headingEnd;
descriptionEnd = fileContent.length;
} else {
descriptionEnd = fileContent.indexOf("## ", descriptionStart + 1);
descriptionStart = descriptionStart + searchKey.length;
}

fs.appendFileSync(
flattenedFileName,
`${fileContent.slice(descriptionStart, descriptionEnd)}\n`
);

// Append links to oskari.org documentation for each request/event
if (includeLinks) {
const bundleName = path.dirname(dirent.path).split("api")[1].replaceAll('\\', '/');
fs.appendFileSync(
flattenedFileName,
`\n\nIncluded in bundle: [${bundleName}](https://oskari.org/api/bundles#${version}${bundleName})\n\n`
);
}

sectionHeadingNumber++;
}
);
}

generateFlattenedFile(dirent => dirent.name === "bundle.md", "Bundles", "2.3");
generateFlattenedFile(dirent => dirent.name.toLowerCase().includes("request.md"), "Bundle requests", "2.4");
generateFlattenedFile(dirent => dirent.name.toLowerCase().includes("event.md"), "Bundle events", "2.5");
generateFlattenedFile(dirent => dirent.name === "bundle.md", "Bundles", "2.3", false);
generateFlattenedFile(dirent => dirent.name.toLowerCase().includes("request.md"), "Bundle requests", "2.4", true);
generateFlattenedFile(dirent => dirent.name.toLowerCase().includes("event.md"), "Bundle events", "2.5", true);

0 comments on commit 210d759

Please sign in to comment.