-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathreorder-docs.js
More file actions
executable file
·148 lines (123 loc) · 4.69 KB
/
reorder-docs.js
File metadata and controls
executable file
·148 lines (123 loc) · 4.69 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const docsPath = path.join(__dirname, '../doc/API.md');
if (!fs.existsSync(docsPath)) {
console.error('Documentation file not found:', docsPath);
process.exit(1);
}
const content = fs.readFileSync(docsPath, 'utf8');
// Split content by ### headers to get sections
const sections = content.split(/(?=^### )/m);
const header = sections[0]; // The initial content before first ###
// Extract class/interface sections
const classSections = sections.slice(1);
// Find specific sections we want to prioritize
const gridStackSection = classSections.find(s => s.startsWith('### GridStack\n'));
const gridStackEngineSection = classSections.find(s => s.startsWith('### GridStackEngine\n'));
const utilsSection = classSections.find(s => s.startsWith('### Utils\n'));
const gridStackOptionsSection = classSections.find(s => s.startsWith('### GridStackOptions\n'));
// Remove prioritized sections from the rest
const remainingSections = classSections.filter(s =>
!s.startsWith('### GridStack\n') &&
!s.startsWith('### GridStackEngine\n') &&
!s.startsWith('### Utils\n') &&
!s.startsWith('### GridStackOptions\n')
);
// Extract existing anchor links from the content to preserve TypeDoc's numbering
const extractExistingAnchors = (content) => {
const anchorMap = new Map();
const linkRegex = /\[([^\]]+)\]\(#([^)]+)\)/g;
let match;
while ((match = linkRegex.exec(content)) !== null) {
const linkText = match[1];
const anchor = match[2];
// Map clean title to actual anchor
const cleanTitle = linkText.replace(/`/g, '').trim();
anchorMap.set(cleanTitle, anchor);
}
return anchorMap;
};
// Create table of contents using existing anchors
const createToc = (sections, anchorMap) => {
let toc = '\n## Table of Contents\n\n';
// Add main sections first
if (gridStackSection) {
toc += '- [GridStack](#gridstack)\n';
}
if (gridStackEngineSection) {
toc += '- [GridStackEngine](#gridstackengine)\n';
}
if (utilsSection) {
toc += '- [Utils](#utils)\n';
}
if (gridStackOptionsSection) {
toc += '- [GridStackOptions](#gridstackoptions)\n';
}
// Add other sections using existing anchors when possible
sections.forEach(section => {
const match = section.match(/^### (.+)$/m);
if (match) {
const title = match[1];
const cleanTitle = title.replace(/`/g, '').trim();
// Use existing anchor if found, otherwise generate one
let anchor = anchorMap.get(cleanTitle);
if (!anchor) {
// Fallback anchor generation
anchor = title
.toLowerCase()
.replace(/`/g, '')
.replace(/[^a-z0-9\s]/g, '')
.replace(/\s+/g, '-')
.replace(/^-+|-+$/g, '');
}
toc += `- [${title}](#${anchor})\n`;
}
});
return toc + '\n';
};
// Extract existing anchors from the original content
const anchorMap = extractExistingAnchors(content);
// Rebuild content with desired order
let reorderedContent = header;
// Add table of contents
reorderedContent += createToc(remainingSections, anchorMap);
// Add prioritized sections first with explicit anchors
if (gridStackSection) {
const sectionWithAnchor = gridStackSection.replace(/^### (.+)$/m, '<a id="gridstack"></a>\n### $1');
reorderedContent += sectionWithAnchor;
}
if (gridStackEngineSection) {
const sectionWithAnchor = gridStackEngineSection.replace(/^### (.+)$/m, '<a id="gridstackengine"></a>\n### $1');
reorderedContent += sectionWithAnchor;
}
if (utilsSection) {
const sectionWithAnchor = utilsSection.replace(/^### (.+)$/m, '<a id="utils"></a>\n### $1');
reorderedContent += sectionWithAnchor;
}
if (gridStackOptionsSection) {
const sectionWithAnchor = gridStackOptionsSection.replace(/^### (.+)$/m, '<a id="gridstackoptions"></a>\n### $1');
reorderedContent += sectionWithAnchor;
}
// Add remaining sections with explicit anchors
remainingSections.forEach(section => {
// Add explicit anchor tags to headers for better compatibility
const sectionWithAnchors = section.replace(/^### (.+)$/gm, (match, title) => {
const cleanTitle = title.replace(/`/g, '').trim();
let anchor = anchorMap.get(cleanTitle);
if (!anchor) {
anchor = title
.toLowerCase()
.replace(/`/g, '')
.replace(/[^a-z0-9\s]/g, '')
.replace(/\s+/g, '-')
.replace(/^-+|-+$/g, '');
}
return `<a id="${anchor}"></a>\n### ${title}`;
});
reorderedContent += sectionWithAnchors;
});
// Write the reordered content back
fs.writeFileSync(docsPath, reorderedContent);
console.log('✅ Documentation reordered successfully!');
console.log('Order: GridStack → GridStackEngine → Utils → GridStackOptions → Others');