-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-headings.ts
159 lines (137 loc) · 4.51 KB
/
format-headings.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
import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
// Abbreviations configuration - ADD NEW ABBREVIATIONS HERE
const ABBREVIATIONS = [
"CLI",
"EVM",
"AI",
"MCP",
"IDE",
"EAS",
"LLM",
"HSM",
"API",
"SDK",
"URL",
"JSON",
"GraphQL",
"BFSI",
"dApp",
// Add more abbreviations here in UPPERCASE
// Example:
// 'API',
// 'SDK',
// 'URL',
];
// Create a regex pattern for abbreviations that matches:
// 1. When they're in parentheses: (CLI)
// 2. When they're standalone with spaces: " CLI "
// 3. When they're at start of line with space after: "CLI "
// 4. When they're at end of line with space before: " CLI"
const createAbbreviationPattern = (abbr: string) => {
return new RegExp(
`(?:\\(${abbr}\\))|(?:^${abbr}\\s)|(?:\\s${abbr}\\s)|(?:\\s${abbr}$)`,
"gi"
);
};
function preserveAbbreviations(text: string): {
text: string;
replacements: Map<string, string>;
} {
const replacements = new Map<string, string>();
let processedText = text;
ABBREVIATIONS.forEach((abbr) => {
const pattern = createAbbreviationPattern(abbr);
processedText = processedText.replace(pattern, (match) => {
const placeholder = `__ABBR_${Math.random().toString(36).substr(2, 9)}__`;
replacements.set(placeholder, match.replace(abbr, abbr.toUpperCase()));
return placeholder;
});
});
return { text: processedText, replacements };
}
function restoreAbbreviations(
text: string,
replacements: Map<string, string>
): string {
let restoredText = text;
replacements.forEach((value, placeholder) => {
restoredText = restoredText.replace(placeholder, value);
});
return restoredText;
}
// Function to convert text to sentence case
function toSentenceCase(text: string): string {
// Remove any remaining asterisks
let cleanText = text.replace(/\*\*/g, "").trim();
// Preserve abbreviations
const { text: processedText, replacements } =
preserveAbbreviations(cleanText);
// Convert to sentence case
let sentenceCaseText =
processedText.charAt(0).toUpperCase() +
processedText.slice(1).toLowerCase();
// Restore abbreviations
return restoreAbbreviations(sentenceCaseText, replacements);
}
// Function to process a single line
function processLine(line: string): string {
// First, check if it's a heading line
if (!line.startsWith("#")) {
return line;
}
// Updated regex to handle all cases including spaced asterisks
const headingRegex =
/^(#{1,6}\s*\*{0,2}\s*(?:\d+(?:\.\d+)?\.?\s*)?)([a-zA-Z])(.*?)$/;
const match = line.match(headingRegex);
if (match) {
const [_, prefix, firstLetter, rest] = match;
// Only capitalize the first letter, keep everything else exactly as is
return `${prefix}${firstLetter.toUpperCase()}${rest}`;
}
return line;
}
// Function to process a single file
async function processFile(filePath: string): Promise<void> {
try {
const content = await readFile(filePath, "utf-8");
const lines = content.split("\n");
const processedLines = lines.map(processLine);
await writeFile(filePath, processedLines.join("\n"));
console.log(`✅ Processed: ${filePath}`);
} catch (error) {
console.error(`❌ Error processing ${filePath}:`, error);
}
}
// Function to recursively process all markdown files in a directory
async function processDirectory(dirPath: string, level = 0): Promise<void> {
try {
console.log(`📂 Scanning directory: ${dirPath}`);
const entries = await readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dirPath, entry.name);
if (entry.isDirectory()) {
console.log(`📁 Entering subdirectory: ${entry.name}`);
await processDirectory(fullPath, level + 1);
} else if (
entry.isFile() &&
(entry.name.endsWith(".md") || entry.name.endsWith(".mdx")) &&
!entry.name.endsWith("Meta.json")
) {
console.log(`📄 Found markdown file: ${entry.name}`);
await processFile(fullPath);
} else {
console.log(`⏭️ Skipping non-markdown file: ${entry.name}`);
}
}
} catch (error) {
console.error(`❌ Error processing directory ${dirPath}:`, error);
}
}
// Main execution
const docsPath = join(process.cwd(), "content", "docs");
console.log("🚀 Starting to process markdown files...");
console.log(`📍 Starting from: ${docsPath}\n`);
processDirectory(docsPath)
.then(() => console.log("\n✨ Finished processing all files"))
.catch((error) => console.error("❌ Error:", error));