Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit a71f623

Browse files
committed
move guides into new guides doc type
1 parent c4e3f6b commit a71f623

File tree

12 files changed

+124
-93
lines changed

12 files changed

+124
-93
lines changed

contentlayer.config.ts

Lines changed: 99 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { defineDocumentType, makeSource } from 'contentlayer2/source-files'
1+
import {
2+
ComputedFields,
3+
defineDocumentType,
4+
defineNestedType,
5+
FieldDefs,
6+
makeSource,
7+
} from 'contentlayer2/source-files'
28
import { extractTocHeadings } from './src/mdx/remark-toc-headings.mjs'
39
import path from 'path'
410
import fs from 'fs'
@@ -7,87 +13,126 @@ const contentDirPath = 'docs'
713

814
const branch = process.env.NEXT_PUBLIC_GITHUB_BRANCH || 'main'
915

16+
const baseFields: FieldDefs = {
17+
title_seo: {
18+
type: 'string',
19+
description:
20+
'The meta title of the doc, this will override the title extracted from the markdown and the nav title',
21+
},
22+
description: {
23+
type: 'string',
24+
description: 'The description of the doc',
25+
},
26+
image: {
27+
type: 'string',
28+
description: 'The image of the doc',
29+
},
30+
image_alt: {
31+
type: 'string',
32+
description: 'The image alt of the doc',
33+
},
34+
disable_edit: {
35+
type: 'boolean',
36+
description: 'Disable the github edit button',
37+
},
38+
}
39+
40+
const computedFields: ComputedFields = {
41+
slug: {
42+
type: 'string',
43+
resolve: (doc) => doc._raw.flattenedPath,
44+
},
45+
toc: { type: 'json', resolve: (doc) => extractTocHeadings(doc.body.raw) },
46+
title: {
47+
type: 'string',
48+
resolve: async (doc) => {
49+
const headings = await extractTocHeadings(doc.body.raw, [1])
50+
51+
return headings[0]?.value
52+
},
53+
},
54+
editUrl: {
55+
type: 'string',
56+
resolve: (doc) =>
57+
`https://github.com/nitrictech/docs/edit/${branch}/docs/${doc._raw.sourceFilePath}`,
58+
},
59+
lastModified: {
60+
type: 'date',
61+
resolve: (doc) => {
62+
// Get the full path to the markdown file
63+
const filePath = path.join(
64+
process.cwd(),
65+
contentDirPath,
66+
doc._raw.sourceFilePath,
67+
)
68+
// Extract and return the last modified date
69+
const stats = fs.statSync(filePath)
70+
return stats.mtime // This is the last modified date
71+
},
72+
},
73+
}
74+
1075
const Doc = defineDocumentType(() => ({
1176
name: 'Doc',
12-
filePathPattern: '**/*.mdx',
77+
filePathPattern: '!**/guides/**/*.mdx',
78+
fields: baseFields,
79+
computedFields,
80+
}))
81+
82+
const Featured = defineNestedType(() => ({
83+
name: 'Featured',
1384
fields: {
14-
title_seo: {
15-
type: 'string',
16-
description:
17-
'The meta title of the doc, this will override the title extracted from the markdown and the nav title',
18-
},
19-
description: {
20-
type: 'string',
21-
description: 'The description of the doc',
22-
},
2385
image: {
2486
type: 'string',
25-
description: 'The image of the doc',
87+
description: 'The featured image of the post, not the same as og image',
88+
required: true,
2689
},
2790
image_alt: {
2891
type: 'string',
29-
description: 'The image alt of the doc',
92+
description: 'The featured image alt of the post',
93+
required: true,
94+
},
95+
},
96+
}))
97+
98+
const Guide = defineDocumentType(() => ({
99+
name: 'Guide',
100+
filePathPattern: '**/guides/**/*.mdx',
101+
fields: {
102+
...baseFields,
103+
published_at: {
104+
type: 'date',
105+
description: 'The date the guide was published',
106+
required: false,
30107
},
31-
disable_edit: {
32-
type: 'boolean',
33-
description: 'Disable the github edit button',
108+
featured: {
109+
type: 'nested',
110+
of: Featured,
34111
},
35112
tags: {
36113
type: 'list',
37114
of: {
38115
type: 'string',
39116
},
40-
description: 'The tags of the post, used by guides',
117+
description: 'The tags of the post',
118+
required: true,
41119
},
42120
languages: {
43121
type: 'list',
44122
of: {
45123
type: 'string',
46124
},
47-
description: 'The languages of the content, used by guides',
125+
description: 'The languages of the content',
48126
},
49127
start_steps: {
50128
type: 'markdown',
51-
description: 'The start steps of the doc, used by guides',
52-
},
53-
},
54-
computedFields: {
55-
slug: {
56-
type: 'string',
57-
resolve: (doc) => doc._raw.flattenedPath,
58-
},
59-
toc: { type: 'json', resolve: (doc) => extractTocHeadings(doc.body.raw) },
60-
title: {
61-
type: 'string',
62-
resolve: async (doc) => {
63-
const headings = await extractTocHeadings(doc.body.raw, [1])
64-
65-
return headings[0]?.value
66-
},
67-
},
68-
editUrl: {
69-
type: 'string',
70-
resolve: (doc) =>
71-
`https://github.com/nitrictech/docs/edit/${branch}/docs/${doc._raw.sourceFilePath}`,
72-
},
73-
lastModified: {
74-
type: 'date',
75-
resolve: (doc) => {
76-
// Get the full path to the markdown file
77-
const filePath = path.join(
78-
process.cwd(),
79-
contentDirPath,
80-
doc._raw.sourceFilePath,
81-
)
82-
// Extract and return the last modified date
83-
const stats = fs.statSync(filePath)
84-
return stats.mtime // This is the last modified date
85-
},
129+
description: 'The start steps of the doc',
86130
},
87131
},
132+
computedFields,
88133
}))
89134

90135
export default makeSource({
91136
contentDirPath,
92-
documentTypes: [Doc],
137+
documentTypes: [Doc, Guide],
93138
})

src/app/(sitemaps)/sitemap-0.xml/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { allDocs } from '@/content'
1+
import { allDocuments } from '@/content'
22
import staticPaths from '@/assets/sitemap.json'
33
import { BASE_URL } from '@/lib/constants'
44

@@ -22,7 +22,7 @@ export async function GET() {
2222
priority: 0.7,
2323
}))
2424

25-
const docPages: SitemapItem[] = allDocs.map((page) => ({
25+
const docPages: SitemapItem[] = allDocuments.map((page) => ({
2626
loc: page.slug === '' ? URL : `${URL}/${page.slug}`,
2727
lastmod: new Date(page.lastModified).toISOString(),
2828
changefreq: 'daily',

src/app/[[...slug]]/layout.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Prose } from '@/components/Prose'
22

33
import React from 'react'
4-
import { allDocs } from '@/content'
4+
import { allDocuments, Guide, isType } from '@/content'
55
import DocToc from '@/components/layout/DocToC'
66
import { Button } from '@/components/ui/button'
77
import { GitHubIcon } from '@/components/icons/GitHubIcon'
@@ -17,19 +17,19 @@ export default function DocLayout({
1717
params: { slug: string[] }
1818
}) {
1919
const slug = params.slug ? decodeURI(params.slug.join('/')) : ''
20-
const doc = allDocs.find((p) => p.slug === slug)
20+
const doc = allDocuments.find((p) => p.slug === slug)
2121

2222
if (!doc) {
2323
return <>{children}</>
2424
}
2525

26-
const startSteps = doc.start_steps && (
26+
const startSteps = isType('Guide') && (doc as Guide).start_steps && (
2727
<Code
2828
hideBashPanel
2929
codeblock={{
3030
lang: 'bash',
3131
meta: '',
32-
value: doc.start_steps?.raw,
32+
value: (doc as Guide).start_steps?.raw || '',
3333
}}
3434
/>
3535
)

src/app/[[...slug]]/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { notFound } from 'next/navigation'
22
import MDXContent from '@/components/MDXContent'
33
import { Metadata } from 'next/types'
44
import { BASE_URL } from '@/lib/constants'
5-
import { allDocs } from '@/content'
5+
import { allDocuments } from '@/content'
66
import { getNavInfo } from '@/lib/getNavInfo'
77
import { title } from 'radash'
88
import { OpenGraph } from 'next/dist/lib/metadata/types/opengraph-types'
@@ -13,7 +13,7 @@ export async function generateMetadata({
1313
params: { slug: string[] }
1414
}): Promise<Metadata | undefined> {
1515
const slug = params.slug ? decodeURI(params.slug.join('/')) : ''
16-
const doc = allDocs.find((p) => p.slug === slug)
16+
const doc = allDocuments.find((p) => p.slug === slug)
1717

1818
if (!doc) {
1919
return
@@ -76,15 +76,15 @@ export async function generateMetadata({
7676
}
7777

7878
export const generateStaticParams = async () => {
79-
return allDocs.map((p) => ({
79+
return allDocuments.map((p) => ({
8080
slug: p.slug.split('/').map((name) => decodeURI(name)),
8181
}))
8282
}
8383

8484
export default function Page({ params }: { params: { slug: string[] } }) {
8585
const slug = params.slug ? decodeURI(params.slug.join('/')) : ''
8686

87-
const doc = allDocs.find((p) => p.slug === slug)
87+
const doc = allDocuments.find((p) => p.slug === slug)
8888

8989
if (!doc) {
9090
return notFound()

src/app/guides/page.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
BreadcrumbSeparator,
99
} from '@/components/ui/breadcrumb'
1010
import { Heading } from '@/components/ui/heading'
11-
import { allDocs } from '@/content'
11+
import { allGuides } from '@/content'
1212
import { BASE_URL } from '@/lib/constants'
1313
import Link from 'next/link'
1414
import { Metadata } from 'next/types'
@@ -34,8 +34,6 @@ export const metadata: Metadata = {
3434
}
3535

3636
export default function GuidesPage() {
37-
const allGuides = allDocs.filter((doc) => doc.slug.startsWith('guides/'))
38-
3937
const allTags = allGuides
4038
.reduce((acc: string[], guide) => {
4139
if (guide.tags) {
@@ -69,7 +67,7 @@ export default function GuidesPage() {
6967
</div>
7068
<div className="-mx-2 border-t px-4 sm:-mx-6 lg:-mx-8">
7169
<div className="mx-auto max-w-7xl px-4">
72-
<GuidePage guides={allGuides} allTags={allTags} />
70+
<GuidePage allTags={allTags} />
7371
</div>
7472
</div>
7573
</>

src/components/Breadcrumbs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
BreadcrumbPage,
88
BreadcrumbSeparator,
99
} from './ui/breadcrumb'
10-
import { Doc } from '@/content'
10+
import { Doc, Guide } from '@/content'
1111
import { getNavInfo, NavInfo } from '@/lib/getNavInfo'
1212
import Link from 'next/link'
1313

1414
interface Props {
15-
doc: Doc
15+
doc: Doc | Guide
1616
className?: string
1717
}
1818

src/components/guides/GuideItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Doc } from '@/content'
1+
import type { Guide } from '@/content'
22
import React from 'react'
33
import { cn } from '@/lib/utils'
44
import Link from 'next/link'
55
import { LanguageIcon } from '../icons/LanguageIcon'
66
import { Language } from '@/lib/constants'
77

88
interface Props {
9-
guide: Doc
9+
guide: Guide
1010
featured?: boolean
1111
}
1212

src/components/guides/GuideList.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
import React from 'react'
44
import { GuideItem } from './GuideItem'
5-
import { Doc } from '@/content'
65
import { cn } from '@/lib/utils'
76
import useParams from '@/hooks/useParams'
7+
import { allGuides } from '@/content'
88

99
interface Props {
10-
guides: Doc[]
1110
className?: string
1211
}
1312

14-
const GuideList: React.FC<Props> = ({ className, guides }) => {
13+
const GuideList: React.FC<Props> = ({ className }) => {
1514
const { searchParams } = useParams()
1615
const selectedTags = searchParams?.get('tags')?.split(',') || []
1716
const selectedLangs = searchParams?.get('langs')?.split(',') || []
1817

19-
const filteredGuides = guides
18+
const filteredGuides = allGuides
2019
.filter((guide) => {
2120
let include = true
2221

src/components/guides/GuidePage.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import { Doc } from '@/content'
21
import React, { Suspense } from 'react'
32

43
import { GuideFilters } from './GuideFilters'
54
import GuideList from './GuideList'
6-
import { Button } from '../ui/button'
7-
import { AdjustmentsHorizontalIcon } from '@heroicons/react/24/outline'
85
import GuideMobileFilters from './GuideMobileFilters'
96

107
interface Props {
11-
guides: Doc[]
128
allTags: string[]
139
}
1410

15-
const GuidePage: React.FC<Props> = ({ guides, allTags }) => {
11+
const GuidePage: React.FC<Props> = ({ allTags }) => {
1612
return (
1713
<div className="gap-x-4 lg:grid lg:grid-cols-[280px,1fr]">
1814
<div className="hidden border-r pb-10 lg:block">
@@ -27,10 +23,7 @@ const GuidePage: React.FC<Props> = ({ guides, allTags }) => {
2723
<GuideMobileFilters allTags={allTags} />
2824
</div>
2925
<Suspense>
30-
<GuideList
31-
guides={guides}
32-
className="relative mx-2 my-4 w-full sm:px-8 lg:my-10"
33-
/>
26+
<GuideList className="relative mx-2 my-4 w-full sm:px-8 lg:my-10" />
3427
</Suspense>
3528
</div>
3629
)

0 commit comments

Comments
 (0)