Skip to content

Commit

Permalink
Merge pull request #118 from DenverCoder544/documentation_version_reload
Browse files Browse the repository at this point in the history
indexJSON fetch by version, remove useless flag
  • Loading branch information
ZakarFin authored Sep 10, 2024
2 parents ebd78fe + 8bff148 commit de66df7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
27 changes: 16 additions & 11 deletions app/documentation/docs/[version]/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ import '@fortawesome/fontawesome-free/css/solid.min.css';
import ApiDocContentWrapper from '@/app/documentation/api/components/ApiDocContentWrapper'
import AccordionListWrapper from '@/app/documentation/docs/[version]/[slug]/AccordionListWrapper'

let indexJSON: MarkdownFileMetadata[] | null = null;
let indexJSON: { [key: string]: MarkdownFileMetadata[] } = {};

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

if (section) {
return { title: section.title }
}

}

const initIndexJSON = async (version: string) => {
if (!indexJSON || !indexJSON[version]) {
if (!indexJSON) {
indexJSON = {};
}
indexJSON[version] = await getVersionIndex(version);
}
}

export default async function SingleDocPage({
params,
}: {
Expand All @@ -37,11 +45,8 @@ export default async function SingleDocPage({


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

const activeSection = indexJSON?.find((item: MarkdownFileMetadata) => item.slug === params.slug);
await initIndexJSON(params.version)
const activeSection = indexJSON[version]?.find((item: MarkdownFileMetadata) => item.slug === params.slug);
const path = activeSection?.children[0].path;
if (!activeSection || !path) {
return <Error text='No documents found' code='404' />;
Expand All @@ -59,7 +64,7 @@ export default async function SingleDocPage({
<div style={{ display: 'flex', flexDirection: 'column', gap: '2rem' }}>
<VersionSidebar selectedVersion={params.version} versions={versions} baseHref='/documentation/docs/' />
<AccordionGroup>
<AccordionListWrapper items={indexJSON} initialOpenSections={[activeSection.slug]}/>
<AccordionListWrapper items={indexJSON[params.version]} initialOpenSections={[activeSection.slug]}/>
</AccordionGroup>
</div>
<div>
Expand Down
16 changes: 7 additions & 9 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ function compileMarkdownToHTML(markdown: string, startingSectionNumber: string):
anchorLinks,
}
}
export async function getVersionIndex(version: string, fetchHtmlContent: boolean = false) {
export async function getVersionIndex(version: string) {
const rootFolder = `_content/docs/${version}`;
if (!fs.existsSync(rootFolder) || !fs.statSync(rootFolder).isDirectory()) {
return null;
}
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;
});
}
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;
}
Expand Down

0 comments on commit de66df7

Please sign in to comment.