Skip to content

Commit 6428f31

Browse files
committed
chore: add AI to mdx-to-md converter
1 parent c6a666a commit 6428f31

4 files changed

Lines changed: 209 additions & 31 deletions

File tree

mdx-to-md-converter/package-lock.json

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mdx-to-md-converter/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
"nodemon": "^3.1.10",
2121
"ts-node": "^10.9.2",
2222
"typescript": "^5.9.2"
23+
},
24+
"dependencies": {
25+
"@ai-sdk/openai-compatible": "^2.0.28",
26+
"ai": "^6.0.78"
2327
}
2428
}

mdx-to-md-converter/src/index.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { convertSection } from './modules/convertSection';
1717
import { convertStep } from './modules/convertStep';
1818
import { removeHrAndLayout } from './modules/removeHrAndLayout';
1919
import { removeIndentations } from './modules/removeIndentations';
20+
import { overviewByAI } from './modules/overviewByAI';
2021

2122
import fs from 'fs';
2223
import path from 'path';
@@ -103,37 +104,45 @@ function getRelativePath(filePath: string, basePath: string): string {
103104
return path.relative(basePath, filePath).replace(/\\/g, '/');
104105
}
105106

106-
try {
107-
const srcPagesPath = path.join(process.cwd(), '..', 'src', 'pages');
108-
const outputDir = path.join(process.cwd(), '..', 'public', 'llms');
109-
const allLinksPath = path.join(process.cwd(), '..', 'public', 'all-links-llms.txt');
110-
111-
console.log(`Searching for MDX files in: ${srcPagesPath}`);
112-
113-
const mdxFiles = findMdxFiles(srcPagesPath);
114-
115-
if (mdxFiles.length === 0) {
116-
console.log('No MDX files found in src/pages');
117-
process.exit(1);
118-
}
119-
120-
console.log(`Found ${mdxFiles.length} MDX files`);
121-
122-
if (!fs.existsSync(outputDir)) {
123-
fs.mkdirSync(outputDir, { recursive: true });
124-
}
125-
126-
// Array to store all the generated links
127-
const allLinks: string[] = [];
128-
129-
for (const filePath of mdxFiles) {
107+
async function main() {
108+
try {
109+
const srcPagesPath = path.join(process.cwd(), '..', 'src', 'pages');
110+
const outputDir = path.join(process.cwd(), '..', 'public', 'llms');
111+
const allLinksPath = path.join(process.cwd(), '..', 'public', 'all-links-llms.txt');
112+
113+
console.log(`Searching for MDX files in: ${srcPagesPath}`);
114+
115+
const mdxFiles = findMdxFiles(srcPagesPath);
116+
117+
if (mdxFiles.length === 0) {
118+
console.log('No MDX files found in src/pages');
119+
process.exit(1);
120+
}
121+
122+
console.log(`Found ${mdxFiles.length} MDX files`);
123+
124+
if (!fs.existsSync(outputDir)) {
125+
fs.mkdirSync(outputDir, { recursive: true });
126+
}
127+
128+
// Array to store all the generated links
129+
const allLinks: string[] = [];
130+
131+
for (const filePath of mdxFiles) {
130132
try {
131133
console.log(`Processing: ${filePath}`);
132134

133135
const mdxContent = readMdxFile(filePath);
134136

135137
const mdContent = convertMdxToMd(mdxContent);
136138

139+
// Extra step: Store the final MD output in informal_md variable
140+
const informal_md = mdContent;
141+
142+
// Process with AI to get overview
143+
console.log(`Processing with AI: ${filePath}`);
144+
const aiProcessedContent = await overviewByAI(informal_md);
145+
137146
const relativePath = getRelativePath(filePath, srcPagesPath);
138147
const mdFileName = relativePath.replace(/\.mdx$/, '.md');
139148
const outputFilePath = path.join(outputDir, mdFileName);
@@ -157,7 +166,7 @@ try {
157166
// Add the "all links" section to the end of each MD file
158167
const finalMdContent =
159168
originalHeader +
160-
mdContent +
169+
aiProcessedContent +
161170
'\n\n## all links\n\n[All links of docs](https://docs.liara.ir/all-links-llms.txt)\n';
162171

163172
// Write file with explicit UTF-8 encoding and BOM
@@ -189,10 +198,14 @@ try {
189198
fs.writeFileSync(allLinksPath, '\ufeff' + allLinksContent, { encoding: 'utf8' });
190199
console.log(`✅ All links saved to: ${allLinksPath}`);
191200

192-
console.log(`All files converted and saved to: ${outputDir}`);
193-
console.log(`Total files processed: ${mdxFiles.length}`);
194-
195-
} catch (err) {
196-
console.error('Error:', err);
197-
process.exit(1);
201+
console.log(`All files converted and saved to: ${outputDir}`);
202+
console.log(`Total files processed: ${mdxFiles.length}`);
203+
204+
} catch (err) {
205+
console.error('Error:', err);
206+
process.exit(1);
207+
}
198208
}
209+
210+
// Run the main function
211+
main();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// npm i @ai-sdk/openai-compatible
2+
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
3+
import { generateText } from 'ai';
4+
5+
export async function overviewByAI(informal_md: string): Promise<string> {
6+
try {
7+
const baseURL = process.env.MY_BASE_URL;
8+
const apiKey = process.env.MY_API_KEY;
9+
10+
if (!baseURL || !apiKey) {
11+
throw new Error('MY_BASE_URL and MY_API_KEY environment variables must be set');
12+
}
13+
14+
const { text } = await generateText({
15+
model: createOpenAICompatible({
16+
name: 'liara-ai',
17+
baseURL,
18+
apiKey,
19+
}).chatModel("google/gemini-2.0-flash-001"),
20+
prompt: `convert the text below to the pure markdown file, do not change any content on it:\n\n${informal_md}`,
21+
});
22+
23+
return text;
24+
} catch (error) {
25+
console.error('Error in overviewByAI:', error);
26+
throw error;
27+
}
28+
}

0 commit comments

Comments
 (0)