Skip to content

Commit 72942dd

Browse files
Copilotnixel2007
andcommitted
Remove redundant single-page generation code and clean up file structure
Co-authored-by: nixel2007 <1132840+nixel2007@users.noreply.github.com>
1 parent 981ae4e commit 72942dd

File tree

3 files changed

+10
-220
lines changed

3 files changed

+10
-220
lines changed

.vitepress/config.mts

Lines changed: 5 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { glob } from 'glob';
33
import matter from 'gray-matter';
44
import fs from 'fs';
55
import path from 'path';
6-
import { createSinglePageDocumentation, generateAllToggleSinglePages } from '../generate-single-page.ts';
6+
import { createSinglePageToggleDocumentation, generateAllToggleSinglePages } from '../generate-single-page.ts';
77

88
const contentRoot = 'docs/';
99

@@ -17,15 +17,11 @@ export default defineConfig({
1717
},
1818

1919
buildStart(siteConfig) {
20-
// Generate single-page documentation for all products and APIs during dev if missing
21-
generateAllSinglePages();
22-
// Generate toggle mode single-page documentation
20+
// Generate toggle mode single-page documentation during dev if missing
2321
generateAllToggleSinglePages();
2422
},
2523

2624
buildEnd(siteConfig) {
27-
// Generate single-page documentation for all products and APIs
28-
generateAllSinglePages();
2925
// Generate toggle mode single-page documentation
3026
generateAllToggleSinglePages();
3127

@@ -402,42 +398,7 @@ function getNavBarItems(contentDir: string, appendNavBarWithContentDir: boolean
402398

403399
}
404400

405-
function getDynamicSinglePageNavItems(sectionType: 'products' | 'api'): DefaultTheme.NavItemWithLink[] {
406-
const navBarItems: DefaultTheme.NavItemWithLink[] = [];
407-
const cwd = `${process.cwd()}/${contentRoot}`;
408-
const contentDir = `${sectionType}/`;
409-
410-
const dirs = glob.sync(`${contentDir}/*/`, { cwd }).sort();
411401

412-
for (const dirIndex in dirs) {
413-
const dir = dirs[dirIndex];
414-
const productName = path.basename(dir);
415-
const cleanName = productName.replace(/^\d+-/, '');
416-
const displayName = getPageName(productName, false);
417-
418-
// Skip if no content exists
419-
const productPath = path.join(cwd, dir);
420-
if (!fs.existsSync(productPath)) continue;
421-
422-
// Check if there are any markdown files in the product directory
423-
const hasContent = hasMarkdownContent(productPath);
424-
if (!hasContent) continue;
425-
426-
const text = `${displayName} (одна страница)`;
427-
428-
// New URL structure: /product-name/single-page or /api/product-name/single-page
429-
let link: string;
430-
if (sectionType === 'api') {
431-
link = `/api/${cleanName}/single-page`;
432-
} else {
433-
link = `/${cleanName}/single-page`;
434-
}
435-
436-
navBarItems.push({ text, link });
437-
}
438-
439-
return navBarItems;
440-
}
441402

442403
function hasMarkdownContent(directory: string): boolean {
443404
if (!fs.existsSync(directory)) return false;
@@ -463,38 +424,7 @@ function hasMarkdownContent(directory: string): boolean {
463424
return hasSubdirContent;
464425
}
465426

466-
function generateAllSinglePages(): void {
467-
console.log('Generating single-page documentation for all products and APIs...');
468-
469-
const sections: ('products' | 'api')[] = ['products', 'api'];
470-
471-
for (const sectionType of sections) {
472-
const sectionPath = `docs/${sectionType}`;
473-
474-
if (!fs.existsSync(sectionPath)) continue;
475-
476-
const products = fs.readdirSync(sectionPath)
477-
.filter(item => {
478-
const itemPath = path.join(sectionPath, item);
479-
return fs.lstatSync(itemPath).isDirectory() || fs.lstatSync(itemPath).isSymbolicLink();
480-
});
481-
482-
for (const productName of products) {
483-
const productPath = path.join(sectionPath, productName);
484-
485-
// Check if product has markdown content
486-
if (hasMarkdownContent(productPath)) {
487-
try {
488-
createSinglePageDocumentation(sectionType, productName);
489-
} catch (error) {
490-
console.warn(`Failed to generate single-page for ${sectionType}/${productName}: ${error.message}`);
491-
}
492-
}
493-
}
494-
}
495-
496-
console.log('Single-page documentation generation complete.');
497-
}
427+
498428

499429
function getProductDisplayName(productName: string): string {
500430
// Remove number prefix and capitalize
@@ -514,42 +444,7 @@ function getProductDisplayName(productName: string): string {
514444
return displayNames[cleanName] || cleanName;
515445
}
516446

517-
function getSinglePageSidebars(): DefaultTheme.SidebarMulti {
518-
const sidebars: DefaultTheme.SidebarMulti = {};
519-
const cwd = `${process.cwd()}/${contentRoot}`;
520-
521-
// Generate sidebars for products
522-
const productDirs = glob.sync(`products/*/`, { cwd }).sort();
523-
for (const dirIndex in productDirs) {
524-
const dir = productDirs[dirIndex].replaceAll('\\', '/');
525-
const productName = path.basename(dir);
526-
const cleanName = productName.replace(/^\d+-/, '');
527-
528-
// Check if product has content
529-
const productPath = path.join(cwd, dir);
530-
if (!hasMarkdownContent(productPath)) continue;
531-
532-
const singlePageRoute = `/${cleanName}/single-page`;
533-
sidebars[singlePageRoute] = generateSinglePageSidebar('products', productName, singlePageRoute);
534-
}
535-
536-
// Generate sidebars for APIs
537-
const apiDirs = glob.sync(`api/*/`, { cwd }).sort();
538-
for (const dirIndex in apiDirs) {
539-
const dir = apiDirs[dirIndex].replaceAll('\\', '/');
540-
const productName = path.basename(dir);
541-
const cleanName = productName.replace(/^\d+-/, '');
542-
543-
// Check if API has content
544-
const apiPath = path.join(cwd, dir);
545-
if (!hasMarkdownContent(apiPath)) continue;
546-
547-
const singlePageRoute = `/api/${cleanName}/single-page`;
548-
sidebars[singlePageRoute] = generateSinglePageSidebar('api', productName, singlePageRoute);
549-
}
550-
551-
return sidebars;
552-
}
447+
553448

554449
function getToggleModeNavItems(): { products: DefaultTheme.NavItemWithLink[], api: DefaultTheme.NavItemWithLink[] } {
555450
const cwd = `${process.cwd()}/${contentRoot}`;
@@ -827,7 +722,6 @@ function generateAllSidebarConfigurations(): DefaultTheme.SidebarMulti {
827722

828723
// Get all individual sidebar configurations
829724
const toggleSidebars = getToggleModeSidebars();
830-
const singlePageSidebars = getSinglePageSidebars();
831725
const productSidebars = getSidebars('products/', false, 'autumn');
832726
const apiSidebars = getSidebars('api/');
833727

@@ -839,12 +733,7 @@ function generateAllSidebarConfigurations(): DefaultTheme.SidebarMulti {
839733
sidebarEntries.push([route, sidebar]);
840734
}
841735

842-
// 2. Regular single-page routes (very specific)
843-
for (const [route, sidebar] of Object.entries(singlePageSidebars)) {
844-
sidebarEntries.push([route, sidebar]);
845-
}
846-
847-
// 3. Specific longer product and API routes first (autumn-collections before autumn)
736+
// 2. Specific longer product and API routes first (autumn-collections before autumn)
848737
const allRoutes = [
849738
...Object.entries(productSidebars),
850739
...Object.entries(apiSidebars)

generate-missing-single-pages.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import { createSinglePageDocumentation, createSinglePageToggleDocumentation } from './generate-single-page.ts';
3+
import { createSinglePageToggleDocumentation } from './generate-single-page.ts';
44

55
function hasMarkdownContent(directory: string): boolean {
66
if (!fs.existsSync(directory)) return false;
@@ -63,34 +63,17 @@ function generateMissingSinglePages(): void {
6363
for (const productName of products) {
6464
const productPath = path.join(sectionPath, productName);
6565

66-
// Determine output paths for both original and toggle modes
67-
let outputPath: string;
66+
// Determine output path for toggle mode only
6867
let toggleOutputPath: string;
6968

7069
if (sectionType === 'api') {
71-
// API files go in the original structure
72-
outputPath = `${productPath}/single-page.md`;
73-
// Toggle mode files
7470
const displayName = getProductDisplayName(productName).toLowerCase().replace(/\s+/g, '-');
7571
toggleOutputPath = `docs/single-page/api/${displayName}.md`;
7672
} else {
77-
// Product files go in the clean name structure (bypass products/ rewrites)
7873
const displayName = getProductDisplayName(productName).toLowerCase().replace(/\s+/g, '-');
79-
outputPath = `docs/${displayName}/single-page.md`;
80-
// Toggle mode files
8174
toggleOutputPath = `docs/single-page/${displayName}.md`;
8275
}
8376

84-
// Generate original single-page if missing
85-
if (!fs.existsSync(outputPath) && hasMarkdownContent(productPath)) {
86-
try {
87-
createSinglePageDocumentation(sectionType, productName);
88-
console.log(`Generated missing single-page: ${outputPath}`);
89-
} catch (error) {
90-
console.warn(`Failed to generate single-page for ${sectionType}/${productName}: ${error.message}`);
91-
}
92-
}
93-
9477
// Generate toggle mode single-page if missing
9578
if (!fs.existsSync(toggleOutputPath) && hasMarkdownContent(productPath)) {
9679
try {

generate-single-page.ts

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -187,89 +187,7 @@ function generateTableOfContents(files: MarkdownFile[]): string {
187187
return toc + '\n';
188188
}
189189

190-
function createSinglePageDocumentation(sectionType: 'products' | 'api', productName: string): void {
191-
const basePath = `docs/${sectionType}/${productName}`;
192-
193-
if (!fs.existsSync(basePath)) {
194-
console.error(`Error: Directory ${basePath} does not exist`);
195-
return;
196-
}
197-
198-
// Get all markdown files recursively
199-
const allFiles = processAllFilesRecursively(basePath, '', sectionType);
200-
201-
if (allFiles.length === 0) {
202-
console.log(`No markdown files found in ${basePath}`);
203-
return;
204-
}
205-
206-
// Create frontmatter for single page
207-
let frontmatter: any;
208-
let pageTitle: string;
209-
210-
if (sectionType === 'api') {
211-
// For API pages, use a more specific title without duplication
212-
pageTitle = `API ${getProductDisplayName(productName)}`;
213-
frontmatter = {
214-
title: pageTitle,
215-
description: `Полный API справочник для ${getProductDisplayName(productName)}.`
216-
};
217-
} else {
218-
// For product pages, use the original format
219-
pageTitle = `${getProductDisplayName(productName)} - Полная документация`;
220-
frontmatter = {
221-
title: pageTitle,
222-
description: `Это единая страница с полной документацией по ${getProductDisplayName(productName)}.`
223-
};
224-
}
225-
226-
let singlePageContent = '---\n';
227-
singlePageContent += `title: "${frontmatter.title}"\n`;
228-
singlePageContent += `description: "${frontmatter.description}"\n`;
229-
singlePageContent += '---\n\n';
230-
231-
// Don't add extra headers for single-page documents
232-
233-
// Process and combine all files without TOC or extra section titles
234-
for (const file of allFiles) {
235-
// Adjust content
236-
let processedContent = adjustImagePaths(file.content, productName, sectionType);
237-
processedContent = adjustHeadingLevels(processedContent, 0); // Don't adjust heading levels
238-
239-
// Add the content directly without extra section titles
240-
singlePageContent += processedContent;
241-
singlePageContent += '\n\n---\n\n';
242-
}
243-
244-
// Remove the last separator
245-
singlePageContent = singlePageContent.replace(/\n\n---\n\n$/, '\n');
246-
247-
// Generate output filename using correct file structure
248-
// Files need to be placed in the source structure to work with VitePress rewrites
249-
let outputPath: string;
250-
251-
if (sectionType === 'api') {
252-
// For API: place in docs/api/000-product/single-page.md
253-
// This will be accessible at /api/product/single-page after rewrites
254-
outputPath = `${basePath}/single-page.md`;
255-
} else {
256-
// For products: place in docs/product-name/single-page.md (bypass products/ rewrites)
257-
// This will be accessible at /product-name/single-page
258-
const displayName = getProductDisplayName(productName).toLowerCase().replace(/\s+/g, '-');
259-
const productDir = `docs/${displayName}`;
260-
if (!fs.existsSync(productDir)) {
261-
fs.mkdirSync(productDir, { recursive: true });
262-
}
263-
outputPath = `${productDir}/single-page.md`;
264-
}
265-
266-
// Write the single page file
267-
fs.writeFileSync(outputPath, singlePageContent, 'utf-8');
268-
269-
console.log(`Single-page documentation generated: ${outputPath}`);
270-
console.log(`Total files combined: ${allFiles.length}`);
271-
console.log(`Total content size: ${Math.round(singlePageContent.length / 1024)}KB`);
272-
}
190+
273191

274192
function getProductDisplayName(productName: string): string {
275193
// Remove number prefix and capitalize
@@ -340,7 +258,7 @@ function main(): void {
340258
return;
341259
}
342260

343-
createSinglePageDocumentation(sectionType as 'products' | 'api', productName);
261+
createSinglePageToggleDocumentation(sectionType as 'products' | 'api', productName);
344262
}
345263

346264
// Check if this module is being run directly
@@ -491,4 +409,4 @@ function hasMarkdownContent(directory: string): boolean {
491409
return hasSubdirContent;
492410
}
493411

494-
export { createSinglePageDocumentation, createSinglePageToggleDocumentation, generateAllToggleSinglePages };
412+
export { createSinglePageToggleDocumentation, generateAllToggleSinglePages };

0 commit comments

Comments
 (0)