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'
28import { extractTocHeadings } from './src/mdx/remark-toc-headings.mjs'
39import path from 'path'
410import fs from 'fs'
@@ -7,87 +13,132 @@ const contentDirPath = 'docs'
713
814const 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+
1075const 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 : {
85+ image : {
1586 type : 'string' ,
1687 description :
17- 'The meta title of the doc, this will override the title extracted from the markdown and the nav title' ,
88+ 'The featured image of the post, not the same as og image. Use 1024x1024 with transparent background.' ,
89+ required : true ,
1890 } ,
19- description : {
91+ image_alt : {
2092 type : 'string' ,
21- description : 'The description of the doc' ,
93+ description : 'The featured image alt of the post' ,
94+ required : true ,
2295 } ,
23- image : {
24- type : 'string' ,
25- description : 'The image of the doc' ,
96+ } ,
97+ } ) )
98+
99+ const Guide = defineDocumentType ( ( ) => ( {
100+ name : 'Guide' ,
101+ filePathPattern : '**/guides/**/*.mdx' ,
102+ fields : {
103+ ...baseFields ,
104+ published_at : {
105+ type : 'date' ,
106+ description : 'The date the guide was published' ,
107+ required : true ,
26108 } ,
27- image_alt : {
28- type : 'string' ,
29- description : 'The image alt of the doc' ,
109+ updated_at : {
110+ type : 'date' ,
111+ description :
112+ 'The date the guide was last updated, will be set to published_at if not set' ,
30113 } ,
31- disable_edit : {
32- type : 'boolean ' ,
33- description : 'Disable the github edit button' ,
114+ featured : {
115+ type : 'nested ' ,
116+ of : Featured ,
34117 } ,
35118 tags : {
36119 type : 'list' ,
37120 of : {
38121 type : 'string' ,
39122 } ,
40- description : 'The tags of the post, used by guides' ,
123+ description : 'The tags of the post' ,
124+ required : true ,
41125 } ,
42126 languages : {
43127 type : 'list' ,
44128 of : {
45129 type : 'string' ,
46130 } ,
47- description : 'The languages of the content, used by guides ' ,
131+ description : 'The languages of the content' ,
48132 } ,
49133 start_steps : {
50134 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- } ,
135+ description : 'The start steps of the doc' ,
86136 } ,
87137 } ,
138+ computedFields,
88139} ) )
89140
90141export default makeSource ( {
91142 contentDirPath,
92- documentTypes : [ Doc ] ,
143+ documentTypes : [ Doc , Guide ] ,
93144} )
0 commit comments