-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathgetExtensions.ts
More file actions
105 lines (86 loc) · 3.26 KB
/
getExtensions.ts
File metadata and controls
105 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from 'fs'
import path from 'path'
import { glob } from 'fast-glob'
import { cache } from 'react'
import frontmatter from 'front-matter'
import { ExtensionMetaWithUrl, PageFrontmatter } from '@/types'
/**
* Gets all extensions from MDX files.
* Reads frontmatter directly from files instead of compiling MDX for better performance.
* Cached per-request to avoid duplicate file reads.
*/
export const getExtensions = cache(async (pathParam: string = '') => {
let pages = (await glob(`**/*.mdx`, { cwd: `src/${pathParam}` })).filter((p) => {
return !p.endsWith('index.mdx') && !p.endsWith('overview.mdx')
})
const pathPrefix = pathParam ? `${pathParam}/` : ''
let allExtensions = (await Promise.all(
pages.map(async (page) => {
let pagePath = `/${pathPrefix + page}`
// Read frontmatter directly without compiling MDX - much faster and less memory
const filePath = path.join(process.cwd(), 'src', pathPrefix + page)
const mdxContent = fs.readFileSync(filePath, 'utf-8')
const { attributes } = frontmatter(mdxContent) as { attributes: PageFrontmatter }
const extensionData = attributes.extension as ExtensionMetaWithUrl | undefined
const pageTags = attributes.tags || []
if (!extensionData) {
return null
}
// Check if the page has a beta tag in the frontmatter
const hasBetaTag = pageTags.some(
(tag: { type: string }) => typeof tag === 'object' && tag.type === 'beta',
)
// If beta tag is present, mark extension as new
if (hasBetaTag && !extensionData.isNew) {
extensionData.isNew = true
}
// Check for 'start' and 'team' tag types and add corresponding tags to the extension
const hasStartTag = pageTags.some(
(tag: { type: string }) => typeof tag === 'object' && tag.type === 'start',
)
const hasTeamTag = pageTags.some(
(tag: { type: string }) => typeof tag === 'object' && tag.type === 'team',
)
const hasAddonTag = pageTags.some(
(tag: { type: string }) => typeof tag === 'object' && tag.type === 'addon',
)
// Initialize tags array if it doesn't exist
if (!extensionData.tags) {
extensionData.tags = []
}
// Add "Start" tag if start type is present
if (hasStartTag && !extensionData.tags.includes('Start')) {
extensionData.tags.push('Start')
}
// Add "Team" tag if team type is present
if (hasTeamTag && !extensionData.tags.includes('Team')) {
extensionData.tags.push('Team')
}
// Add "Addon" tag if addon type is present
if (hasAddonTag && !extensionData.tags.includes('Addon')) {
extensionData.tags.push('Addon')
}
return [
pathParam + pagePath,
{
...extensionData,
path: page,
url: pagePath.replace('content/', '').replace('.mdx', ''),
},
]
}),
)) as Array<[string, ExtensionMetaWithUrl]>
allExtensions = allExtensions
.filter((ext) => ext !== null)
.sort((extA, extB) => {
if (extA[1].name < extB[1].name) {
return -1
}
if (extA[1].name > extB[1].name) {
return 1
}
return 0
})
const extensionData = Object.fromEntries(allExtensions)
return extensionData
})