2024-06-14 11:09:53 +00:00
|
|
|
import { ContentPath, StaticPath, ViewsPath } from "./Utils/Config";
|
|
|
|
import DocsNav, { NavGroup, NavLink } from "./Utils/Nav";
|
|
|
|
import DocsRender from "./Utils/Render";
|
|
|
|
import FeatureSet from "CommonServer/Types/FeatureSet";
|
2023-12-29 21:27:05 +00:00
|
|
|
import Express, {
|
2024-06-14 11:09:53 +00:00
|
|
|
ExpressApplication,
|
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
ExpressStatic,
|
|
|
|
} from "CommonServer/Utils/Express";
|
|
|
|
import LocalFile from "CommonServer/Utils/LocalFile";
|
|
|
|
import logger from "CommonServer/Utils/Logger";
|
|
|
|
import "ejs";
|
2023-12-29 21:27:05 +00:00
|
|
|
|
2024-04-24 12:03:07 +00:00
|
|
|
const DocsFeatureSet: FeatureSet = {
|
2024-06-14 11:09:53 +00:00
|
|
|
init: async (): Promise<void> => {
|
|
|
|
const app: ExpressApplication = Express.getExpressApp();
|
2023-12-29 21:27:05 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
app.get("/docs", (_req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
res.redirect("/docs/introduction/getting-started");
|
|
|
|
});
|
2024-03-18 19:32:49 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
app.get(
|
|
|
|
"/docs/:categorypath/:pagepath",
|
|
|
|
async (_req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
try {
|
|
|
|
const fullPath: string =
|
|
|
|
`${_req.params["categorypath"]}/${_req.params["pagepath"]}`.toLowerCase();
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// read file from Content folder.
|
|
|
|
let contentInMarkdown: string = await LocalFile.read(
|
|
|
|
`${ContentPath}/${fullPath}.md`,
|
|
|
|
);
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// remove first line from content because we dont want to show title in content. Title is already in nav.
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
contentInMarkdown = contentInMarkdown.split("\n").slice(1).join("\n");
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const renderedContent: string =
|
|
|
|
await DocsRender.render(contentInMarkdown);
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const currentCategory: NavGroup | undefined = DocsNav.find(
|
|
|
|
(category: NavGroup) => {
|
|
|
|
return category.links.find((link: NavLink) => {
|
|
|
|
return link.url.toLocaleLowerCase().includes(fullPath);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2024-04-24 12:03:07 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const currrentNavLink: NavLink | undefined =
|
|
|
|
currentCategory?.links.find((link: NavLink) => {
|
|
|
|
return link.url.toLocaleLowerCase().includes(fullPath);
|
|
|
|
});
|
2024-04-24 11:20:04 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!currentCategory || !currrentNavLink) {
|
|
|
|
// render not found.
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
res.status(404);
|
|
|
|
return res.render(`${ViewsPath}/NotFound`, {
|
|
|
|
nav: DocsNav,
|
|
|
|
});
|
|
|
|
}
|
2024-03-18 21:33:37 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
res.render(`${ViewsPath}/Index`, {
|
|
|
|
nav: DocsNav,
|
|
|
|
content: renderedContent,
|
|
|
|
category: currentCategory,
|
|
|
|
link: currrentNavLink,
|
|
|
|
githubPath: fullPath,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
res.status(500);
|
|
|
|
return res.render(`${ViewsPath}/ServerError`, {
|
|
|
|
nav: DocsNav,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2024-04-24 11:20:04 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
app.use("/docs/static", ExpressStatic(StaticPath));
|
|
|
|
},
|
2024-04-24 11:20:04 +00:00
|
|
|
};
|
2023-12-29 21:27:05 +00:00
|
|
|
|
2024-04-24 12:03:07 +00:00
|
|
|
export default DocsFeatureSet;
|