-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
136 lines (114 loc) · 4.28 KB
/
Copy pathindex.ts
File metadata and controls
136 lines (114 loc) · 4.28 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
#!/usr/bin/env node
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { Command } from 'commander';
import { exportPng } from '../exporter/export-png.js';
import { createCodeHighlighter } from '../highlighter/create-highlighter.js';
import { highlightCode } from '../highlighter/highlight-code.js';
import { extractCodeBlocks } from '../parser/extract-code-blocks.js';
import { calculateLayout } from '../renderer/layout.js';
import { renderCardSvg } from '../renderer/render-card.js';
import type { CliOptions, CliRunSummary, RenderOptions } from '../types/index.js';
import { ensureDir } from '../utils/ensure-dir.js';
import { buildOutputFileName } from '../utils/file-naming.js';
type RunCliOptions = {
cwd?: string;
exportPngOverride?: typeof exportPng;
};
export async function runCli(argv: string[], overrides: RunCliOptions = {}): Promise<CliRunSummary> {
const cwd = overrides.cwd ?? process.cwd();
const cliOptions = parseCliArgs(argv, cwd);
const markdown = await readFile(cliOptions.input, 'utf8');
const blocks = await extractCodeBlocks(markdown);
const highlighter = await createCodeHighlighter(cliOptions.theme);
const renderOptions = toRenderOptions(cliOptions);
const writePng = overrides.exportPngOverride ?? exportPng;
await ensureDir(cliOptions.out);
const summary: CliRunSummary = {
processed: blocks.length,
succeeded: 0,
failed: 0,
errors: [],
};
for (const block of blocks) {
const outputPath = path.join(cliOptions.out, buildOutputFileName(block.index, block.lang));
try {
const highlighted = await highlightCode(highlighter, block.code, block.lang, cliOptions.theme);
const layout = calculateLayout(highlighted, renderOptions);
const svg = await renderCardSvg(highlighted, layout, renderOptions);
await writePng({
svg,
outputPath,
width: layout.width,
height: layout.height,
scale: cliOptions.scale,
});
summary.succeeded += 1;
} catch (error) {
summary.failed += 1;
summary.errors.push({
index: block.index,
message: error instanceof Error ? error.message : String(error),
});
}
}
return summary;
}
function parseCliArgs(argv: string[], cwd: string): CliOptions {
const program = new Command();
program
.name('md-code-image')
.argument('<input>')
.option('--out <path>', 'Output directory', './output')
.option('--theme <theme>', 'Shiki theme name', 'github-dark')
.option('--line-numbers', 'Render line numbers', false)
.option('--window <style>', 'Window chrome style', 'none')
.option('--background <mode>', 'Background mode', 'solid')
.option('--padding <number>', 'Card padding', '24')
.option('--radius <number>', 'Card radius', '12')
.option('--scale <number>', 'PNG scale factor', '2')
.option('--max-width <number>', 'Maximum card width', '800');
program.parse(['node', 'md-code-image', ...argv], { from: 'node' });
const input = program.args[0];
const options = program.opts();
return {
input: path.resolve(cwd, input),
out: path.resolve(cwd, options.out),
theme: options.theme,
lineNumbers: Boolean(options.lineNumbers),
window: options.window,
background: options.background,
padding: Number(options.padding),
radius: Number(options.radius),
scale: Number(options.scale),
maxWidth: Number(options.maxWidth),
} as CliOptions;
}
function toRenderOptions(options: CliOptions): RenderOptions {
return {
theme: options.theme,
lineNumbers: options.lineNumbers,
window: options.window,
background: options.background,
padding: options.padding,
radius: options.radius,
maxWidth: options.maxWidth,
fontFamily: 'JetBrains Mono',
fontSize: 16,
lineHeight: 24,
showFilename: false,
};
}
export async function main(argv = process.argv.slice(2)): Promise<void> {
const summary = await runCli(argv);
process.stdout.write(`Processed ${summary.processed} code blocks\n`);
process.stdout.write(`Succeeded: ${summary.succeeded}\n`);
process.stdout.write(`Failed: ${summary.failed}\n`);
if (summary.failed > 0) {
process.exitCode = 1;
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
void main();
}