Skip to content

Commit a223351

Browse files
committed
feat: refactor parseDocId
1 parent e7cb4d9 commit a223351

File tree

5 files changed

+94
-40
lines changed

5 files changed

+94
-40
lines changed

apps/docs/src/app/[locale]/docs/[[...slug]]/page.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DocsLayout } from '@/components/layout';
22
import { getPage } from '@/lib/page';
33
import { getDocsLayoutTree, getPageTreePeers } from '@/lib/pageTree';
44
import { type Page, type Source, sourceMap } from '@/lib/source';
5-
import { getDocsUrl, isAppDoc, isPagesDoc } from '@/lib/utils';
5+
import { getDocId, getDocUrl, parseDocId } from '@/lib/utils';
66
import { getMDXComponents } from '@/mdx-components';
77
import { Identity } from '@/mdx/Identity';
88
import { Void } from '@/mdx/Void';
@@ -19,20 +19,18 @@ export default async function Docs(props: {
1919
const slug = params.slug || [];
2020
const locale = params.locale;
2121
const source = sourceMap[locale];
22-
const docsUrl = getDocsUrl(slug);
23-
const id = `${locale}${docsUrl}`;
24-
const page = getPage(locale, docsUrl);
22+
const docUrl = getDocUrl(slug);
23+
const docId = getDocId(locale, docUrl);
24+
const page = getPage(locale, docUrl);
2525
if (!page) notFound();
26-
26+
const { isApp, isPages } = parseDocId(docId);
2727
// Markdown content requires await
2828
let { body: MdxContent, toc } = await page.data.load();
2929

30-
const isAppDocs = isAppDoc(id);
31-
const isPagesDocs = isPagesDoc(id);
3230
// source: app/getting-started/installation
3331
const ref = page.data.source;
3432
if (ref) {
35-
const refUrl = getDocsUrl(ref);
33+
const refUrl = getDocUrl(ref);
3634
const refPage = getPage(locale, refUrl);
3735
if (!refPage) notFound();
3836
const { body: MdxContent2, toc: toc2 } = await refPage.data.load();
@@ -45,7 +43,10 @@ export default async function Docs(props: {
4543
const isIndex = page.file.name === 'index';
4644

4745
return (
48-
<DocsLayout pageTree={getDocsLayoutTree(source.pageTree, slug)}>
46+
<DocsLayout
47+
docId={docId}
48+
pageTree={getDocsLayoutTree(source.pageTree, slug)}
49+
>
4950
<DocsPage
5051
toc={toc}
5152
full={page.data.full}
@@ -57,8 +58,8 @@ export default async function Docs(props: {
5758
components={getMDXComponents({
5859
// this allows you to link to other pages with relative file paths
5960
a: createRelativeLink(source, page),
60-
AppOnly: isAppDocs ? Identity : Void,
61-
PagesOnly: isPagesDocs ? Identity : Void,
61+
AppOnly: isApp ? Identity : Void,
62+
PagesOnly: isPages ? Identity : Void,
6263
})}
6364
/>
6465
{hasRelated ? <DocsRelated page={page} /> : null}
@@ -90,7 +91,7 @@ function DocsRelated({ page }: { page: Page }) {
9091
const relatedLinks = page.data.related?.links || [];
9192
const pages = relatedLinks
9293
.map((link) => {
93-
return getPage(locale, getDocsUrl(link));
94+
return getPage(locale, getDocUrl(link));
9495
})
9596
.filter(Boolean) as Page[];
9697
return (
@@ -126,7 +127,7 @@ export async function generateMetadata(props: {
126127
params: Promise<{ slug?: string[]; locale: Locale }>;
127128
}) {
128129
const params = await props.params;
129-
const url = getDocsUrl(params.slug);
130+
const url = getDocUrl(params.slug);
130131
const page = getPage(params.locale, url);
131132
if (!page) notFound();
132133

apps/docs/src/components/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import type { ReactNode } from 'react';
1111
export function DocsLayout({
1212
children,
1313
pageTree,
14+
docId,
1415
}: {
1516
children: ReactNode;
1617
pageTree: PageTree.Root;
18+
docId: string;
1719
}) {
1820
const baseOptions = useBaseOptions();
1921
const docsLayout: DocsLayoutProps = {

apps/docs/src/components/search-dialog.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { ORAMA_CONFIGS_MAP } from '@/lib/orama/config';
4-
import { getDocTag, getDocTags } from '@/lib/utils';
4+
import { getDocId, parseDocId } from '@/lib/utils';
55
import { OramaClient } from '@oramacloud/client';
66
import type { SharedProps } from 'fumadocs-ui/components/dialog/search';
77
import { useLocale } from 'next-intl';
@@ -22,23 +22,13 @@ export default function CustomSearchDialog(props: SharedProps) {
2222
}),
2323
[config],
2424
);
25-
const id = `${locale}${pathname}`;
26-
const tag = getDocTag(id);
27-
const tags = getDocTags(id);
28-
const isV13 = tags.includes('13');
29-
const isV14 = tags.includes('14');
30-
const version = isV13 ? '13' : isV14 ? '14' : '';
31-
const appTag =
32-
isV13 || isV14 ? `${locale}/docs/${version}/app` : `${locale}/docs/app`;
33-
const pagesTag =
34-
isV13 || isV14 ? `${locale}/docs/${version}/pages` : `${locale}/docs/pages`;
35-
const whereTag =
36-
isV13 || isV14 ? `${locale}/docs/${version}` : `${locale}/docs`;
25+
const docId = getDocId(locale, pathname);
26+
const { isPages, appTag, pagesTag, whereTag } = parseDocId(docId);
3727

3828
return (
3929
<SearchOrama
4030
{...props}
41-
defaultTag={tag}
31+
defaultTag={isPages ? pagesTag : appTag}
4232
whereTag={whereTag}
4333
allowClear
4434
tags={[

apps/docs/src/lib/orama/orama-document.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import type { OramaDocument } from 'fumadocs-core/search/orama-cloud';
2+
import type { Locale } from 'next-intl';
23
import { structure } from '../remark-plugins/remark-structure';
34
import type { Source } from '../source';
4-
import { getDocTag, isAppDoc, isPagesDoc } from '../utils';
5-
import { getDocsUrl } from '../utils';
5+
import { getDocId, getDocUrl, parseDocId } from '../utils';
66

77
export async function getOramaDocuments(
88
source: Source,
9-
lang: string,
9+
lang: Locale,
1010
i: number,
1111
) {
1212
const results: OramaDocument[] = [];
1313
const pages = source.getPages();
1414
for (const page of pages.slice(i * 20, (i + 1) * 20)) {
15-
const id = `${lang}${page.url}`;
16-
const isApp = isAppDoc(id);
17-
const isPages = isPagesDoc(id);
15+
const id = getDocId(lang, page.url);
16+
const { isApp, isPages } = parseDocId(id);
1817
const ref = page.data.source;
1918
let content = page.data.content;
2019

2120
if (ref) {
2221
// If the page has a reference, we need to get the content from the referenced page
23-
const url = getDocsUrl(ref);
22+
const url = getDocUrl(ref);
2423
const refPage = pages.find((p) => p.url === url);
2524
if (!refPage) continue;
2625
content = refPage.data.content;
@@ -47,7 +46,7 @@ export async function getOramaDocuments(
4746
url: page.url,
4847
title: page.data.title,
4948
description: page.data.description,
50-
tag: getDocTag(id),
49+
tag: parseDocId(id).docTag,
5150
};
5251

5352
results.push(result);

apps/docs/src/lib/utils.ts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Locale } from 'next-intl';
2+
13
/**
24
* en/docs/13
35
* en/docs/13/app
@@ -42,15 +44,75 @@ export const getDocTags = (id: string) => {
4244
return tags;
4345
};
4446

45-
export const isAppDoc = (id: string) => {
46-
return getDocTags(id).includes('app');
47+
/**
48+
* Get the full doc id
49+
* locale: en
50+
* docUrl: /docs/13/app/getting-started/installation
51+
* docId: en/docs/13/app/getting-started/installation
52+
*
53+
* @param locale
54+
* @param docUrl
55+
* @returns
56+
*/
57+
export const getDocId = (locale: Locale, docUrl: string) => {
58+
return `${locale}${docUrl}`;
4759
};
4860

49-
export const isPagesDoc = (id: string) => {
50-
return getDocTags(id).includes('pages');
61+
/**
62+
* docId: en/docs/13/app/getting-started/installation
63+
* -->
64+
*
65+
* @param id
66+
* @returns
67+
*/
68+
export const parseDocId = (id: string) => {
69+
const paths = id.split('/');
70+
const locale = paths[0];
71+
// const tags = paths.slice(0, 2);
72+
const slugs = paths.slice(2);
73+
let isApp = slugs[0] === 'app';
74+
let isPages = slugs[0] === 'pages';
75+
const isV13 = slugs[0] === '13';
76+
const isV14 = slugs[0] === '14';
77+
const isVLatest = !isV13 && !isV14;
78+
const version = isVLatest ? 'latest' : isV13 ? '13' : isV14 ? '14' : '';
79+
80+
if (isV13 || isV14) {
81+
isApp = slugs[1] === 'app';
82+
isPages = slugs[1] === 'pages';
83+
}
84+
85+
const docTag = isVLatest
86+
? paths.slice(0, 3).join('/')
87+
: paths.slice(0, 4).join('/');
88+
89+
const appTag = !isVLatest
90+
? `${locale}/docs/${version}/app`
91+
: `${locale}/docs/app`;
92+
const pagesTag = !isVLatest
93+
? `${locale}/docs/${version}/pages`
94+
: `${locale}/docs/pages`;
95+
const whereTag = !isVLatest ? `${locale}/docs/${version}` : `${locale}/docs`;
96+
97+
return {
98+
locale: paths[0],
99+
version,
100+
isApp,
101+
isPages,
102+
isV13,
103+
isV14,
104+
isVLatest,
105+
docsRoot: isVLatest ? '/docs' : `/docs/${slugs[0]}`,
106+
appDocsRoot: isVLatest ? '/docs/app' : `/docs/${slugs[0]}/app`,
107+
pagesDocsRoot: isVLatest ? '/docs/pages' : `/docs/${slugs[0]}/pages`,
108+
docTag,
109+
appTag,
110+
pagesTag,
111+
whereTag,
112+
};
51113
};
52114

53-
export function getDocsUrl(slug: string[] | string | undefined) {
115+
export function getDocUrl(slug: string[] | string | undefined) {
54116
if (typeof slug === 'string') {
55117
return `/docs/${slug}`;
56118
}

0 commit comments

Comments
 (0)