-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartials.ts
90 lines (79 loc) · 2.3 KB
/
partials.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
import { join, dirname } from "https://deno.land/std/path/mod.ts";
interface PartialImport {
importName: string;
partialPath: string;
}
/**
* Extracts partial imports from MDX content
* Example: import InstallTheCli from "@site/docs/_install-the-cli.mdx";
*/
function extractPartialImports(content: string): PartialImport[] {
const importRegex = /import\s+(\w+)\s+from\s+"@site\/([^"]+)"/g;
const partials: PartialImport[] = [];
let match;
while ((match = importRegex.exec(content)) !== null) {
const [_, importName, partialPath] = match;
if (partialPath.includes("/_")) {
partials.push({ importName, partialPath });
}
}
return partials;
}
/**
* Reads the content of a partial MDX file
*/
async function readPartialContent(
baseDir: string,
partialPath: string
): Promise<string> {
try {
// Remove @site prefix and normalize path
// Since @site maps to the root directory, we need to go up one level from the docs directory
const normalizedPath = partialPath.replace("@site/", "");
const fullPath = join(dirname(baseDir), normalizedPath);
const content = await Deno.readTextFile(fullPath);
// Remove frontmatter if present
const contentWithoutFrontmatter = content.replace(
/^---\n[\s\S]*?\n---\n/,
""
);
return contentWithoutFrontmatter.trim();
} catch (error) {
console.error(`Error reading partial ${partialPath}:`, error);
return "";
}
}
/**
* Replaces partial component usage with actual content
* Example: <InstallTheCli /> -> content of _install-the-cli.mdx
*/
function replacePartialUsage(
content: string,
importName: string,
partialContent: string
): string {
const componentRegex = new RegExp(`<${importName}\\s*/>`, "g");
return content.replace(componentRegex, partialContent);
}
/**
* Main function to process partials in MDX content
*/
export async function processPartials(
content: string,
baseDir: string
): Promise<string> {
const partials = extractPartialImports(content);
let processedContent = content;
for (const partial of partials) {
const partialContent = await readPartialContent(
baseDir,
partial.partialPath
);
processedContent = replacePartialUsage(
processedContent,
partial.importName,
partialContent
);
}
return processedContent;
}