oneie 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/agent-integration.js +499 -0
- package/bin/cli-animations.js +420 -0
- package/bin/template-generator.js +661 -0
- package/one/tools/agent-installer.js +575 -0
- package/one/tools/lib/dependency-resolver.js +179 -0
- package/one/tools/lib/template-processor.js +748 -0
- package/one/tools/lib/yaml-utils.js +29 -0
- package/package.json +1 -1
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template Processor for Apple-Style Book Generation
|
|
3
|
+
* Handles hierarchical book structure detection, image mapping, and content processing
|
|
4
|
+
* Supports 5-part structure: Foundation → ATTRACT → CONVERT → GROW → REFINE + Appendix
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { promises as fs, accessSync } from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Strip YAML frontmatter from markdown content
|
|
12
|
+
* @param {string} content - Markdown content with potential frontmatter
|
|
13
|
+
* @returns {string} Content without frontmatter
|
|
14
|
+
*/
|
|
15
|
+
function stripYamlFrontmatter(content) {
|
|
16
|
+
// Remove YAML frontmatter (--- ... --- at start of file)
|
|
17
|
+
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n/;
|
|
18
|
+
content = content.replace(frontmatterRegex, '');
|
|
19
|
+
|
|
20
|
+
// Also remove any remaining --- ... --- blocks in the content
|
|
21
|
+
const inlineYamlRegex = /\n---\s*\n([\s\S]*?)\n---?\s*\n/g;
|
|
22
|
+
content = content.replace(inlineYamlRegex, '\n\n');
|
|
23
|
+
|
|
24
|
+
return content.trim();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Detect hierarchical book structure from content directory
|
|
29
|
+
* Supports folder-based structure: 0-introduction/, 1-foundation/, 2-attract/, etc.
|
|
30
|
+
* @param {string} contentDir - Path to content directory
|
|
31
|
+
* @param {string} imagesDir - Path to images directory
|
|
32
|
+
* @returns {Object} Detected hierarchical book structure
|
|
33
|
+
*/
|
|
34
|
+
async function detectHierarchicalBookStructure(contentDir, imagesDir) {
|
|
35
|
+
const structure = {
|
|
36
|
+
introduction: { files: [], title: 'Introduction' },
|
|
37
|
+
parts: [
|
|
38
|
+
{ number: 1, title: 'Foundation', chapters: [], folder: '' },
|
|
39
|
+
{ number: 2, title: 'ATTRACT', chapters: [], folder: '' },
|
|
40
|
+
{ number: 3, title: 'CONVERT', chapters: [], folder: '' },
|
|
41
|
+
{ number: 4, title: 'GROW', chapters: [], folder: '' },
|
|
42
|
+
{ number: 5, title: 'REFINE', chapters: [], folder: '' }
|
|
43
|
+
],
|
|
44
|
+
appendix: { files: [], title: 'Appendix' },
|
|
45
|
+
images: {}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const entries = await fs.readdir(contentDir, { withFileTypes: true });
|
|
50
|
+
|
|
51
|
+
for (const entry of entries) {
|
|
52
|
+
if (entry.isDirectory()) {
|
|
53
|
+
const folderName = entry.name;
|
|
54
|
+
const folderPath = path.join(contentDir, folderName);
|
|
55
|
+
|
|
56
|
+
if (folderName.match(/^0-introduction/)) {
|
|
57
|
+
// Introduction folder
|
|
58
|
+
const introFiles = await scanFolderForFiles(folderPath, imagesDir);
|
|
59
|
+
structure.introduction.files = introFiles;
|
|
60
|
+
} else if (folderName.match(/^[1-5]-/)) {
|
|
61
|
+
// Part folders (1-foundation, 2-attract, etc.)
|
|
62
|
+
const partMatch = folderName.match(/^(\d+)-(.+)$/);
|
|
63
|
+
if (partMatch) {
|
|
64
|
+
const [, partNum, partName] = partMatch;
|
|
65
|
+
const partIndex = parseInt(partNum) - 1;
|
|
66
|
+
|
|
67
|
+
if (partIndex >= 0 && partIndex < structure.parts.length) {
|
|
68
|
+
const partFiles = await scanFolderForFiles(folderPath, imagesDir);
|
|
69
|
+
structure.parts[partIndex].title = partName.toUpperCase();
|
|
70
|
+
structure.parts[partIndex].folder = folderName;
|
|
71
|
+
structure.parts[partIndex].chapters = await parseHierarchicalFiles(partFiles, folderName);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} else if (folderName === 'appendix') {
|
|
75
|
+
// Appendix folder
|
|
76
|
+
const appendixFiles = await scanFolderForFiles(folderPath, imagesDir);
|
|
77
|
+
structure.appendix.files = appendixFiles;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Sort all parts and their chapters
|
|
83
|
+
structure.parts.forEach(part => {
|
|
84
|
+
part.chapters.sort((a, b) => {
|
|
85
|
+
// Sort by hierarchical numbering
|
|
86
|
+
const aSort = `${a.chapterNumber || 0}.${a.sectionNumber || 0}.${a.subSectionNumber || 0}`;
|
|
87
|
+
const bSort = `${b.chapterNumber || 0}.${b.sectionNumber || 0}.${b.subSectionNumber || 0}`;
|
|
88
|
+
return aSort.localeCompare(bSort, undefined, { numeric: true });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Sort sections within each chapter
|
|
92
|
+
part.chapters.forEach(chapter => {
|
|
93
|
+
if (chapter.sections) {
|
|
94
|
+
chapter.sections.sort((a, b) => {
|
|
95
|
+
const aSort = `${a.sectionNumber || 0}.${a.subSectionNumber || 0}`;
|
|
96
|
+
const bSort = `${b.sectionNumber || 0}.${b.subSectionNumber || 0}`;
|
|
97
|
+
return aSort.localeCompare(bSort, undefined, { numeric: true });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Sort introduction and appendix files
|
|
104
|
+
structure.introduction.files.sort((a, b) => parseFloat(a.number) - parseFloat(b.number));
|
|
105
|
+
structure.appendix.files.sort((a, b) => a.number.localeCompare(b.number));
|
|
106
|
+
|
|
107
|
+
return structure;
|
|
108
|
+
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error('Error detecting hierarchical book structure:', error);
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Scan a folder for markdown files and return file information
|
|
117
|
+
* @param {string} folderPath - Path to folder
|
|
118
|
+
* @param {string} imagesDir - Images directory path
|
|
119
|
+
* @returns {Array} Array of file information objects
|
|
120
|
+
*/
|
|
121
|
+
async function scanFolderForFiles(folderPath, imagesDir) {
|
|
122
|
+
const files = [];
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const entries = await fs.readdir(folderPath);
|
|
126
|
+
|
|
127
|
+
for (const entry of entries) {
|
|
128
|
+
if (entry.endsWith('.md')) {
|
|
129
|
+
const filePath = path.join(folderPath, entry);
|
|
130
|
+
const fileInfo = await parseHierarchicalFile(entry, folderPath, imagesDir);
|
|
131
|
+
if (fileInfo) {
|
|
132
|
+
files.push(fileInfo);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error(`Error scanning folder ${folderPath}:`, error);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return files;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Parse hierarchical files within a folder
|
|
145
|
+
* @param {Array} files - Array of file information objects
|
|
146
|
+
* @param {string} folderName - Name of the folder
|
|
147
|
+
* @returns {Array} Array of parsed chapter/section objects
|
|
148
|
+
*/
|
|
149
|
+
async function parseHierarchicalFiles(files, folderName) {
|
|
150
|
+
const chapters = [];
|
|
151
|
+
|
|
152
|
+
for (const file of files) {
|
|
153
|
+
const fileName = file.file;
|
|
154
|
+
|
|
155
|
+
// Parse files like: 1-foundation.md, 1.1-company.md, 1.1.2-tokenomics.md
|
|
156
|
+
const match = fileName.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?-(.+)\.md$/);
|
|
157
|
+
if (match) {
|
|
158
|
+
const [, chapterNum, sectionNum, subSectionNum, name] = match;
|
|
159
|
+
|
|
160
|
+
const chapterInfo = {
|
|
161
|
+
file: fileName,
|
|
162
|
+
folder: folderName,
|
|
163
|
+
chapterNumber: parseInt(chapterNum),
|
|
164
|
+
sectionNumber: sectionNum ? parseInt(sectionNum) : null,
|
|
165
|
+
subSectionNumber: subSectionNum ? parseInt(subSectionNum) : null,
|
|
166
|
+
name: name,
|
|
167
|
+
title: formatTitle(name),
|
|
168
|
+
number: fileName.match(/^(\d+(?:\.\d+)*)/)[1],
|
|
169
|
+
level: sectionNum ? (subSectionNum ? 3 : 2) : 1,
|
|
170
|
+
sections: [],
|
|
171
|
+
fullPageImage: false,
|
|
172
|
+
imageName: null
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// Check for associated image
|
|
176
|
+
await checkForImage(chapterInfo, file.imagesDir);
|
|
177
|
+
|
|
178
|
+
chapters.push(chapterInfo);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return chapters;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Parse hierarchical file information
|
|
187
|
+
* @param {string} file - Filename
|
|
188
|
+
* @param {string} folderPath - Path to folder containing the file
|
|
189
|
+
* @param {string} imagesDir - Images directory path
|
|
190
|
+
* @returns {Object|null} Parsed file information
|
|
191
|
+
*/
|
|
192
|
+
async function parseHierarchicalFile(file, folderPath, imagesDir) {
|
|
193
|
+
const fileInfo = {
|
|
194
|
+
file,
|
|
195
|
+
folderPath,
|
|
196
|
+
imagesDir,
|
|
197
|
+
fullPageImage: false,
|
|
198
|
+
imageName: null
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
return fileInfo;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Format title from filename
|
|
206
|
+
* @param {string} name - Name from filename
|
|
207
|
+
* @returns {string} Formatted title
|
|
208
|
+
*/
|
|
209
|
+
function formatTitle(name) {
|
|
210
|
+
return name.split('-').map(word =>
|
|
211
|
+
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
212
|
+
).join(' ');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Check for associated image for a chapter/section
|
|
217
|
+
* @param {Object} chapterInfo - Chapter information object
|
|
218
|
+
* @param {string} imagesDir - Images directory path
|
|
219
|
+
*/
|
|
220
|
+
async function checkForImage(chapterInfo, imagesDir) {
|
|
221
|
+
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif'];
|
|
222
|
+
|
|
223
|
+
// Try different image naming patterns
|
|
224
|
+
const patterns = [
|
|
225
|
+
`${chapterInfo.number}-${chapterInfo.name}`, // e.g., 1.1-company
|
|
226
|
+
`${chapterInfo.title.replace(/\s+/g, '')}`, // e.g., Company
|
|
227
|
+
`${chapterInfo.name}` // e.g., company
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
for (const pattern of patterns) {
|
|
231
|
+
for (const ext of imageExtensions) {
|
|
232
|
+
const imageName = `${pattern}.${ext}`;
|
|
233
|
+
const imagePath = path.join(imagesDir, imageName);
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
await fs.access(imagePath);
|
|
237
|
+
chapterInfo.fullPageImage = true;
|
|
238
|
+
chapterInfo.imageName = imageName;
|
|
239
|
+
return;
|
|
240
|
+
} catch (err) {
|
|
241
|
+
// Image doesn't exist, continue checking
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Add file to hierarchical structure
|
|
249
|
+
* @param {Object} structure - Book structure
|
|
250
|
+
* @param {Object} fileInfo - File information
|
|
251
|
+
*/
|
|
252
|
+
function addToHierarchicalStructure(structure, fileInfo) {
|
|
253
|
+
const numberParts = fileInfo.number.split('.');
|
|
254
|
+
|
|
255
|
+
if (fileInfo.number.startsWith('0.')) {
|
|
256
|
+
// Introduction files
|
|
257
|
+
structure.introduction.files.push(fileInfo);
|
|
258
|
+
} else if (fileInfo.number.startsWith('A.')) {
|
|
259
|
+
// Appendix files
|
|
260
|
+
structure.appendix.files.push(fileInfo);
|
|
261
|
+
} else if (numberParts.length >= 2) {
|
|
262
|
+
// Part files (1.x, 2.x, etc.)
|
|
263
|
+
const partNum = parseInt(numberParts[0]);
|
|
264
|
+
const part = structure.parts.find(p => p.number === partNum);
|
|
265
|
+
|
|
266
|
+
if (part) {
|
|
267
|
+
if (numberParts.length === 2) {
|
|
268
|
+
// Chapter level (e.g., 2.1-attract.md = Part 2, Chapter 1)
|
|
269
|
+
const chapterNum = parseInt(numberParts[1]);
|
|
270
|
+
const chapter = {
|
|
271
|
+
...fileInfo,
|
|
272
|
+
chapterNumber: chapterNum,
|
|
273
|
+
partNumber: partNum,
|
|
274
|
+
sections: [],
|
|
275
|
+
isChapter: true
|
|
276
|
+
};
|
|
277
|
+
part.chapters.push(chapter);
|
|
278
|
+
} else {
|
|
279
|
+
// Section level (e.g., 2.1.1-attract-introduction.md = Part 2, Chapter 1, Section 1)
|
|
280
|
+
const chapterNum = parseInt(numberParts[1]);
|
|
281
|
+
const chapterKey = `${partNum}.${chapterNum}`;
|
|
282
|
+
let parentChapter = part.chapters.find(c => c.chapterNumber === chapterNum);
|
|
283
|
+
|
|
284
|
+
if (!parentChapter) {
|
|
285
|
+
// Create placeholder chapter if it doesn't exist
|
|
286
|
+
parentChapter = {
|
|
287
|
+
number: chapterKey,
|
|
288
|
+
chapterNumber: chapterNum,
|
|
289
|
+
partNumber: partNum,
|
|
290
|
+
name: 'placeholder',
|
|
291
|
+
title: 'Placeholder Chapter',
|
|
292
|
+
file: '',
|
|
293
|
+
sections: [],
|
|
294
|
+
isChapter: true,
|
|
295
|
+
fullPageImage: false
|
|
296
|
+
};
|
|
297
|
+
part.chapters.push(parentChapter);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const sectionNum = parseInt(numberParts[2]);
|
|
301
|
+
parentChapter.sections.push({
|
|
302
|
+
...fileInfo,
|
|
303
|
+
sectionNumber: sectionNum,
|
|
304
|
+
chapterNumber: chapterNum,
|
|
305
|
+
partNumber: partNum,
|
|
306
|
+
isSection: true
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Legacy function for backward compatibility
|
|
315
|
+
* @param {string} contentDir - Path to content directory
|
|
316
|
+
* @param {string} imagesDir - Path to images directory
|
|
317
|
+
* @returns {Object} Detected book structure
|
|
318
|
+
*/
|
|
319
|
+
async function detectChapterStructure(contentDir, imagesDir) {
|
|
320
|
+
// Try hierarchical structure first
|
|
321
|
+
const hierarchicalStructure = await detectHierarchicalBookStructure(contentDir, imagesDir);
|
|
322
|
+
|
|
323
|
+
// Convert to legacy format for backward compatibility
|
|
324
|
+
const legacyStructure = {
|
|
325
|
+
chapters: [],
|
|
326
|
+
images: hierarchicalStructure.images,
|
|
327
|
+
sections: {}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// Flatten hierarchical structure to legacy format
|
|
331
|
+
hierarchicalStructure.parts.forEach(part => {
|
|
332
|
+
part.chapters.forEach(chapter => {
|
|
333
|
+
if (chapter.isChapter) {
|
|
334
|
+
legacyStructure.chapters.push({
|
|
335
|
+
file: chapter.file,
|
|
336
|
+
number: parseFloat(chapter.number),
|
|
337
|
+
name: chapter.name,
|
|
338
|
+
title: chapter.title,
|
|
339
|
+
titlePage: true,
|
|
340
|
+
fullPageImage: chapter.fullPageImage,
|
|
341
|
+
sections: chapter.sections.map(section => ({
|
|
342
|
+
file: section.file,
|
|
343
|
+
chapterNumber: parseInt(chapter.number.split('.')[0]),
|
|
344
|
+
sectionNumber: parseInt(section.number.split('.').slice(-1)[0]),
|
|
345
|
+
name: section.name,
|
|
346
|
+
title: section.title,
|
|
347
|
+
fullPath: section.number
|
|
348
|
+
}))
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// Add sections to legacy sections object
|
|
352
|
+
chapter.sections.forEach(section => {
|
|
353
|
+
legacyStructure.sections[section.file] = {
|
|
354
|
+
file: section.file,
|
|
355
|
+
chapterNumber: parseInt(chapter.number.split('.')[0]),
|
|
356
|
+
sectionNumber: parseInt(section.number.split('.').slice(-1)[0]),
|
|
357
|
+
name: section.name,
|
|
358
|
+
title: section.title,
|
|
359
|
+
fullPath: section.number
|
|
360
|
+
};
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
return legacyStructure;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Process a hierarchical file (part, chapter, or section) with appropriate formatting
|
|
371
|
+
* @param {Object} item - File item (part, chapter, or section)
|
|
372
|
+
* @param {string} content - File content
|
|
373
|
+
* @param {string} imagesDir - Images directory path
|
|
374
|
+
* @param {string} type - Type of item ('part', 'chapter', 'section', 'introduction', 'appendix')
|
|
375
|
+
* @returns {string} Processed content
|
|
376
|
+
*/
|
|
377
|
+
function processHierarchicalItem(item, content, imagesDir, type = 'chapter') {
|
|
378
|
+
let processed = '';
|
|
379
|
+
|
|
380
|
+
if (type === 'part') {
|
|
381
|
+
// Add part title page
|
|
382
|
+
processed += `\\newpart{${item.number}}{${item.title}}\n\n`;
|
|
383
|
+
} else if (type === 'chapter') {
|
|
384
|
+
// Add chapter title page marker with simple chapter number
|
|
385
|
+
const chapterNum = item.chapterNumber || item.number;
|
|
386
|
+
processed += `\\newchapter{${chapterNum}}{${item.title}}\n\n`;
|
|
387
|
+
} else if (type === 'section') {
|
|
388
|
+
// Add section header with simple section number
|
|
389
|
+
const sectionNum = item.sectionNumber || item.number;
|
|
390
|
+
processed += `\\newsection{${sectionNum}}{${item.title}}\n\n`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Add full-page image only if it exists
|
|
394
|
+
if (item.fullPageImage && item.imageName) {
|
|
395
|
+
const imagePath = path.join(imagesDir, item.imageName);
|
|
396
|
+
// Verify the image file exists before including it
|
|
397
|
+
try {
|
|
398
|
+
accessSync(imagePath);
|
|
399
|
+
processed += `\n\n`;
|
|
400
|
+
} catch (err) {
|
|
401
|
+
console.warn(`Image not found: ${imagePath} for ${type} ${item.number}`);
|
|
402
|
+
// Don't include image if it doesn't exist
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Process content for image references
|
|
407
|
+
const processedContent = processImageReferences(content, item.file, imagesDir);
|
|
408
|
+
processed += processedContent;
|
|
409
|
+
|
|
410
|
+
return processed;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Legacy function for backward compatibility
|
|
415
|
+
* @param {Object} chapter - Chapter object
|
|
416
|
+
* @param {string} content - Chapter content
|
|
417
|
+
* @param {string} imagesDir - Images directory path
|
|
418
|
+
* @returns {string} Processed content
|
|
419
|
+
*/
|
|
420
|
+
function processChapter(chapter, content, imagesDir) {
|
|
421
|
+
return processHierarchicalItem(chapter, content, imagesDir, 'chapter');
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Process image references in markdown content
|
|
426
|
+
* @param {string} content - Markdown content
|
|
427
|
+
* @param {string} currentFile - Current file being processed
|
|
428
|
+
* @param {string} imagesDir - Images directory path
|
|
429
|
+
* @returns {string} Content with resolved image paths
|
|
430
|
+
*/
|
|
431
|
+
function processImageReferences(content, currentFile, imagesDir) {
|
|
432
|
+
// First, handle Unicode characters that LaTeX doesn't handle well
|
|
433
|
+
content = content.replace(/✓/g, '[X]');
|
|
434
|
+
content = content.replace(/✗/g, '[ ]');
|
|
435
|
+
content = content.replace(/→/g, '->');
|
|
436
|
+
content = content.replace(/←/g, '<-');
|
|
437
|
+
content = content.replace(/↑/g, '^');
|
|
438
|
+
content = content.replace(/↓/g, 'v');
|
|
439
|
+
content = content.replace(/•/g, '*');
|
|
440
|
+
content = content.replace(/…/g, '...');
|
|
441
|
+
|
|
442
|
+
// Then process image references
|
|
443
|
+
return content.replace(
|
|
444
|
+
/!\[([^\]]*)\]\(([^)]+)\)/g,
|
|
445
|
+
(match, alt, src) => {
|
|
446
|
+
// If already absolute path or URL, leave as is
|
|
447
|
+
if (src.startsWith('/') || src.startsWith('http')) {
|
|
448
|
+
return match;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Handle images/ path references by converting to images/
|
|
452
|
+
let imagePath = src;
|
|
453
|
+
if (src.startsWith('images/')) {
|
|
454
|
+
imagePath = src.replace('images/', '');
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Resolve relative path to images directory
|
|
458
|
+
const fullPath = path.join(imagesDir, imagePath);
|
|
459
|
+
|
|
460
|
+
// Check if file exists
|
|
461
|
+
try {
|
|
462
|
+
// Use synchronous access for now since we're in a replace callback
|
|
463
|
+
accessSync(fullPath);
|
|
464
|
+
return ``;
|
|
465
|
+
} catch (err) {
|
|
466
|
+
console.warn(`Image not found: ${src} in ${currentFile}`);
|
|
467
|
+
// Return empty string or skip image if not found to prevent broken images
|
|
468
|
+
return '';
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Generate consolidated markdown for hierarchical book structure
|
|
476
|
+
* @param {Object} structure - Hierarchical book structure
|
|
477
|
+
* @param {string} contentDir - Content directory path
|
|
478
|
+
* @param {string} imagesDir - Images directory path
|
|
479
|
+
* @returns {string} Consolidated markdown content
|
|
480
|
+
*/
|
|
481
|
+
async function generateHierarchicalMarkdown(structure, contentDir, imagesDir) {
|
|
482
|
+
let consolidated = '';
|
|
483
|
+
|
|
484
|
+
// Process Introduction (just content, no chapter/part structure)
|
|
485
|
+
if (structure.introduction.files.length > 0) {
|
|
486
|
+
for (const file of structure.introduction.files) {
|
|
487
|
+
if (file.file) {
|
|
488
|
+
const filePath = path.join(file.folderPath, file.file);
|
|
489
|
+
let content = await fs.readFile(filePath, 'utf8');
|
|
490
|
+
content = stripYamlFrontmatter(content);
|
|
491
|
+
|
|
492
|
+
// Convert markdown headers to proper LaTeX sections
|
|
493
|
+
const lines = content.split('\n');
|
|
494
|
+
const processedLines = [];
|
|
495
|
+
|
|
496
|
+
for (const line of lines) {
|
|
497
|
+
if (line.startsWith('# ')) {
|
|
498
|
+
processedLines.push(`\\section*{${line.substring(2)}}`);
|
|
499
|
+
} else if (line.startsWith('## ')) {
|
|
500
|
+
processedLines.push(`\\subsection*{${line.substring(3)}}`);
|
|
501
|
+
} else if (line.startsWith('### ')) {
|
|
502
|
+
processedLines.push(`\\subsubsection*{${line.substring(4)}}`);
|
|
503
|
+
} else {
|
|
504
|
+
processedLines.push(line);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
content = processedLines.join('\n');
|
|
509
|
+
|
|
510
|
+
// Remove CSS references from frontmatter to avoid conflicts
|
|
511
|
+
content = content.replace(/^css:\s*.*$/gm, '');
|
|
512
|
+
|
|
513
|
+
// Add full-page image if exists
|
|
514
|
+
if (file.fullPageImage && file.imageName) {
|
|
515
|
+
const imagePath = path.join(imagesDir, file.imageName);
|
|
516
|
+
try {
|
|
517
|
+
accessSync(imagePath);
|
|
518
|
+
consolidated += `\n\n`;
|
|
519
|
+
} catch (err) {
|
|
520
|
+
// Image doesn't exist, skip
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// Process image references in introduction content
|
|
525
|
+
const processedContent = processImageReferences(content, file.file, imagesDir);
|
|
526
|
+
consolidated += processedContent + '\n\n';
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Process Parts
|
|
532
|
+
for (const part of structure.parts) {
|
|
533
|
+
if (part.chapters.length > 0) {
|
|
534
|
+
consolidated += `# ${part.title}\n\n`;
|
|
535
|
+
|
|
536
|
+
for (const chapter of part.chapters) {
|
|
537
|
+
if (chapter.file) {
|
|
538
|
+
const chapterPath = path.join(contentDir, part.folder, chapter.file);
|
|
539
|
+
let chapterContent = await fs.readFile(chapterPath, 'utf8');
|
|
540
|
+
chapterContent = stripYamlFrontmatter(chapterContent);
|
|
541
|
+
|
|
542
|
+
// Remove markdown headers since we're using LaTeX structure
|
|
543
|
+
chapterContent = chapterContent.replace(/^#+\s+/gm, '');
|
|
544
|
+
|
|
545
|
+
// Remove CSS references from frontmatter to avoid conflicts
|
|
546
|
+
chapterContent = chapterContent.replace(/^css:\s*.*$/gm, '');
|
|
547
|
+
|
|
548
|
+
const chapterNum = chapter.chapterNumber || 1;
|
|
549
|
+
consolidated += `# ${chapter.title}\n\n`;
|
|
550
|
+
|
|
551
|
+
// Note: Images are now handled by the content files themselves
|
|
552
|
+
// Removed automatic image insertion to prevent duplicates
|
|
553
|
+
|
|
554
|
+
// Process image references in chapter content
|
|
555
|
+
const processedChapterContent = processImageReferences(chapterContent, chapter.file, imagesDir);
|
|
556
|
+
consolidated += processedChapterContent + '\n\n';
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Add sections
|
|
560
|
+
for (const section of chapter.sections) {
|
|
561
|
+
if (section.file) {
|
|
562
|
+
const sectionPath = path.join(contentDir, part.folder, section.file);
|
|
563
|
+
let sectionContent = await fs.readFile(sectionPath, 'utf8');
|
|
564
|
+
sectionContent = stripYamlFrontmatter(sectionContent);
|
|
565
|
+
|
|
566
|
+
// Remove markdown headers since we're using LaTeX structure
|
|
567
|
+
sectionContent = sectionContent.replace(/^#+\s+/gm, '');
|
|
568
|
+
|
|
569
|
+
const sectionNum = section.sectionNumber || 1;
|
|
570
|
+
consolidated += `\\section{${section.title}}\n\n`;
|
|
571
|
+
|
|
572
|
+
// Process image references in section content
|
|
573
|
+
const processedSectionContent = processImageReferences(sectionContent, section.file, imagesDir);
|
|
574
|
+
consolidated += processedSectionContent + '\n\n';
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// Process Appendix
|
|
582
|
+
if (structure.appendix.files.length > 0) {
|
|
583
|
+
consolidated += '# Appendix\n\n';
|
|
584
|
+
|
|
585
|
+
for (const file of structure.appendix.files) {
|
|
586
|
+
if (file.file) {
|
|
587
|
+
const filePath = path.join(file.folderPath, file.file);
|
|
588
|
+
let content = await fs.readFile(filePath, 'utf8');
|
|
589
|
+
content = stripYamlFrontmatter(content);
|
|
590
|
+
|
|
591
|
+
// Remove markdown headers since we're using LaTeX structure
|
|
592
|
+
content = content.replace(/^#+\s+/gm, '');
|
|
593
|
+
|
|
594
|
+
consolidated += `# ${file.title}\n\n`;
|
|
595
|
+
|
|
596
|
+
// Process image references in appendix content
|
|
597
|
+
const processedContent = processImageReferences(content, file.file, imagesDir);
|
|
598
|
+
consolidated += processedContent + '\n\n';
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
return consolidated;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Legacy function for backward compatibility
|
|
608
|
+
* @param {Object} structure - Book structure
|
|
609
|
+
* @param {string} contentDir - Content directory path
|
|
610
|
+
* @param {string} imagesDir - Images directory path
|
|
611
|
+
* @returns {string} Consolidated markdown content
|
|
612
|
+
*/
|
|
613
|
+
async function generateConsolidatedMarkdown(structure, contentDir, imagesDir) {
|
|
614
|
+
// Check if this is a hierarchical structure
|
|
615
|
+
if (structure.parts && structure.introduction && structure.appendix) {
|
|
616
|
+
return generateHierarchicalMarkdown(structure, contentDir, imagesDir);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Legacy processing for old structure
|
|
620
|
+
let consolidated = '';
|
|
621
|
+
|
|
622
|
+
for (const chapter of structure.chapters) {
|
|
623
|
+
// Read chapter content
|
|
624
|
+
const chapterPath = path.join(contentDir, chapter.file);
|
|
625
|
+
let chapterContent = await fs.readFile(chapterPath, 'utf8');
|
|
626
|
+
chapterContent = stripYamlFrontmatter(chapterContent);
|
|
627
|
+
|
|
628
|
+
// Process chapter with title page and image
|
|
629
|
+
const processedChapter = processChapter(chapter, chapterContent, imagesDir);
|
|
630
|
+
consolidated += processedChapter + '\n\n';
|
|
631
|
+
|
|
632
|
+
// Add sections
|
|
633
|
+
for (const section of chapter.sections) {
|
|
634
|
+
const sectionPath = path.join(contentDir, section.file);
|
|
635
|
+
let sectionContent = await fs.readFile(sectionPath, 'utf8');
|
|
636
|
+
sectionContent = stripYamlFrontmatter(sectionContent);
|
|
637
|
+
|
|
638
|
+
// Process section content
|
|
639
|
+
const processedSection = processImageReferences(sectionContent, section.file, imagesDir);
|
|
640
|
+
consolidated += processedSection + '\n\n';
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
return consolidated;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Create hierarchical table of contents from book structure
|
|
649
|
+
* @param {Object} structure - Hierarchical book structure
|
|
650
|
+
* @returns {string} Table of contents markdown
|
|
651
|
+
*/
|
|
652
|
+
function generateHierarchicalTableOfContents(structure) {
|
|
653
|
+
let toc = '# Table of Contents\n\n';
|
|
654
|
+
|
|
655
|
+
// Introduction
|
|
656
|
+
if (structure.introduction.files.length > 0) {
|
|
657
|
+
toc += '## Introduction\n\n';
|
|
658
|
+
for (const file of structure.introduction.files) {
|
|
659
|
+
// Extract title from filename or use formatted title
|
|
660
|
+
const match = file.file.match(/^(\d+)-(.+)\.md$/);
|
|
661
|
+
const title = match ? formatTitle(match[2]) : file.title || 'Introduction';
|
|
662
|
+
toc += `- [${title}](#introduction)\n`;
|
|
663
|
+
}
|
|
664
|
+
toc += '\n';
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Parts
|
|
668
|
+
for (const part of structure.parts) {
|
|
669
|
+
if (part.chapters.length > 0) {
|
|
670
|
+
toc += `## Part ${part.number}: ${part.title}\n\n`;
|
|
671
|
+
|
|
672
|
+
for (const chapter of part.chapters) {
|
|
673
|
+
if (chapter.file) {
|
|
674
|
+
const chapterNum = chapter.number || chapter.chapterNumber;
|
|
675
|
+
toc += `### ${chapterNum}: ${chapter.title}\n\n`;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// Group sections by level
|
|
679
|
+
const level2Sections = chapter.sections?.filter(s => s.level === 2) || [];
|
|
680
|
+
const level3Sections = chapter.sections?.filter(s => s.level === 3) || [];
|
|
681
|
+
|
|
682
|
+
for (const section of level2Sections) {
|
|
683
|
+
const sectionNum = section.number || section.sectionNumber;
|
|
684
|
+
toc += `- [${sectionNum} ${section.title}](#${section.number.replace(/\./g, '-')})\n`;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
for (const section of level3Sections) {
|
|
688
|
+
const sectionNum = section.number || section.sectionNumber;
|
|
689
|
+
toc += ` - [${sectionNum} ${section.title}](#${section.number.replace(/\./g, '-')})\n`;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
if (chapter.sections && chapter.sections.length > 0) {
|
|
693
|
+
toc += '\n';
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// Appendix
|
|
700
|
+
if (structure.appendix.files.length > 0) {
|
|
701
|
+
toc += '## Appendix\n\n';
|
|
702
|
+
for (const file of structure.appendix.files) {
|
|
703
|
+
const match = file.file.match(/^A\.(\d+)-(.+)\.md$/);
|
|
704
|
+
const title = match ? formatTitle(match[2]) : file.title || 'Appendix';
|
|
705
|
+
toc += `- [${title}](#appendix)\n`;
|
|
706
|
+
}
|
|
707
|
+
toc += '\n';
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
return toc;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Legacy function for backward compatibility
|
|
715
|
+
* @param {Object} structure - Book structure
|
|
716
|
+
* @returns {string} Table of contents markdown
|
|
717
|
+
*/
|
|
718
|
+
function generateTableOfContents(structure) {
|
|
719
|
+
// Check if this is a hierarchical structure
|
|
720
|
+
if (structure.parts && structure.introduction && structure.appendix) {
|
|
721
|
+
return generateHierarchicalTableOfContents(structure);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Legacy TOC generation
|
|
725
|
+
let toc = '# Table of Contents\n\n';
|
|
726
|
+
|
|
727
|
+
for (const chapter of structure.chapters) {
|
|
728
|
+
toc += `## Chapter ${chapter.number}: ${chapter.title}\n\n`;
|
|
729
|
+
|
|
730
|
+
for (const section of chapter.sections) {
|
|
731
|
+
toc += `### ${section.fullPath} ${section.title}\n\n`;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
return toc;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
export {
|
|
739
|
+
detectHierarchicalBookStructure,
|
|
740
|
+
detectChapterStructure,
|
|
741
|
+
processHierarchicalItem,
|
|
742
|
+
processChapter,
|
|
743
|
+
processImageReferences,
|
|
744
|
+
generateHierarchicalMarkdown,
|
|
745
|
+
generateConsolidatedMarkdown,
|
|
746
|
+
generateHierarchicalTableOfContents,
|
|
747
|
+
generateTableOfContents
|
|
748
|
+
};
|