Skip to content

Commit e3be3a1

Browse files
style: auto-fix linting issues
1 parent 20656e6 commit e3be3a1

File tree

1 file changed

+44
-72
lines changed

1 file changed

+44
-72
lines changed

src/commands/toc.ts

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ export interface TocResult {
7070
/**
7171
* Generate and insert table of contents into markdown files.
7272
*
73-
* Analyzes markdown files to extract headings and generates a formatted table of contents
74-
* that can be inserted at various positions within the file.
73+
* Analyzes markdown files to extract headings and generates a formatted table of contents that can
74+
* be inserted at various positions within the file.
7575
*
7676
* @example
7777
* Basic TOC generation
7878
* ```typescript
7979
* const result = await generateToc(['README.md'], {
80-
* position: 'after-title',
81-
* minDepth: 2,
82-
* maxDepth: 4
80+
* position: 'after-title',
81+
* minDepth: 2,
82+
* maxDepth: 4
8383
* });
8484
*
8585
* console.log(`Added TOC to ${result.filesModified} files`);
@@ -89,9 +89,9 @@ export interface TocResult {
8989
* Replace existing TOC
9090
* ```typescript
9191
* const result = await generateToc(['docs/*.md'], {
92-
* position: 'replace',
93-
* marker: '<!-- TOC -->',
94-
* skipEmpty: true
92+
* position: 'replace',
93+
* marker: '<!-- TOC -->',
94+
* skipEmpty: true
9595
* });
9696
* ```
9797
*
@@ -168,15 +168,15 @@ export async function generateToc(
168168

169169
// Generate TOC markdown
170170
const tocMarkdown = generateTocMarkdown(tocResult.toc, opts.title, opts.headingLevel);
171-
171+
172172
// Insert TOC into content
173173
const modifiedContent = insertTocIntoContent(content, tocMarkdown, opts);
174174

175175
// Write file if not dry run and content changed
176176
if (!opts.dryRun && modifiedContent !== content) {
177177
await writeFile(filePath, modifiedContent, 'utf-8');
178178
result.filesModified++;
179-
179+
180180
if (opts.verbose) {
181181
console.log(` ✅ TOC added/updated`);
182182
}
@@ -196,7 +196,6 @@ export async function generateToc(
196196
tocLength: tocResult.toc.length,
197197
position: opts.position,
198198
});
199-
200199
} catch (error) {
201200
result.fileErrors.push({
202201
file: filePath,
@@ -213,17 +212,13 @@ export async function generateToc(
213212
return result;
214213
}
215214

216-
/**
217-
* Generate formatted TOC markdown with title and heading level.
218-
*/
215+
/** Generate formatted TOC markdown with title and heading level. */
219216
function generateTocMarkdown(toc: string, title: string, headingLevel: number): string {
220217
const headingPrefix = '#'.repeat(headingLevel);
221218
return `${headingPrefix} ${title}\n\n${toc}`;
222219
}
223220

224-
/**
225-
* Insert TOC into content at the specified position.
226-
*/
221+
/** Insert TOC into content at the specified position. */
227222
function insertTocIntoContent(
228223
content: string,
229224
tocMarkdown: string,
@@ -249,12 +244,10 @@ function insertTocIntoContent(
249244
}
250245
}
251246

252-
/**
253-
* Insert TOC after the first heading (title).
254-
*/
247+
/** Insert TOC after the first heading (title). */
255248
function insertAfterTitle(lines: string[], tocMarkdown: string): string {
256-
const titleIndex = lines.findIndex(line => line.trim().startsWith('#'));
257-
249+
const titleIndex = lines.findIndex((line) => line.trim().startsWith('#'));
250+
258251
if (titleIndex === -1) {
259252
// No title found, insert at top
260253
return `${tocMarkdown}\n\n${lines.join('\n')}`;
@@ -269,19 +262,11 @@ function insertAfterTitle(lines: string[], tocMarkdown: string): string {
269262
// Insert TOC
270263
const before = lines.slice(0, insertIndex);
271264
const after = lines.slice(insertIndex);
272-
273-
return [
274-
...before,
275-
'',
276-
tocMarkdown,
277-
'',
278-
...after
279-
].join('\n');
265+
266+
return [...before, '', tocMarkdown, '', ...after].join('\n');
280267
}
281268

282-
/**
283-
* Insert TOC before the main content (after frontmatter if present).
284-
*/
269+
/** Insert TOC before the main content (after frontmatter if present). */
285270
function insertBeforeContent(lines: string[], tocMarkdown: string): string {
286271
let insertIndex = 0;
287272

@@ -304,26 +289,22 @@ function insertBeforeContent(lines: string[], tocMarkdown: string): string {
304289
// Insert TOC
305290
const before = lines.slice(0, insertIndex);
306291
const after = lines.slice(insertIndex);
307-
308-
return [
309-
...before,
310-
tocMarkdown,
311-
'',
312-
...after
313-
].join('\n');
292+
293+
return [...before, tocMarkdown, '', ...after].join('\n');
314294
}
315295

316-
/**
317-
* Replace existing TOC using marker or heuristic detection.
318-
*/
296+
/** Replace existing TOC using marker or heuristic detection. */
319297
function replaceExistingToc(
320298
content: string,
321299
tocMarkdown: string,
322300
options: Required<TocOperationOptions>
323301
): string {
324302
// Try marker-based replacement first
325303
if (options.marker) {
326-
const markerRegex = new RegExp(`${escapeRegExp(options.marker)}[\\s\\S]*?${escapeRegExp(options.marker)}`, 'g');
304+
const markerRegex = new RegExp(
305+
`${escapeRegExp(options.marker)}[\\s\\S]*?${escapeRegExp(options.marker)}`,
306+
'g'
307+
);
327308
if (markerRegex.test(content)) {
328309
return content.replace(markerRegex, `${options.marker}\n${tocMarkdown}\n${options.marker}`);
329310
}
@@ -332,24 +313,24 @@ function replaceExistingToc(
332313
// Try to detect existing TOC by looking for "Table of Contents" heading
333314
const tocHeadingRegex = /^#{1,6}\s+table\s+of\s+contents\s*$/im;
334315
const match = content.match(tocHeadingRegex);
335-
316+
336317
if (match) {
337318
const lines = content.split('\n');
338-
const tocLineIndex = lines.findIndex(line => tocHeadingRegex.test(line));
339-
319+
const tocLineIndex = lines.findIndex((line) => tocHeadingRegex.test(line));
320+
340321
if (tocLineIndex !== -1) {
341322
// Find the end of the TOC (next heading or two consecutive empty lines)
342323
let endIndex = tocLineIndex + 1;
343324
let emptyLineCount = 0;
344-
325+
345326
while (endIndex < lines.length) {
346327
const line = lines[endIndex];
347-
328+
348329
// If we hit another heading, that's the end
349330
if (line.trim().startsWith('#')) {
350331
break;
351332
}
352-
333+
353334
// Count empty lines
354335
if (line.trim() === '') {
355336
emptyLineCount++;
@@ -359,39 +340,32 @@ function replaceExistingToc(
359340
} else {
360341
emptyLineCount = 0;
361342
}
362-
343+
363344
endIndex++;
364345
}
365-
346+
366347
// Replace the TOC section
367348
const before = lines.slice(0, tocLineIndex);
368349
const after = lines.slice(endIndex);
369-
370-
return [
371-
...before,
372-
tocMarkdown,
373-
'',
374-
...after
375-
].join('\n');
350+
351+
return [...before, tocMarkdown, '', ...after].join('\n');
376352
}
377353
}
378354

379355
// If no existing TOC found, insert after title
380356
return insertAfterTitle(content.split('\n'), tocMarkdown);
381357
}
382358

383-
/**
384-
* Escape special regex characters.
385-
*/
359+
/** Escape special regex characters. */
386360
function escapeRegExp(string: string): string {
387361
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
388362
}
389363

390364
/**
391365
* CLI command handler for TOC operations.
392366
*
393-
* Processes markdown files to generate and insert table of contents. Supports various
394-
* positioning options and customization.
367+
* Processes markdown files to generate and insert table of contents. Supports various positioning
368+
* options and customization.
395369
*
396370
* @example
397371
* ```bash
@@ -403,15 +377,12 @@ function escapeRegExp(string: string): string {
403377
*
404378
* # Replace existing TOC using marker
405379
* markmv toc file.md --position replace --marker "<!-- TOC -->"
406-
* ```
380+
* ```;
407381
*
408382
* @param filePaths - Array of file paths to process
409383
* @param cliOptions - CLI-specific options
410384
*/
411-
export async function tocCommand(
412-
filePaths: string[],
413-
cliOptions: TocCliOptions
414-
): Promise<void> {
385+
export async function tocCommand(filePaths: string[], cliOptions: TocCliOptions): Promise<void> {
415386
// Validate position option
416387
const validPositions: readonly TocOperationOptions['position'][] = [
417388
'top',
@@ -466,7 +437,9 @@ export async function tocCommand(
466437
for (const detail of result.fileDetails) {
467438
const status = detail.tocGenerated ? '✅' : '⏭️';
468439
console.log(` ${status} ${detail.file}`);
469-
console.log(` Headings: ${detail.headingsFound}, TOC lines: ${detail.tocLength}, Position: ${detail.position}`);
440+
console.log(
441+
` Headings: ${detail.headingsFound}, TOC lines: ${detail.tocLength}, Position: ${detail.position}`
442+
);
470443
}
471444
}
472445

@@ -475,9 +448,8 @@ export async function tocCommand(
475448
} else if (result.filesModified > 0) {
476449
console.log(`✅ Successfully added/updated TOC in ${result.filesModified} files`);
477450
}
478-
479451
} catch (error) {
480452
console.error('TOC generation failed:', error);
481453
process.exitCode = 1;
482454
}
483-
}
455+
}

0 commit comments

Comments
 (0)