kompendium
Version:
Documentation generator for Stencil components
50 lines (41 loc) • 1.25 kB
text/typescript
import { basename, dirname, resolve } from 'path';
import { readFile } from 'fs/promises';
import { KompendiumGuide, KompendiumConfig, Guide } from '../types';
export async function findGuides(
config: Partial<KompendiumConfig>,
): Promise<KompendiumGuide[]> {
const nodes = config.guides.map(createMenuNode('/')).flat();
const promises = nodes.map(createGuide);
return Promise.all(promises);
}
interface MenuNode {
menupath: string;
filepath: string;
}
export const createMenuNode =
(path: string) =>
(guide: Guide): MenuNode | MenuNode[] => {
if (typeof guide !== 'string') {
const newPath = path + guide.name + '/';
return guide.children.map(createMenuNode(newPath)).flat();
}
return {
menupath: path,
filepath: guide,
};
};
export const createGuide = async ({
menupath: path,
filepath,
}: MenuNode): Promise<KompendiumGuide> => {
const content = await readFile(filepath, 'utf8');
return {
dirPath: dirname(filepath),
fileName: basename(filepath),
filePath: resolve(filepath),
data: {
path: path + basename(filepath),
},
content: content,
};
};