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

data loading in documentation refactored #113

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
22 changes: 11 additions & 11 deletions app/documentation/docs/[version]/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import VersionSidebar from '@/components/VersionSidebar'
import { compareSemanticVersions } from '@/utils/misc'
import Link from 'next/link'
import styles from '@/styles/accordion.module.scss'
import { cleanTags, getVersionIndex, readAndConcatMarkdownFiles } from '@/lib/utils'
import { cleanTags, getVersionIndex } from '@/lib/utils'
import availableVersions from '@/_content/docs';
import Error from '@/components/Error'
import { DocAnchorLinksType, MarkdownFileMetadata } from '@/types/types'
Expand All @@ -14,12 +14,16 @@ import '@fortawesome/fontawesome-free/css/fontawesome.min.css';
import '@fortawesome/fontawesome-free/css/solid.min.css';
import ApiDocContentWrapper from '@/app/documentation/api/components/ApiDocContentWrapper'

let indexJSON: MarkdownFileMetadata[] | null = null;

export const generateMetadata = async ({
params,
}: {
params: { slug: string; version: string }
}) => {
const indexJSON = await getVersionIndex(params.version);
if (!indexJSON) {
indexJSON = await getVersionIndex(params.version, true);
}
const section = indexJSON?.find((item: MarkdownFileMetadata) => item.slug === params.slug);

if (section) {
Expand Down Expand Up @@ -52,21 +56,17 @@ export default async function SingleDocPage({
params: { slug: string; version: string }
}) {

const indexJSON = await getVersionIndex(params.version);
const { version } = params;
if (!indexJSON) {
indexJSON = await getVersionIndex(version, true);
}

const activeSection = indexJSON?.find((item: MarkdownFileMetadata) => item.slug === params.slug);
const path = activeSection?.children[0].path;

const imagesRuntimePath = '/assets/docs/' + params.version + '/resources/';
if (!activeSection || !path) {
return <Error text='No documents found' code='404' />;
}

await indexJSON.forEach(async (element: MarkdownFileMetadata) => {
const { html, anchorLinks } = await readAndConcatMarkdownFiles(element, imagesRuntimePath, indexJSON, element.title);
element.anchorLinks = anchorLinks;
element.html = html;
});

const versions = [
...new Set(
availableVersions
Expand Down
17 changes: 13 additions & 4 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ function compileMarkdownToHTML(markdown: string, startingSectionNumber: string):
anchorLinks,
}
}

export async function getVersionIndex(version: string) {
export async function getVersionIndex(version: string, fetchHtmlContent: boolean = false) {
const rootFolder = `_content/docs/${version}`;
if (!fs.existsSync(rootFolder) || !fs.statSync(rootFolder).isDirectory()) {
return null;
}
const allDocs = (await import(`_content/docs/${version}/index.js`)).default;
return allDocs;
const indexJSON = (await import(`_content/docs/${version}/index.js`)).default;

if (fetchHtmlContent) {
const imagesRuntimePath = '/assets/docs/' + version + '/resources/';
await indexJSON.forEach(async (element: MarkdownFileMetadata) => {
const { html, anchorLinks } = await readAndConcatMarkdownFiles(element, imagesRuntimePath, indexJSON, element.title);
element.anchorLinks = anchorLinks;
element.html = html;
});
}

return indexJSON;
}

export const readMarkdownFile = async function(filePath: string, imagesPath: string = '') {
Expand Down
Loading