-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-docs.ts
More file actions
379 lines (337 loc) · 11.4 KB
/
markdown-docs.ts
File metadata and controls
379 lines (337 loc) · 11.4 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
/**
* Markdown documentation generator
*
* @module docs
* @description
* Generates markdown documentation from IR data and rendered diagrams.
*
* This generator:
* 1. Reads IR data and rendered diagram files
* 2. Configures Nunjucks templating engine
* 3. Generates system overview page with system/container/component diagrams
* 4. Generates individual component pages with component/code diagrams
* 5. Writes markdown files to docs_out directory
* 6. Updates pipeline state with generated file metadata
*
* @see {@link module:core/types-ir} for IR structure
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import type { PipelineContext, Logger, RendererOutput } from '../../core/types.js';
import type { Component } from '../../core/types-ir.js';
import { resolveArchlettePath } from '../../core/path-resolver.js';
const require = createRequire(import.meta.url);
const nunjucks = require('nunjucks');
/**
* Generate markdown documentation
*/
export default async function markdownDocs(ctx: PipelineContext): Promise<void> {
ctx.log.info('Markdown Docs: generating documentation...');
// Get IR data
const ir = ctx.state.validatedIR || ctx.state.aggregatedIR;
if (!ir) {
ctx.log.error('No IR data found in pipeline state');
throw new Error('No IR data found. Run extract and validate stages first.');
}
ctx.log.debug(
`Loaded IR: ${ir.components.length} components, ${ir.actors.length} actors`,
);
// Get rendered diagram files
const rendererOutputs = ctx.state.rendererOutputs || [];
ctx.log.debug(`Found ${rendererOutputs.length} renderer output(s)`);
// Get output directory
const docsDir = resolveArchlettePath(ctx.config.paths.docs_out, {
cliDir: ctx.configBaseDir,
});
// Get diagram directory from render_out config (not docs_out)
const diagramsDir = resolveArchlettePath(ctx.config.paths.render_out, {
cliDir: ctx.configBaseDir,
});
// Ensure output directory exists
fs.mkdirSync(docsDir, { recursive: true });
// Configure Nunjucks
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Copy brand images to output directory (one level up from docs_out)
const imagesSourceDir = path.join(__dirname, 'images');
const imagesDestDir = path.join(docsDir, '..', 'images');
if (fs.existsSync(imagesSourceDir)) {
fs.mkdirSync(imagesDestDir, { recursive: true });
for (const file of fs.readdirSync(imagesSourceDir)) {
fs.copyFileSync(path.join(imagesSourceDir, file), path.join(imagesDestDir, file));
}
ctx.log.debug(`Copied brand images to ${imagesDestDir}`);
}
const templateDir = path.join(__dirname, 'templates');
const env = nunjucks.configure(templateDir, {
autoescape: false,
trimBlocks: true,
lstripBlocks: true,
});
// Add custom filters
env.addFilter('kebabCase', (str: string) => {
return str
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
});
env.addFilter('sanitizeFileName', (str: string) => {
return sanitizeFileName(str);
});
env.addFilter('forwardSlashes', (str: string) => {
return str.replace(/\\/g, '/');
});
env.addFilter('date', (date: Date | string, format: string) => {
const d = typeof date === 'string' ? new Date() : date;
const pad = (n: number) => String(n).padStart(2, '0');
return format
.replace('YYYY', String(d.getFullYear()))
.replace('MM', pad(d.getMonth() + 1))
.replace('DD', pad(d.getDate()))
.replace('HH', pad(d.getHours()))
.replace('mm', pad(d.getMinutes()))
.replace('ss', pad(d.getSeconds()));
});
env.addFilter(
'selectattr',
(
arr: Array<Record<string, unknown>>,
attr: string,
test: string,
value: unknown,
) => {
if (test === 'equalto') {
return arr.filter((item) => item[attr] === value);
}
return arr;
},
);
env.addFilter(
'map',
(arr: Array<Record<string, unknown>>, _mapType: string, attr: string) => {
return arr.map((item) => item[attr]);
},
);
env.addFilter('first', <T>(arr: T[]) => {
return arr && arr.length > 0 ? arr[0] : undefined;
});
env.addFilter('mdTableCell', (value: unknown) => {
if (value === null || value === undefined || value === '') {
return '-';
}
return String(value).replace(/\|/g, '\\|').replace(/\r?\n/g, '<br>');
});
// Find diagram files for system views
const systemDiagrams = findDiagramsForView(
rendererOutputs,
diagramsDir,
docsDir,
'SystemContext',
ctx.log,
);
const containerDiagrams = findDiagramsForView(
rendererOutputs,
diagramsDir,
docsDir,
'Container',
ctx.log,
);
const componentDiagrams = findDiagramsForView(
rendererOutputs,
diagramsDir,
docsDir,
'Component',
ctx.log,
);
// Render system page
ctx.log.info('Generating system overview page...');
const systemPageContent = env.render('system.md.njk', {
system: ir.system,
actors: ir.actors,
containers: ir.containers,
components: ir.components,
systemDiagrams,
containerDiagrams,
componentDiagrams,
});
const systemPagePath = path.join(docsDir, 'README.md');
fs.writeFileSync(systemPagePath, systemPageContent, 'utf8');
ctx.log.info(`Generated README.md`);
const generatedFiles: string[] = ['README.md'];
// Render container pages
ctx.log.info(`Generating ${ir.containers.length} container page(s)...`);
for (const container of ir.containers) {
const containerComponents = ir.components.filter(
(c) => c.containerId === container.id,
);
// Find component diagrams for this container
const containerComponentDiagrams = findDiagramsForContainer(
rendererOutputs,
diagramsDir,
docsDir,
container,
);
const containerPageContent = env.render('container.md.njk', {
container,
system: ir.system,
components: containerComponents,
containerDiagrams,
componentDiagrams: containerComponentDiagrams,
});
const filename = `${sanitizeFileName(container.id)}.md`;
const containerPagePath = path.join(docsDir, filename);
fs.writeFileSync(containerPagePath, containerPageContent, 'utf8');
ctx.log.debug(` • ${filename}`);
generatedFiles.push(filename);
}
ctx.log.info(`Generated ${ir.containers.length} container page(s)`);
// Render component pages
ctx.log.info(`Generating ${ir.components.length} component page(s)...`);
for (const component of ir.components) {
const componentPageContent = env.render('component.md.njk', {
component,
system: ir.system,
container: ir.containers.find((c) => c.id === component.containerId),
codeItems: ir.code.filter((item) => item.componentId === component.id),
codeDiagrams: findClassDiagramsForComponent(
rendererOutputs,
diagramsDir,
docsDir,
component,
),
});
const filename = `${sanitizeFileName(component.id)}.md`;
const componentPagePath = path.join(docsDir, filename);
fs.writeFileSync(componentPagePath, componentPageContent, 'utf8');
ctx.log.debug(` • ${filename}`);
generatedFiles.push(filename);
}
ctx.log.info(`Generated ${ir.components.length} component page(s)`);
// Update pipeline state
if (!ctx.state.docOutputs) {
ctx.state.docOutputs = [];
}
ctx.state.docOutputs.push({
generator: 'markdown-docs',
format: 'markdown',
directory: docsDir,
files: generatedFiles,
timestamp: Date.now(),
});
ctx.log.info('Markdown Docs: completed');
}
/**
* Find diagram files for a specific view type
*/
function findDiagramsForView(
rendererOutputs: RendererOutput[],
diagramsDir: string,
docsDir: string,
viewType: string,
log?: Logger,
): string[] {
const diagrams: string[] = [];
log?.debug(
`Searching for diagrams using diagramsDir: ${diagramsDir}, docsDir: ${docsDir}, view type: ${viewType}`,
);
for (const output of rendererOutputs) {
if (output.format === 'png' || output.format === 'svg') {
for (const file of output.files) {
const ext = path.extname(file);
const filename = path.basename(file, ext);
if (filename.includes(viewType) && !filename.includes('-key')) {
const fullPath = path.join(diagramsDir, file);
log?.debug('Checking for diagram file:', fullPath);
if (fs.existsSync(fullPath)) {
diagrams.push(path.relative(docsDir, fullPath));
}
}
}
}
}
return diagrams;
}
/**
* Find component diagrams for a specific container
*/
function findDiagramsForContainer(
rendererOutputs: RendererOutput[],
diagramsDir: string,
docsDir: string,
container: { id: string; name: string },
): string[] {
const diagrams: string[] = [];
// Sanitize container NAME same way as generator does (not ID)
// The DSL generator uses container.name to create the view name
const sanitizedContainerName = container.name.replace(/[^a-zA-Z0-9_]/g, '_');
for (const output of rendererOutputs) {
if (output.format === 'png' || output.format === 'svg') {
for (const file of output.files) {
const ext = path.extname(file);
const filename = path.basename(file, ext);
// Look for component view diagrams for this specific container
// Format: structurizr-Components_{sanitized-container-name}
if (
filename.includes('Component') &&
filename.includes(sanitizedContainerName) &&
!filename.includes('Classes') &&
!filename.includes('-key')
) {
const fullPath = path.join(diagramsDir, file);
if (fs.existsSync(fullPath)) {
diagrams.push(path.relative(docsDir, fullPath));
}
}
}
}
}
return diagrams;
}
/**
* Find class diagrams for a specific component
*/
function findClassDiagramsForComponent(
rendererOutputs: RendererOutput[],
diagramsDir: string,
docsDir: string,
component: Component,
): string[] {
const diagrams: string[] = [];
// Sanitize component ID same way as generator does
// (replaces non-alphanumeric except underscore with underscore)
const sanitizedComponentId = component.id.replace(/[^a-zA-Z0-9_]/g, '_');
for (const output of rendererOutputs) {
if (output.format === 'png' || output.format === 'svg') {
for (const file of output.files) {
const ext = path.extname(file);
const filename = path.basename(file, ext);
// Look for class diagrams for this specific component
// Format: structurizr-Classes_{sanitized-component-id}
if (
filename.includes('Classes') &&
filename.includes(sanitizedComponentId) &&
!filename.includes('-key')
) {
const fullPath = path.join(diagramsDir, file);
if (fs.existsSync(fullPath)) {
diagrams.push(path.relative(docsDir, fullPath));
}
}
}
}
}
return diagrams;
}
function sanitizeFileName(name: string): string {
// Remove or replace characters not allowed in Windows or Linux filenames
// Windows: \ / : * ? " < > |
// Linux: /
return name
.replace(/[\\/:*?"<>|]/g, '-') // Replace invalid characters with hyphen
.replace(/^\.+/, '') // Remove leading dots
.replace(/\s+/g, '-') // Replace spaces with hyphen
.replace(/-+/g, '-') // Collapse multiple hyphens
.replace(/^-+|-+$/g, ''); // Trim leading/trailing hyphens
}