forked from pendle-finance/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-gitbook.ts
More file actions
669 lines (529 loc) · 21.9 KB
/
migrate-gitbook.ts
File metadata and controls
669 lines (529 loc) · 21.9 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#!/usr/bin/env ts-node
const fs = require('fs');
const path = require('path');
interface CliOptions {
project?: string;
help?: boolean;
}
interface SidebarItem {
type: 'doc' | 'category' | 'link';
id?: string;
label?: string;
items?: SidebarItem[];
href?: string;
}
class GitBookMigrator {
private gitbookDir = './gitbook-docs';
private docsDir = './docs';
private staticDir = './static';
async migrateAllProjects(specificProject?: string): Promise<void> {
console.log('🚀 Starting GitBook to Docusaurus migration...');
const allProjects = fs.readdirSync(this.gitbookDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
let projects: string[];
if (specificProject) {
if (!allProjects.includes(specificProject)) {
throw new Error(`❌ Project "${specificProject}" not found. Available projects: ${allProjects.join(', ')}`);
}
projects = [specificProject];
console.log(`📂 Migrating specific project: ${specificProject}`);
} else {
projects = allProjects;
console.log(`📂 Found projects: ${projects.join(', ')}`);
}
for (const project of projects) {
console.log(`\n🔄 Processing project: ${project}`);
await this.processProject(project);
}
console.log('\n✅ Migration completed successfully!');
}
private async processProject(projectName: string): Promise<void> {
const projectPath = path.join(this.gitbookDir, projectName);
const targetDocsPath = path.join(this.docsDir, projectName);
const targetStaticPath = path.join(this.staticDir, projectName, 'imgs');
// Create target directories
this.ensureDirectoryExists(targetDocsPath);
this.ensureDirectoryExists(targetStaticPath);
console.log(` 📝 Converting README.md to Introduction.md`);
await this.convertReadmeToIntroduction(projectPath, targetDocsPath);
console.log(` 🗂️ Generating sidebars.js from SUMMARY.md`);
await this.generateSidebarsFromSummary(projectPath, targetDocsPath);
console.log(` 📸 Copying assets to static folder`);
await this.copyAssets(projectPath, targetStaticPath);
console.log(` 📄 Copying and updating documentation files`);
await this.copyAndUpdateDocs(projectPath, targetDocsPath, projectName);
console.log(` ✅ Project ${projectName} migration completed`);
}
private async convertReadmeToIntroduction(projectPath: string, targetDocsPath: string): Promise<void> {
const readmePath = path.join(projectPath, 'README.md');
const introductionPath = path.join(targetDocsPath, 'Introduction.md');
if (!fs.existsSync(readmePath)) {
console.log(` ⚠️ README.md not found in ${projectPath}`);
return;
}
let content = fs.readFileSync(readmePath, 'utf-8');
// Format the document using the comprehensive formatter
content = this.formatDocFile(content, projectPath.split('/').pop()!);
fs.writeFileSync(introductionPath, content, 'utf-8');
}
private async generateSidebarsFromSummary(projectPath: string, targetDocsPath: string): Promise<void> {
const summaryPath = path.join(projectPath, 'SUMMARY.md');
const sidebarPath = path.join(targetDocsPath, 'sidebars.js');
if (!fs.existsSync(summaryPath)) {
console.log(` ⚠️ SUMMARY.md not found in ${projectPath}`);
return;
}
const summaryContent = fs.readFileSync(summaryPath, 'utf-8');
const sidebarItems = this.parseSummaryToSidebar(summaryContent);
const sidebarConfig = `module.exports = {
myAutogeneratedSidebar: [
{
type: "doc",
id: "Introduction",
label: "📖 Introduction",
},
${this.generateSidebarItemsString(sidebarItems, ' ')}
],
};
`;
fs.writeFileSync(sidebarPath, sidebarConfig, 'utf-8');
}
private parseSummaryToSidebar(summaryContent: string): SidebarItem[] {
const lines = summaryContent.split('\n');
const items: SidebarItem[] = [];
let currentCategory: SidebarItem | null = null;
const stack: SidebarItem[] = []; // Stack to track nested categories
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines and main title header (single #)
if (!trimmedLine || (trimmedLine.startsWith('#') && !trimmedLine.startsWith('##'))) continue;
// Check if this is a section header (##)
if (trimmedLine.startsWith('##')) {
const categoryLabel = trimmedLine.replace(/^##\s*/, '').trim();
currentCategory = {
type: 'category',
label: categoryLabel,
items: []
};
items.push(currentCategory);
stack.length = 0; // Clear stack for new top-level category
continue;
}
// Check if this is a doc item (* [Title](path))
const docMatch = line.match(/^(\s*)\*\s*\[([^\]]+)\]\(([^)]+)\)$/);
if (docMatch) {
const [, indentation, title, filePath] = docMatch;
const indentLevel = indentation.length;
// Skip README.md as it's converted to Introduction.md
if (filePath === 'README.md') continue;
const docId = this.filePathToDocId(filePath);
// Determine if this is a nested item (indented)
if (indentLevel > 0 && stack.length > 0) {
// This is a nested item, add to the last doc item in stack as a category
const parentItem = stack[stack.length - 1];
// Convert parent to category if it's not already
if (parentItem.type === 'doc') {
const parentDocId = parentItem.id;
const parentLabel = parentItem.label;
// Convert to category and add the parent doc as the first item
parentItem.type = 'category';
parentItem.items = [
{
type: 'doc',
id: parentDocId,
label: parentLabel
}
];
delete parentItem.id; // Remove id when converting to category
}
const nestedDocItem: SidebarItem = {
type: 'doc',
id: docId,
label: title
};
if (parentItem.items) {
parentItem.items.push(nestedDocItem);
}
} else {
// This is a top-level item
const docItem: SidebarItem = {
type: 'doc',
id: docId,
label: title
};
if (currentCategory && currentCategory.items) {
currentCategory.items.push(docItem);
stack.push(docItem); // Add to stack for potential nesting
} else {
items.push(docItem);
stack.push(docItem); // Add to stack for potential nesting
}
}
}
}
return items;
}
private escapeQuotesInLabel(label: string): string {
// Escape double quotes in labels to prevent syntax errors
return label.replace(/"/g, '\\"');
}
private generateSidebarItemsString(items: SidebarItem[], indent: string): string {
return items.map(item => {
if (item.type === 'category') {
const nestedItems = item.items ? this.generateSidebarItemsString(item.items, indent + ' ') : '';
const escapedLabel = this.escapeQuotesInLabel(item.label || '');
return `${indent}{
${indent} type: "category",
${indent} label: "${escapedLabel}",
${indent} items: [
${nestedItems}
${indent} ],
${indent}},`;
} else if (item.type === 'doc') {
const escapedLabel = this.escapeQuotesInLabel(item.label || '');
return `${indent}{ type: "doc", id: "${item.id}", label: "${escapedLabel}" },`;
}
return '';
}).join('\n');
}
private async copyAssets(projectPath: string, targetStaticPath: string): Promise<void> {
const assetsPath = path.join(projectPath, '.gitbook', 'assets');
if (!fs.existsSync(assetsPath)) {
console.log(` ⚠️ Assets folder not found in ${projectPath}`);
return;
}
this.copyDirectory(assetsPath, targetStaticPath);
}
private async copyAndUpdateDocs(projectPath: string, targetDocsPath: string, projectName: string): Promise<void> {
const items = fs.readdirSync(projectPath, { withFileTypes: true });
for (const item of items) {
// Skip special GitBook files and directories
if (['README.md', 'SUMMARY.md', '.gitbook', '.git'].includes(item.name)) {
continue;
}
const sourcePath = path.join(projectPath, item.name);
const targetPath = path.join(targetDocsPath, item.name);
if (item.isDirectory()) {
this.ensureDirectoryExists(targetPath);
this.copyDocumentationDirectory(sourcePath, targetPath, projectName);
} else if (item.name.endsWith('.md')) {
this.copyAndUpdateMarkdownFile(sourcePath, targetPath, projectName);
}
}
}
private copyDocumentationDirectory(sourceDir: string, targetDir: string, projectName: string): void {
const items = fs.readdirSync(sourceDir, { withFileTypes: true });
for (const item of items) {
const sourcePath = path.join(sourceDir, item.name);
const targetPath = path.join(targetDir, item.name);
if (item.isDirectory()) {
this.ensureDirectoryExists(targetPath);
this.copyDocumentationDirectory(sourcePath, targetPath, projectName);
} else if (item.name.endsWith('.md')) {
this.copyAndUpdateMarkdownFile(sourcePath, targetPath, projectName);
}
}
}
private copyAndUpdateMarkdownFile(sourcePath: string, targetPath: string, projectName: string): void {
let content = fs.readFileSync(sourcePath, 'utf-8');
// Format the document using the comprehensive formatter
content = this.formatDocFile(content, projectName);
fs.writeFileSync(targetPath, content, 'utf-8');
}
private removeAllFrontmatter(content: string): string {
// Remove any frontmatter (GitBook or existing Docusaurus)
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n\n?/);
if (frontmatterMatch) {
return content.substring(frontmatterMatch[0].length);
}
return content;
}
private convertGitBookTables(content: string): string {
// Convert GitBook table syntax to CardGrid components
// Look for the specific comment pattern followed by table HTML
const tablePattern = /<!-- Table converted from GitBook format -->\s*\n<table[\s\S]*?<\/table>/gi;
content = content.replace(tablePattern, (match) => {
return this.parseGitBookTableToCardGrid(match);
});
// Also handle tables without the comment (in case some are missed)
const directTablePattern = /<table[^>]*data-view="cards"[\s\S]*?<\/table>/gi;
content = content.replace(directTablePattern, (match) => {
// Only convert if it's not already processed (doesn't have the comment)
if (!match.includes('<!-- Table converted from GitBook format -->')) {
return this.parseGitBookTableToCardGrid(match);
}
return match;
});
return content;
}
private parseGitBookTableToCardGrid(tableHtml: string): string {
// Parse GitBook table and convert to CardGrid component
// First, clean the input by removing the comment if present
const cleanTableHtml = tableHtml.replace(/<!-- Table converted from GitBook format -->\s*\n?/gi, '');
const tableType = this.detectTableType(cleanTableHtml);
// Extract table rows
const rowMatches = cleanTableHtml.match(/<tr[\s\S]*?<\/tr>/gi);
if (!rowMatches || rowMatches.length < 2) {
console.log(` ⚠️ Could not parse table rows, returning original`);
return tableHtml; // Return original if can't parse
}
// Skip header row, process data rows
const dataRows = rowMatches.slice(1);
const cards: string[] = [];
for (const row of dataRows) {
const card = this.parseTableRowToCard(row, tableType);
if (card) {
cards.push(card);
}
}
if (cards.length === 0) {
console.log(` ⚠️ No cards extracted from table, returning original`);
return tableHtml; // Return original if no cards extracted
}
const gridType = this.getGridTypeFromTableType(tableType);
console.log(` ✅ Converted table to CardGrid with ${cards.length} cards (type: ${gridType})`);
return `<CardGrid type="${gridType}">
${cards.join('\n')}
</CardGrid>`;
}
private detectTableType(tableHtml: string): 'selfService' | 'default' {
// All GitBook tables will be processed as self-service cards
if (tableHtml.includes('data-view="cards"') || tableHtml.includes('<table')) {
return 'selfService';
}
return 'default';
}
private getGridTypeFromTableType(tableType: string): string {
switch (tableType) {
case 'selfService': return 'selfService';
default: return 'default';
}
}
private parseTableRowToCard(rowHtml: string, tableType: string): string | null {
// Extract cell contents
const cellMatches = rowHtml.match(/<td[\s\S]*?<\/td>/gi);
if (!cellMatches) return null;
let title = '';
let link = '';
// Parse cells to extract content
for (const cell of cellMatches) {
const cellContent = cell.replace(/<\/?td[^>]*>/gi, '').trim();
if (!cellContent || cellContent === '') continue;
// Extract links
const linkMatch = cellContent.match(/<a[^>]+href="([^"]+)"[^>]*>/);
if (linkMatch) {
link = linkMatch[1];
}
// Extract strong text (titles)
const strongMatch = cellContent.match(/<strong[^>]*>(.*?)<\/strong>/);
if (strongMatch) {
title = strongMatch[1].trim();
}
}
if (!title) return null;
// All GitBook tables use the same self-service card format
return this.generateSelfServiceCard(title, link);
}
private generateSelfServiceCard(title: string, link: string): string {
const linkProp = link ? ` link="${link}"` : '';
return ` <Card
title="${this.escapeQuotesInLabel(title)}"${linkProp}
/>`;
}
private convertGitBookHints(content: string): string {
// Convert GitBook hint blocks to custom Hint component
// Pattern: {% hint style="info" %} ... {% endhint %}
const hintPattern = /{% hint style="(info|warning|danger|success)" %}\n([\s\S]*?)\n{% endhint %}/g;
return content.replace(hintPattern, (match, style, hintContent) => {
// Clean up the hint content by trimming whitespace
const cleanContent = hintContent.trim();
// Convert to JSX Hint component with import
return `<Hint style="${style}">
${cleanContent}
</Hint>`;
});
}
private addComponentImports(content: string): string {
const imports: string[] = [];
// Check if the content contains any Hint components
if (content.includes('<Hint')) {
imports.push("import Hint from '@site/src/components/Hint';");
}
// Check if the content contains any CardGrid components
if (content.includes('<CardGrid') || content.includes('<Card')) {
imports.push("import CardGrid, { Card } from '@site/src/components/CardGrid';");
}
if (imports.length > 0) {
return imports.join('\n') + '\n\n' + content;
}
return content;
}
private fixUnclosedImgTags(content: string): string {
// Fix unclosed img tags by converting <img ...> to <img ... />
// This handles both single-line and multi-line img tags
return content.replace(/<img([^>]*?)(?<!\/)>/g, '<img$1 />');
}
private convertGitBookEmbeds(content: string): string {
// Convert GitBook embed blocks to iframe elements
// Pattern: {% embed url="https://www.youtube.com/watch?v=VIDEO_ID" %}
const embedPattern = /{% embed url="([^"]+)" %}/g;
return content.replace(embedPattern, (match, url) => {
// Check if it's a YouTube URL and convert to embed format
const youtubeMatch = url.match(/(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)/);
if (youtubeMatch) {
const videoId = youtubeMatch[1];
const embedUrl = `https://www.youtube.com/embed/${videoId}`;
return `<iframe height="400" width="100%" src="${embedUrl}" title="Video" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>`;
}
// For other URLs, create a generic iframe
return `<iframe height="400" width="100%" src="${url}" title="Embedded Content" frameBorder="0" allowFullScreen></iframe>`;
});
}
private disableSingleDollarLatex(content: string): string {
// Escape single dollar signs to prevent LaTeX rendering
// This is specific to GitBook projects that don't support single dollar LaTeX syntax
// Pattern: $ (single dollar not followed by another dollar)
// We need to be careful not to escape double dollars ($$)
return content.replace(/(?<!\$)\$(?!\$)/g, '\\$');
}
private removeMarkdownExtensions(content: string): string {
// Remove .md extensions from all internal links to follow Docusaurus link format
// Pattern: href="something.md" or [text](something.md) or <a href="something.md">
// Handle markdown links: [text](file.md) -> [text](file)
content = content.replace(/\[([^\]]*)\]\(([^)]+)\.md(#[^)]*)?(\)|#[^)]*\))/g, (match, text, link, anchor, closeParen) => {
const finalAnchor = anchor || '';
return `[${text}](${link}${finalAnchor})`;
});
// Handle HTML href attributes: href="file.md" -> href="file"
content = content.replace(/href="([^"]+)\.md(#[^"]*)?"/g, (match, link, anchor) => {
const finalAnchor = anchor || '';
return `href="${link}${finalAnchor}"`;
});
// Handle card links: link="file.md#anchor" -> link="file#anchor"
content = content.replace(/link="([^"]+)\.md(#[^"]*)?"/g, (match, link, anchor) => {
const finalAnchor = anchor || '';
return `link="${link}${finalAnchor}"`;
});
return content;
}
private stripInvalidEntities(content: string): string {
// Strip down all "&#xNAN;" invalid HTML entities from the content
return content.replace(/&#xNAN;/g, '');
}
private formatDocFile(content: string, projectName: string): string {
// Remove all existing frontmatter
content = this.removeAllFrontmatter(content);
// Convert GitBook table syntax
content = this.convertGitBookTables(content);
// Convert GitBook hints to custom Hint components
content = this.convertGitBookHints(content);
// Convert GitBook embeds to iframe elements
content = this.convertGitBookEmbeds(content);
// Disable single dollar LaTeX syntax for GitBook projects
content = this.disableSingleDollarLatex(content);
// Remove .md extensions from internal links
content = this.removeMarkdownExtensions(content);
// Strip invalid HTML entities
content = this.stripInvalidEntities(content);
// Fix unclosed img tags
content = this.fixUnclosedImgTags(content);
// Update image paths
content = this.updateImagePathsInContent(content, projectName);
// Add component imports if needed
content = this.addComponentImports(content);
return content;
}
private updateImagePathsInContent(content: string, projectName: string): string {
// Replace GitBook image paths with Docusaurus static paths
// Pattern: ../.gitbook/assets/filename.ext -> /projectName/imgs/filename.ext
return content.replace(/\.\.\/\.gitbook\/assets\//g, `/${projectName}/imgs/`);
}
private filePathToDocId(filePath: string): string {
// Convert file path to Docusaurus doc ID
// Remove .md extension and replace path separators with slashes
return filePath.replace(/\.md$/, '').replace(/\\/g, '/');
}
private generateTitleFromFileName(fileName: string): string {
// Convert filename to a readable title
return fileName
.replace(/-/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase());
}
private ensureDirectoryExists(dirPath: string): void {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
private copyDirectory(sourceDir: string, targetDir: string): void {
this.ensureDirectoryExists(targetDir);
const items = fs.readdirSync(sourceDir, { withFileTypes: true });
for (const item of items) {
const sourcePath = path.join(sourceDir, item.name);
const targetPath = path.join(targetDir, item.name);
if (item.isDirectory()) {
this.copyDirectory(sourcePath, targetPath);
} else {
fs.copyFileSync(sourcePath, targetPath);
}
}
}
}
// Command line argument parsing
function parseArgs(args: string[]): CliOptions {
const options: CliOptions = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--help' || arg === '-h') {
options.help = true;
} else if (arg === '--project' || arg === '-p') {
if (i + 1 >= args.length) {
throw new Error('❌ --project/-p option requires a project name');
}
options.project = args[i + 1];
i++; // Skip the next argument as it's the project name
}
}
return options;
}
function showHelp(): void {
console.log(`
🚀 GitBook to Docusaurus Migration Tool
Usage:
yarn migrate-gitbook [options]
Options:
-p, --project <name> Migrate only the specified project
-h, --help Show this help message
Examples:
yarn migrate-gitbook # Migrate all projects
yarn migrate-gitbook -p boros-academy # Migrate only boros-academy
yarn migrate-gitbook --project pendle-academy # Migrate only pendle-academy
Available projects will be listed if no specific project is provided.
`);
}
// Main execution
async function main(): Promise<void> {
const migrator = new GitBookMigrator();
try {
// Parse command line arguments (skip 'node' and script name)
const args = process.argv.slice(2);
const options = parseArgs(args);
if (options.help) {
showHelp();
return;
}
await migrator.migrateAllProjects(options.project);
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
} else {
console.error('❌ Migration failed:', error);
}
process.exit(1);
}
}
// Run the migration if this file is executed directly
if (require.main === module) {
main();
}
module.exports = { GitBookMigrator };