-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
220 lines (190 loc) · 6.6 KB
/
main.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { walk } from "https://deno.land/std/fs/mod.ts";
import { join, normalize, dirname } from "https://deno.land/std/path/mod.ts";
import { processPartials } from "./partials.ts";
interface CategoryConfig {
position?: number;
label?: string;
}
interface MDXFrontmatter {
sidebar_position?: number;
sidebar_label?: string;
title?: string;
[key: string]: string | number | undefined;
}
async function readCategoryConfig(
dirPath: string
): Promise<CategoryConfig | null> {
try {
const categoryPath = join(dirPath, "_category_.json");
const content = await Deno.readTextFile(categoryPath);
return JSON.parse(content);
} catch {
return null;
}
}
async function parseMDXFrontmatter(content: string): Promise<MDXFrontmatter> {
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) return {};
const frontmatter = frontmatterMatch[1];
const lines = frontmatter.split("\n");
const result: MDXFrontmatter = {};
for (const line of lines) {
const [key, value] = line.split(":").map((s) => s.trim());
if (key && value) {
// Remove quotes if present
const cleanValue = value.replace(/^["']|["']$/g, "");
if (key === "sidebar_position") {
result.sidebar_position = parseInt(cleanValue, 10);
} else {
result[key] = cleanValue;
}
}
}
return result;
}
interface FileEntry {
path: string;
content: string;
frontmatter: MDXFrontmatter;
categoryConfig: CategoryConfig | null;
parentDirs: string[];
}
function generateUrl(filePath: string): string {
// Remove the base docs directory prefix and .mdx extension
const relativePath = filePath
.replace(/^.*?\/docs\//, "")
.replace(/\.mdx$/, "");
// Convert the path to URL format
const urlPath = relativePath.split("/").join("/");
return `https://hasura.io/docs/3.0/${urlPath}/`;
}
async function combineMarkdownFiles(directory: string, outputFile: string) {
const allContent: FileEntry[] = [];
const categoryConfigs = new Map<string, CategoryConfig | null>();
// First pass: collect all files and their metadata
for await (const entry of walk(directory, {
exts: [".mdx"],
includeDirs: false,
})) {
// Skip files that begin with an underscore (partials)
if (entry.name.startsWith("_")) {
continue;
}
try {
const content = await Deno.readTextFile(entry.path);
const frontmatter = await parseMDXFrontmatter(content);
const dirPath = dirname(entry.path);
// Process partials in the content
const processedContent = await processPartials(content, directory);
// Get all parent directories
const parentDirs: string[] = [];
let currentDir = dirPath;
while (currentDir !== directory) {
parentDirs.push(currentDir);
currentDir = dirname(currentDir);
}
parentDirs.reverse(); // Order from root to leaf
// Get or cache category config for this directory and all parent directories
const dirConfigs = new Map<string, CategoryConfig | null>();
for (const dir of [dirPath, ...parentDirs]) {
let config: CategoryConfig | null;
if (!categoryConfigs.has(dir)) {
config = await readCategoryConfig(dir);
categoryConfigs.set(dir, config);
} else {
const existingConfig = categoryConfigs.get(dir);
if (existingConfig === undefined) {
throw new Error(`Category config not found for directory ${dir}`);
}
config = existingConfig;
}
dirConfigs.set(dir, config);
}
allContent.push({
path: entry.path,
content: processedContent,
frontmatter,
categoryConfig: dirConfigs.get(dirPath)!,
parentDirs,
});
} catch (error) {
console.error(`Error reading file ${entry.path}:`, error);
}
}
// Sort content based on hierarchical category positions and sidebar position
allContent.sort((a, b) => {
// Compare each level of the directory hierarchy
const maxDepth = Math.max(a.parentDirs.length, b.parentDirs.length);
for (let i = 0; i < maxDepth; i++) {
const aDir = a.parentDirs[i];
const bDir = b.parentDirs[i];
// If one path is shorter, it should come first
if (!aDir && !bDir) break;
if (!aDir) return -1;
if (!bDir) return 1;
// Compare category positions at this level
const aConfig = categoryConfigs.get(aDir);
const bConfig = categoryConfigs.get(bDir);
const aPos = aConfig?.position ?? 0;
const bPos = bConfig?.position ?? 0;
if (aPos !== bPos) {
return aPos - bPos;
}
}
// If we're in the same directory, compare sidebar positions
const aPos = a.frontmatter.sidebar_position ?? 0;
const bPos = b.frontmatter.sidebar_position ?? 0;
if (aPos !== bPos) {
return aPos - bPos;
}
// If positions are equal, sort alphabetically by path
return a.path.localeCompare(b.path);
});
// Generate the output content
const outputContent = allContent
.map((entry) => {
const title =
entry.frontmatter.title ||
entry.frontmatter.sidebar_label ||
entry.path;
const url = generateUrl(entry.path);
return `\n\n--- File: ${entry.path} ---\nURL: ${url}\n# ${title}\n\n${entry.content}`;
})
.join("\n");
try {
await Deno.writeTextFile(outputFile, outputContent);
console.log(`Successfully combined all MDX files into ${outputFile}`);
} catch (error) {
console.error("Error writing to output file:", error);
}
}
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
// Get the path from command line arguments
const subPath = Deno.args[0];
const baseDocsDir = "../ddn-docs/docs";
// If no subpath provided, use the base docs directory
if (!subPath) {
const outputFile = "./allContent.txt";
try {
await combineMarkdownFiles(baseDocsDir, outputFile);
} catch (error) {
console.error("Error processing files:", error);
Deno.exit(1);
}
Deno.exit(0);
}
// Normalize the path by removing extra slashes and resolving . and ..
const normalizedPath = normalize(subPath).replace(/^\/+|\/+$/g, "");
// Construct the full docs directory path
const docsDir = join(baseDocsDir, normalizedPath);
// Create output filename based on the last part of the path
const outputName = normalizedPath.split("/").pop() || normalizedPath;
const outputFile = `./${outputName}.txt`;
try {
await combineMarkdownFiles(docsDir, outputFile);
} catch (error) {
console.error("Error processing files:", error);
Deno.exit(1);
}
}