Skip to content

Commit

Permalink
feat: Use Lesta framework (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
taorepoara authored Feb 18, 2023
1 parent 1e8e43c commit d7c24a0
Show file tree
Hide file tree
Showing 21 changed files with 453 additions and 1,456 deletions.
20 changes: 9 additions & 11 deletions .scripts/load-api.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const { exec } = require('child_process');
const Path = require('path');
const fs = require('fs-extra');
const yaml = require('yaml');
const https = require('https'); // or 'http' for http:// URLs
const jszip = require('jszip');
const { default: js } = require('minify/lib/js');
import * as Path from 'path';
import * as fs from 'fs';
import * as yaml from 'yaml';
import * as https from 'https'; // or 'http' for http:// URLs
import JSZip from 'jszip';

const srcPath = Path.join(__dirname, '..', 'src');
const srcPath = 'src';
const apiPath = Path.join(srcPath, 'api');

const components_yaml = yaml.parse(fs.readFileSync(Path.join(__dirname, 'doc-deps.yml')).toString())
const components_yaml = yaml.parse(fs.readFileSync('doc-deps.yml').toString())

for (const component of components_yaml.components) {
const component_path = Path.join(apiPath, component.name)
Expand All @@ -36,9 +34,9 @@ for (const component of components_yaml.components) {

console.log(`Unzip ${Path.basename(file_path)} ...`)
const file_content = fs.readFileSync(file_path)
const jszip_instance = new jszip()
const jszip_instance = new JSZip()
const result = await jszip_instance.loadAsync(file_content)
for(key of Object.keys(result.files)) {
for(let key of Object.keys(result.files)) {
const item = result.files[key];
if (item.dir) {
fs.mkdirSync(Path.join(component_path, item.name))
Expand Down
4 changes: 2 additions & 2 deletions .scripts/doc-deps.yml → doc-deps.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
components:
- name: components-api
url: https://github.com/lenra-io/components-api
version: v1.0.0-beta.69
file: lenra-api-docs-v1.0.0-beta.69.zip
version: v1.0.0-beta.71
file: lenra-api-docs-v1.0.0-beta.71.zip
110 changes: 110 additions & 0 deletions lesta.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Page, PageManager, RobotsManager, SitemapManager } from '@lenra/lesta';
import { getFilesRecursively } from '@lenra/lesta/lib/utils.js';
import * as Path from 'path';
import * as fs from 'fs';
import Showdown from 'showdown';

const languageFileRegex = /^(.+)[.]([a-z]{2})([.](md|html))$/
const converter = new Showdown.Converter();

/**
* Returns the website path managers
* @returns {PathManager[]}
*/
export function getManagers() {
return [new PageManager(pageLister), new RobotsManager(), new SitemapManager()];
}

/**
* Return a page list based on markdown files and the APIs HTML files
* @param {import('../config/configurator.js').Configuration} configuration The configuration
* @returns {Promise<Page[]>}
*/
async function pageLister(configuration) {
const markdownPages = await markdownPageLister(configuration);
const apiPages = await apiPageLister(configuration);
return [
...markdownPages,
...apiPages
];
}

/**
* Return a page list based on markdown files
* @param {import('../config/configurator.js').Configuration} configuration The configuration
* @returns {Promise<Page[]>}
*/
async function markdownPageLister(configuration) {
const markdownDirPath = Path.join(process.cwd(), "src/markdown");
const files = await getFilesRecursively(markdownDirPath);
const relativeFiles = files.filter(file => !Path.basename(file).startsWith('.'))
.map(file => Path.relative(markdownDirPath, file));
const langViews = {};
const matchFiles = relativeFiles.map(file => ({
file,
match: file.match(languageFileRegex)
}));
matchFiles.filter(({ match }) => match)
.forEach(({ file, match }) => {
const lang = match[2];
const f = match[1] + match[3];
if (!langViews[f]) langViews[f] = {};
langViews[f][lang] = file;
});
return matchFiles.filter(({ match }) => !match)
.map(({ file }) => {
const path = file.replace(/[.]md$/, '.html');
const title = file.replace(/[.]md$/, '');
const description = `${title} page`;
return new Page(
path,
'layout.pug',
langViews[file] || {},
{
title,
description,
// TODO: get markdown content
content: converter.makeHtml(fs.readFileSync(Path.join(markdownDirPath, file), 'utf8'))
}
)
})
}

/**
* Return a page list based on APIs HTML files
* @param {import('../config/configurator.js').Configuration} configuration The configuration
* @returns {Promise<Page[]>}
*/
async function apiPageLister(configuration) {
const apisDirPath = Path.join(process.cwd(), "src/api");
const files = (await getFilesRecursively(apisDirPath)).filter(p => p.endsWith(".html"));
const relativeFiles = files.filter(file => !Path.basename(file).startsWith('.'))
.map(file => Path.relative(apisDirPath, file));
const langViews = {};
const matchFiles = relativeFiles.map(file => ({
file,
match: file.match(languageFileRegex)
}));
matchFiles.filter(({ match }) => match)
.forEach(({ file, match }) => {
const lang = match[2];
const f = match[1] + match[3];
if (!langViews[f]) langViews[f] = {};
langViews[f][lang] = file;
});
return matchFiles.filter(({ match }) => !match)
.map(({ file }) => {
const path = file;
const content = fs.readFileSync(Path.join(apisDirPath, file), 'utf8');
const json = JSON.parse(fs.readFileSync(Path.join(apisDirPath, `${file}.json`), 'utf8'));
return new Page(
path,
'layout.pug',
langViews[file] || {},
{
...json,
content
}
)
})
}
Loading

0 comments on commit d7c24a0

Please sign in to comment.