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'
2
8
import { extractTocHeadings } from './src/mdx/remark-toc-headings.mjs'
3
9
import path from 'path'
4
10
import fs from 'fs'
@@ -7,87 +13,132 @@ const contentDirPath = 'docs'
7
13
8
14
const branch = process . env . NEXT_PUBLIC_GITHUB_BRANCH || 'main'
9
15
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
+
10
75
const Doc = defineDocumentType ( ( ) => ( {
11
76
name : 'Doc' ,
12
- filePathPattern : '**/*.mdx' ,
77
+ filePathPattern : '!**/guides/**/*.mdx' ,
78
+ fields : baseFields ,
79
+ computedFields,
80
+ } ) )
81
+
82
+ const Featured = defineNestedType ( ( ) => ( {
83
+ name : 'Featured' ,
13
84
fields : {
14
- title_seo : {
85
+ image : {
15
86
type : 'string' ,
16
87
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 ,
18
90
} ,
19
- description : {
91
+ image_alt : {
20
92
type : 'string' ,
21
- description : 'The description of the doc' ,
93
+ description : 'The featured image alt of the post' ,
94
+ required : true ,
22
95
} ,
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 ,
26
108
} ,
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' ,
30
113
} ,
31
- disable_edit : {
32
- type : 'boolean ' ,
33
- description : 'Disable the github edit button' ,
114
+ featured : {
115
+ type : 'nested ' ,
116
+ of : Featured ,
34
117
} ,
35
118
tags : {
36
119
type : 'list' ,
37
120
of : {
38
121
type : 'string' ,
39
122
} ,
40
- description : 'The tags of the post, used by guides' ,
123
+ description : 'The tags of the post' ,
124
+ required : true ,
41
125
} ,
42
126
languages : {
43
127
type : 'list' ,
44
128
of : {
45
129
type : 'string' ,
46
130
} ,
47
- description : 'The languages of the content, used by guides ' ,
131
+ description : 'The languages of the content' ,
48
132
} ,
49
133
start_steps : {
50
134
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' ,
86
136
} ,
87
137
} ,
138
+ computedFields,
88
139
} ) )
89
140
90
141
export default makeSource ( {
91
142
contentDirPath,
92
- documentTypes : [ Doc ] ,
143
+ documentTypes : [ Doc , Guide ] ,
93
144
} )
0 commit comments