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: added list of most useful options, their meaning and default values #131

Merged
merged 18 commits into from
Oct 27, 2023
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
158 changes: 85 additions & 73 deletions generateTypedoc.mjs
Original file line number Diff line number Diff line change
@@ -1,175 +1,187 @@
#!/usr/bin/env node
import * as fs from "fs";
import * as fs from 'fs'

import * as path from "path";
import { createRequire } from "module"; // built-in module
const require = createRequire(import.meta.url); // construct the require function for this ES module
import * as path from 'path'
import { createRequire } from 'module' // built-in module
const require = createRequire(import.meta.url) // construct the require function for this ES module

const docsPath = path.resolve("./src/pages/docs/golem-js/reference");
const docsPath = path.resolve('./src/pages/docs/golem-js/reference')

const branchPrefix = process.argv[2];
const branchPrefix = process.argv[2]

import { dirname } from 'path' // Importing dirname

import { fileURLToPath } from 'url' // Importing fileURLToPath
// Retrieve the current file's absolute path
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

if (!branchPrefix) {
console.error("Please provide branch name as argument.");
process.exit(1);
console.error('Please provide branch name as argument.')
process.exit(1)
}

async function main() {
console.log(`Switching to branch ${branchPrefix} ...`);
console.log(`Switching to branch ${branchPrefix} ...`)

try {
console.log("Generating typedoc ...");
await generateTypedoc(branchPrefix);

const navigation = await generateNavigation([branchPrefix]);
await fs.promises.writeFile(
"./src/navigation/jsreference.js",
"export const navigation = " + navigation
);
} catch (error) {
console.error(`Error with branch ${branchPrefix}:`, error.message);
}
console.log('Generating typedoc ...')
await generateTypedoc(branchPrefix)

const navigation = await generateNavigation([branchPrefix])
await fs.promises.writeFile(
'./src/navigation/jsreference.js',
'export const navigation = ' + navigation
)

// Move default-values.md to the correct location, so we don't overwrite it with reference generation
const sourcePath = `${__dirname}/ `
const destPath = `${__dirname}/src/pages/docs/golem-js/reference/default-values.md`
await fs.promises.copyFile(sourcePath, destPath)
}

const util = require("util");
const glob = util.promisify(require("glob"));
const util = require('util')
const glob = util.promisify(require('glob'))

async function generateTypedoc(branchPrefix) {
const outputPath = "./src/pages/docs/golem-js/reference";
const outputPath = './src/pages/docs/golem-js/reference'

// Once typedoc is done, start looking for .md files and remove ".md" mentions.
const files = await glob(outputPath + "/**/*.md");
const files = await glob(outputPath + '/**/*.md')

console.log("Starting to remove '.md' mentions from files.");
console.log("Starting to remove '.md' mentions from files.")

await Promise.all(
files.map(async (file) => {
let data = await fs.promises.readFile(file, "utf8");
const result = data.replace(/\.md/g, "");
await fs.promises.writeFile(file, result, "utf8");
let data = await fs.promises.readFile(file, 'utf8')
const result = data.replace(/\.md/g, '')
await fs.promises.writeFile(file, result, 'utf8')
})
);
)
}

async function generateNavigation(versions) {
try {
const navigation = await Promise.all(
versions.map(async (version) => {
const title = `${version}`;
const childrenDirs = getChildrenDirectories(docsPath);
const title = `${version}`
const childrenDirs = getChildrenDirectories(docsPath)
const links = await Promise.all([
{
title: "Content overview",
title: 'Content overview',
href: `/docs/golem-js/reference/overview`,
},
{
title: 'Default values',
href: `/docs/golem-js/reference/default-values`,
},
...childrenDirs.map(async (item) => {
const hrefPrefix = `/docs/golem-js/reference/${item}`;
const hrefPrefix = `/docs/golem-js/reference/${item}`

return {
title: item,
children: await getMarkdownTitles(
path.join(docsPath, item),
hrefPrefix
),
};
}
}),
]);
])

return {
title,
links,
};
}
})
);
)

return stringifyArray(navigation);
return stringifyArray(navigation)
} catch (error) {
console.error("Error in generateNavigation:", error);
console.error('Error in generateNavigation:', error)
}
}

function getChildrenDirectories(parentDir) {
try {
const entries = fs.readdirSync(parentDir, { withFileTypes: true });
const entries = fs.readdirSync(parentDir, { withFileTypes: true })

return entries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
.map((entry) => entry.name)
} catch (error) {
console.error("Error in getChildrenDirectories:", error);
console.error('Error in getChildrenDirectories:', error)
}
}

async function getMarkdownTitles(dirPath, hrefPrefix) {
try {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true })
const directlyNestedMarkdownFiles = entries.filter(
(entry) => entry.isFile() && entry.name.endsWith(".md")
);
(entry) => entry.isFile() && entry.name.endsWith('.md')
)

return directlyNestedMarkdownFiles.map((file) => {
let title = getTitleFromFile(file.name);
let title = getTitleFromFile(file.name)

// If title contains a ".", return the part after it.
// Otherwise, leave the title as it is.
const splitTitle = title.split(".");
const splitTitle = title.split('.')
if (splitTitle.length > 1) {
title = splitTitle.slice(1).join(".");
title = splitTitle.slice(1).join('.')
}

const href = path.join(hrefPrefix, file.name.replace(".md", ""));
const href = path.join(hrefPrefix, file.name.replace('.md', ''))

return {
title,
href,
};
});
}
})
} catch (error) {
console.error("Error in getMarkdownTitles:", error);
console.error('Error in getMarkdownTitles:', error)
}
}

function getTitleFromFile(filename) {
try {
return filename
.replace(/-/g, " ")
.replace(".md", "")
.split(" ")
.replace(/-/g, ' ')
.replace('.md', '')
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
.join(' ')
} catch (error) {
console.error("Error in getTitleFromFile:", error);
console.error('Error in getTitleFromFile:', error)
}
}

function stringifyObject(obj, indent = " ") {
function stringifyObject(obj, indent = ' ') {
const entries = Object.entries(obj)
.map(([key, value]) => {
const formattedValue =
value.constructor === Object
? stringifyObject(value, indent + " ")
? stringifyObject(value, indent + ' ')
: Array.isArray(value)
? stringifyArray(value, indent + " ")
: JSON.stringify(value);
return `${indent}${key}: ${formattedValue}`;
? stringifyArray(value, indent + ' ')
: JSON.stringify(value)
return `${indent}${key}: ${formattedValue}`
})
.join(",\n");
return `{\n${entries}\n${indent.slice(2)}}`;
.join(',\n')
return `{\n${entries}\n${indent.slice(2)}}`
}

function stringifyArray(array, indent = " ") {
function stringifyArray(array, indent = ' ') {
const entries = array
.map((entry) => {
return Array.isArray(entry)
? stringifyArray(entry, indent + " ")
? stringifyArray(entry, indent + ' ')
: entry.constructor === Object
? stringifyObject(entry, indent + " ")
: JSON.stringify(entry);
? stringifyObject(entry, indent + ' ')
: JSON.stringify(entry)
})
.join(",\n");
return `[\n${entries}\n${indent.slice(2)}]`;
.join(',\n')
return `[\n${entries}\n${indent.slice(2)}]`
}

main().catch((error) => console.error(error));
main().catch((error) => console.error(error))

export default undefined;
export default undefined
36 changes: 36 additions & 0 deletions src/components/DefaultValue.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { slugifyWithCounter } from '@sindresorhus/slugify'
import Link from 'next/link'
const slugify = slugifyWithCounter()

function DefaultValue({ title, children, defaultValue, id, referenceLink = null }) {
return (
<div className="mb-6 rounded-lg border border-lightbluedarker bg-white p-6 shadow-sm transition-shadow duration-200 dark:border-none dark:bg-darkcontent">
<h3
id={id}
className="mb-4 text-xl font-semibold text-primary dark:text-darkprimary"
>
{title}
</h3>
{children}
<div className='-mt-2'>
<span className="font-medium text-gray-900 dark:text-lightbluedarker">
Default value:{' '}
</span>
{referenceLink ? (
<Link
target='_blank'
rel='noopener noreferrer'
href={referenceLink} className="text-primary dark:text-darkprimary hover:underline">
{defaultValue}
</Link>
) : (
<span className="text-primary dark:text-darkprimary">
{defaultValue}
</span>
) }
</div>
</div>
)
}

export default DefaultValue
Loading
Loading