-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx.ts
More file actions
138 lines (112 loc) · 3.76 KB
/
mdx.ts
File metadata and controls
138 lines (112 loc) · 3.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import { MDXRemote } from "next-mdx-remote/rsc";
import { mdxComponents } from "./mdx/components";
import { MDXComponents } from "mdx/types";
import { ReactDocsConfig } from "./config";
interface Heading {
id: string;
text: string;
level: number;
}
function slugify(str: string) {
return String(str)
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
function extractHeadings(source: string): Heading[] {
const headings: Heading[] = [];
const lines = source.split("\n");
let inCodeBlock = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Check for code block delimiters
if (line.trim().startsWith("```")) {
inCodeBlock = !inCodeBlock;
continue;
}
// Skip if we're inside a code block
if (inCodeBlock) continue;
// Match headings, but only if they're not inside a code block
const headingMatch = line.match(/^(#{1,3})\s+(.+)$/);
if (headingMatch) {
const level = headingMatch[1].length;
const text = headingMatch[2];
const id = slugify(text);
headings.push({ level, text, id });
}
}
return headings;
}
export async function getDocBySlug(docsConfig: ReactDocsConfig, slug: string) {
const directory = path.join(process.cwd(), docsConfig.contentPath);
const fullPath = path.join(directory, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data: frontmatter, content: source } = matter(fileContents);
const headings = extractHeadings(source);
return {
frontmatter,
content: await MDXRemote({
source,
components: mdxComponents as MDXComponents,
}),
headings,
};
}
export function getAllDocs(docsConfig: ReactDocsConfig) {
const docs: { slug: string; frontmatter: any }[] = [];
function traverseDirectory(currentPath: string, baseDir: string) {
const files = fs.readdirSync(currentPath);
for (const file of files) {
const fullPath = path.join(currentPath, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
traverseDirectory(fullPath, baseDir);
} else if (file.endsWith(".mdx")) {
const relativePath = path.relative(baseDir, fullPath);
const slug = relativePath.replace(/\.mdx$/, "");
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data: frontmatter } = matter(fileContents);
docs.push({
slug,
frontmatter,
});
}
}
}
const docsDirectory = path.join(process.cwd(), docsConfig.contentPath);
traverseDirectory(docsDirectory, docsDirectory);
return docs;
}
// Cache for docs content
const docsContentCache = new Map<string, string>();
export function getAllDocsContent(docsConfig: ReactDocsConfig): string {
const cacheKey = docsConfig.contentPath;
if (docsContentCache.has(cacheKey)) {
return docsContentCache.get(cacheKey)!;
}
const content: string[] = [];
function traverseDirectory(currentPath: string) {
const files = fs.readdirSync(currentPath);
for (const file of files) {
const fullPath = path.join(currentPath, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
traverseDirectory(fullPath);
} else if (file.endsWith(".mdx")) {
const fileContents = fs.readFileSync(fullPath, "utf8");
const { content: mdxContent } = matter(fileContents);
content.push(mdxContent);
}
}
}
const docsDirectory = path.join(process.cwd(), docsConfig.contentPath);
traverseDirectory(docsDirectory);
const joinedContent = content.join('\n\n---\n\n');
docsContentCache.set(cacheKey, joinedContent);
return joinedContent;
}