-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathassembler.mjs
More file actions
237 lines (203 loc) · 8.12 KB
/
assembler.mjs
File metadata and controls
237 lines (203 loc) · 8.12 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
import {existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync} from 'node:fs';
import {basename, join} from 'node:path';
import process from 'node:process';
import {config} from './config.mjs';
import {logger} from './logger.mjs';
import {parseFrontmatter, slugify, stripFrontmatter, yamlQuote} from './utils.mjs';
const {order: CATEGORY_ORDER, labels: CATEGORY_LABELS} = config.categories;
/**
* Assembles enriched/raw extension docs into the docs-src/ output directory
*/
export class Assembler {
constructor(docsGenDir, outDir) {
this.docsGenDir = docsGenDir;
this.rawDir = join(docsGenDir, 'raw');
this.enrichedDir = join(docsGenDir, 'enriched');
this.irPath = join(docsGenDir, 'extensions.json');
this.outDir = outDir;
this.extensionsOutDir = join(outDir, 'extensions');
}
/**
* Runs the full assembly pipeline
*/
run() {
if (!existsSync(this.rawDir)) {
logger.error(`${this.rawDir} not found. Run extract first.`);
process.exit(1);
}
if (!existsSync(this.outDir)) {
logger.error(`${this.outDir} not found. Run generate first.`);
process.exit(1);
}
const extensions = existsSync(this.irPath)
? JSON.parse(readFileSync(this.irPath, 'utf-8'))
: [];
const version = this.resolveVersion(extensions);
logger.info(`Assembling extension docs for v${version}...`);
const docs = this.collectDocs();
logger.info(
`Found ${docs.size} extension docs (enriched: ${[...docs.values()].filter((d) => d.source === 'enriched').length})`,
);
const pages = this.writePages(docs, extensions);
this.writeIndex(pages, extensions, version);
const tocItems = this.generateTocItems(pages);
this.patchTocYaml(tocItems);
this.patchIndexMd(version);
logger.success(`Assembled ${pages.length} extension pages in ${this.extensionsOutDir}/`);
logger.info('Updated toc.yaml and index.md');
}
/**
* Collects docs preferring enriched over raw
*/
collectDocs() {
const docs = new Map();
if (existsSync(this.rawDir)) {
for (const file of readdirSync(this.rawDir).filter((f) => f.endsWith('.md'))) {
const name = basename(file, '.md');
docs.set(name, {
name,
source: 'raw',
content: readFileSync(join(this.rawDir, file), 'utf-8'),
});
}
}
if (existsSync(this.enrichedDir)) {
for (const file of readdirSync(this.enrichedDir).filter((f) => f.endsWith('.md'))) {
const name = basename(file, '.md');
docs.set(name, {
name,
source: 'enriched',
content: readFileSync(join(this.enrichedDir, file), 'utf-8'),
});
}
}
return docs;
}
/**
* Writes individual extension pages to docs-src/extensions/
*/
writePages(docs, extensions) {
mkdirSync(this.extensionsOutDir, {recursive: true});
const pages = [];
for (const [name, doc] of docs) {
const extInfo = extensions.find((e) => e.name === name);
const category = extInfo?.category || parseFrontmatter(doc.content).category || 'other';
const slug = slugify(name);
writeFileSync(join(this.extensionsOutDir, `${slug}.md`), stripFrontmatter(doc.content));
pages.push({
name,
slug,
category,
relativePath: `extensions/${slug}.md`,
hasNodes: extInfo?.nodes?.length > 0,
hasMarks: extInfo?.marks?.length > 0,
hasActions: extInfo?.actions?.length > 0,
source: doc.source,
});
}
return pages;
}
/**
* Writes the extensions index page with categorized tables
*/
writeIndex(pages, extensions, version) {
const lines = [
'# Extensions Reference',
'',
`Documentation generated for \`@gravity-ui/markdown-editor@${version}\`.`,
'',
];
for (const category of CATEGORY_ORDER) {
const categoryPages = pages
.filter((p) => p.category === category)
.sort((a, b) => a.name.localeCompare(b.name));
if (categoryPages.length === 0) continue;
lines.push(`## ${CATEGORY_LABELS[category]} Extensions`, '');
lines.push('| Extension | Nodes | Marks | Actions |');
lines.push('|-----------|-------|-------|---------|');
for (const page of categoryPages) {
const ext = extensions.find((e) => e.name === page.name);
const nodes = ext?.nodes?.join(', ') || '-';
const marks = ext?.marks?.join(', ') || '-';
const actions = ext?.actions?.length || 0;
lines.push(
`| [${page.name}](${page.relativePath}) | ${nodes} | ${marks} | ${actions} |`,
);
}
lines.push('');
}
writeFileSync(join(this.outDir, 'extensions-index.md'), lines.join('\n'));
}
/**
* Generates YAML toc entries for the extensions section
*/
generateTocItems(pages) {
const lines = [];
lines.push(' - name: Extensions');
lines.push(' href: extensions-index.md');
lines.push(' items:');
for (const category of CATEGORY_ORDER) {
const categoryPages = pages
.filter((p) => p.category === category)
.sort((a, b) => a.name.localeCompare(b.name));
if (categoryPages.length === 0) continue;
lines.push(` - name: ${yamlQuote(CATEGORY_LABELS[category])}`);
lines.push(' items:');
for (const page of categoryPages) {
lines.push(` - name: ${yamlQuote(page.name)}`);
lines.push(` href: ${page.relativePath}`);
}
}
return lines.join('\n');
}
/**
* Patches toc.yaml to include the extensions section
*/
patchTocYaml(extensionsTocItems) {
const tocPath = join(this.outDir, 'toc.yaml');
if (!existsSync(tocPath)) {
logger.warn('toc.yaml not found, creating minimal version');
const content =
[
'title: Markdown Editor',
'href: index.md',
'items:',
' - name: Overview',
' href: index.md',
extensionsTocItems,
].join('\n') + '\n';
writeFileSync(tocPath, content);
return;
}
let content = readFileSync(tocPath, 'utf-8');
// Remove previous Extensions section before appending fresh one
const extSectionRe = /\n {2}- name: Extensions\n[\s\S]*?(?=\n {2}- name:|\n?$)/;
content = content.replace(extSectionRe, '');
content = content.trimEnd() + '\n' + extensionsTocItems + '\n';
writeFileSync(tocPath, content);
}
/**
* Patches index.md to add a link to the extensions reference
*/
patchIndexMd(version) {
const indexPath = join(this.outDir, 'index.md');
if (!existsSync(indexPath)) return;
let content = readFileSync(indexPath, 'utf-8');
content = content.replace(/\n## Extensions[\s\S]*?(?=\n## |\n?$)/, '');
content = content.trimEnd() + '\n\n## Extensions\n\n';
content += `- [Extensions Reference](extensions-index.md) (v${version})\n`;
writeFileSync(indexPath, content);
}
/**
* Reads version from the first extension's raw doc frontmatter
*/
resolveVersion(extensions) {
if (extensions[0]) {
const raw = join(this.rawDir, `${extensions[0].name}.md`);
if (existsSync(raw)) {
return parseFrontmatter(readFileSync(raw, 'utf-8')).version || 'unknown';
}
}
return 'unknown';
}
}