upkram 1.0.6 → 1.0.8

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/cli.js ADDED
@@ -0,0 +1,306 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * UpKram CLI — render Markdown to HTML.
4
+ *
5
+ * upkram input.md > out.html
6
+ * cat input.md | upkram > out.html
7
+ * upkram input.md --preset gfm -o out.html
8
+ * upkram lint input.md [--rules r1,r2] [--json] [--quiet]
9
+ * upkram input.md --check # render + lint; exit 1 on error findings
10
+ *
11
+ * Zero dependencies. Requires Node.js >= 14.
12
+ */
13
+ 'use strict';
14
+
15
+ var fs = require('fs');
16
+ var path = require('path');
17
+ var upkram = require('../upkram-ni.js');
18
+
19
+ var VERSION = upkram && upkram.VERSION ? upkram.VERSION : 'unknown';
20
+
21
+ var HELP = [
22
+ 'UpKram ' + VERSION + ' — Markdown / GFM renderer',
23
+ '',
24
+ 'Usage',
25
+ ' upkram [options] [file] Render Markdown to HTML (stdin when no file)',
26
+ ' upkram lint [options] <file> Lint a Markdown file',
27
+ '',
28
+ 'Options',
29
+ ' -o, --output <file> Write to <file> instead of stdout',
30
+ ' -p, --preset <name> commonmark | gfm-core | gfm (standard-only output)',
31
+ ' --gfm Shorthand for --preset gfm',
32
+ ' --commonmark Shorthand for --preset commonmark',
33
+ ' -s, --standalone Wrap the fragment in a complete HTML document',
34
+ ' --title <text> <title> for --standalone (default: input file name)',
35
+ ' --css <href> Stylesheet link for --standalone',
36
+ ' --check Also lint; exit 1 when any error-level finding appears',
37
+ ' -h, --help Show this help',
38
+ ' -v, --version Print the version',
39
+ '',
40
+ 'Lint subcommand options',
41
+ ' --rules <list> Comma-separated rule ids to run (default: all)',
42
+ ' --json Print diagnostics as JSON',
43
+ ' --quiet Suppress output; exit code still reflects findings',
44
+ '',
45
+ 'Examples',
46
+ ' upkram README.md > README.html',
47
+ ' upkram post.md --preset gfm -s --title "Post" --css upkram-all.css -o post.html',
48
+ ' echo "# hi" | upkram --preset commonmark',
49
+ ' upkram lint README.md --rules ref-invalid-list,qr-sensitive-url',
50
+ ' upkram README.md --check > /dev/null # pre-commit gate',
51
+ ''
52
+ ].join('\n');
53
+
54
+ var LINT_HELP = [
55
+ 'UpKram ' + VERSION + ' — Markdown linter',
56
+ '',
57
+ 'Usage',
58
+ ' upkram lint [options] <file>',
59
+ '',
60
+ ' Reads Markdown from <file> (or stdin with "-") and prints lint findings.',
61
+ ' Exits 1 when any error-level finding exists, 0 otherwise.',
62
+ '',
63
+ 'Options',
64
+ ' --rules <list> Comma-separated rule ids (default: all)',
65
+ ' --json Print diagnostics as JSON',
66
+ ' --quiet Suppress output; exit code still reflects findings',
67
+ ' -h, --help Show this help',
68
+ ''
69
+ ].join('\n');
70
+
71
+ function fail(message) {
72
+ process.stderr.write('upkram: ' + message + '\n');
73
+ process.exitCode = 1;
74
+ }
75
+
76
+ function parseArgs(argv) {
77
+ var opts = {
78
+ file: null,
79
+ output: null,
80
+ preset: null,
81
+ standalone: false,
82
+ title: null,
83
+ css: null,
84
+ check: false
85
+ };
86
+ var rest = [];
87
+
88
+ for (var i = 0; i < argv.length; i++) {
89
+ var arg = argv[i];
90
+ var eq;
91
+ if (arg === '-h' || arg === '--help') {
92
+ opts.help = true;
93
+ } else if (arg === '-v' || arg === '--version') {
94
+ opts.version = true;
95
+ } else if (arg === '-s' || arg === '--standalone') {
96
+ opts.standalone = true;
97
+ } else if (arg === '--check') {
98
+ opts.check = true;
99
+ } else if (arg === '--gfm') {
100
+ opts.preset = 'gfm';
101
+ } else if (arg === '--commonmark') {
102
+ opts.preset = 'commonmark';
103
+ } else if ((eq = arg.indexOf('=')) > 0 && arg.slice(0, 2) === '--') {
104
+ applyValue(opts, arg.slice(0, eq), arg.slice(eq + 1));
105
+ } else if (arg === '-o' || arg === '--output' || arg === '-p' || arg === '--preset' ||
106
+ arg === '--title' || arg === '--css') {
107
+ var value = argv[++i];
108
+ if (value === undefined) throw new Error('missing value for ' + arg);
109
+ applyValue(opts, arg, value);
110
+ } else if (arg === '-') {
111
+ rest.push('-');
112
+ } else if (arg.charAt(0) === '-' && arg !== '-') {
113
+ throw new Error('unknown option ' + arg);
114
+ } else {
115
+ rest.push(arg);
116
+ }
117
+ }
118
+
119
+ if (rest.length > 1) throw new Error('expected at most one input file, got ' + rest.join(' '));
120
+ if (rest.length === 1) opts.file = rest[0];
121
+ return opts;
122
+ }
123
+
124
+ function applyValue(opts, flag, value) {
125
+ if (flag === '-o' || flag === '--output') opts.output = value;
126
+ else if (flag === '-p' || flag === '--preset') opts.preset = value;
127
+ else if (flag === '--title') opts.title = value;
128
+ else if (flag === '--css') opts.css = value;
129
+ else throw new Error('unknown option ' + flag);
130
+ }
131
+
132
+ var PRESETS = { commonmark: 1, 'gfm-core': 1, gfm: 1 };
133
+
134
+ function buildOptions(opts) {
135
+ var config = {};
136
+ if (opts.preset) {
137
+ if (!PRESETS[opts.preset]) {
138
+ throw new Error('unknown preset "' + opts.preset + '" (expected commonmark, gfm-core or gfm)');
139
+ }
140
+ config.preset = opts.preset;
141
+ }
142
+ return config;
143
+ }
144
+
145
+ function readInput(file) {
146
+ if (!file || file === '-') {
147
+ if (process.stdin.isTTY) {
148
+ throw new Error('no input: pass a file path or pipe Markdown into stdin');
149
+ }
150
+ return fs.readFileSync(0, 'utf8');
151
+ }
152
+ return fs.readFileSync(file, 'utf8');
153
+ }
154
+
155
+ function escapeHtml(text) {
156
+ return String(text)
157
+ .replace(/&/g, '&amp;')
158
+ .replace(/</g, '&lt;')
159
+ .replace(/>/g, '&gt;')
160
+ .replace(/"/g, '&quot;');
161
+ }
162
+
163
+ function standalone(body, opts, sourceName) {
164
+ var title = opts.title || (sourceName && sourceName !== '-' ? path.basename(sourceName, path.extname(sourceName)) : 'Untitled');
165
+ var css = opts.css ? ' <link rel="stylesheet" href="' + escapeHtml(opts.css) + '">\n' : '';
166
+ return [
167
+ '<!DOCTYPE html>',
168
+ '<html lang="zh-CN">',
169
+ '<head>',
170
+ ' <meta charset="utf-8">',
171
+ ' <meta name="viewport" content="width=device-width, initial-scale=1">',
172
+ ' <title>' + escapeHtml(title) + '</title>',
173
+ css + '</head>',
174
+ '<body>',
175
+ '<main class="upkram-body">',
176
+ body,
177
+ '</main>',
178
+ '</body>',
179
+ '</html>',
180
+ ''
181
+ ].join('\n');
182
+ }
183
+
184
+ /** Run error-diagnostic gate; write findings to stderr so stdout stays clean HTML. */
185
+ function checkMarkdown(markdown) {
186
+ if (typeof upkram.lintMarkdown !== 'function') return 0;
187
+ var diags;
188
+ try { diags = upkram.lintMarkdown(markdown, {}) || []; }
189
+ catch (e) { process.stderr.write('upkram: lint failed: ' + e.message + '\n'); return 1; }
190
+ var errors = diags.filter(function (d) { return d && d.severity === 'error'; });
191
+ errors.forEach(function (d) {
192
+ var loc = d.position ? d.position.start.line + ':' + d.position.start.column : '-';
193
+ process.stderr.write(loc + '\terror\t' + d.rule + '\t' + d.message + '\n');
194
+ });
195
+ return errors.length ? 1 : 0;
196
+ }
197
+
198
+ /** `upkram lint <file>` — lint diagnostics with exit-code gate. */
199
+ function lintMain(argv) {
200
+ var opts = { file: null, rules: null, json: false, quiet: false, help: false };
201
+ for (var i = 0; i < argv.length; i++) {
202
+ var a = argv[i];
203
+ if (a === '-h' || a === '--help') opts.help = true;
204
+ else if (a === '--json') opts.json = true;
205
+ else if (a === '--quiet') opts.quiet = true;
206
+ else if (a === '-') opts.file = '-';
207
+ else if (a.indexOf('--rules=') === 0) opts.rules = a.slice(8).split(',').filter(Boolean);
208
+ else if (a === '--rules') {
209
+ var v = argv[++i];
210
+ if (v === undefined) { fail('missing value for --rules'); return; }
211
+ opts.rules = v.split(',').filter(Boolean);
212
+ } else if (a.charAt(0) === '-') { fail('unknown lint option ' + a); process.stderr.write('\n' + LINT_HELP); return; }
213
+ else if (opts.file === null) opts.file = a;
214
+ else { fail('expected at most one input file, got ' + a); return; }
215
+ }
216
+
217
+ if (opts.help) { process.stdout.write(LINT_HELP); return; }
218
+ if (!opts.file) { process.stderr.write(LINT_HELP); process.exitCode = 2; return; }
219
+
220
+ var markdown;
221
+ try { markdown = readInput(opts.file); }
222
+ catch (err) { fail(err.message); return; }
223
+
224
+ var diags;
225
+ try {
226
+ diags = upkram.lintMarkdown(markdown, {}, opts.rules ? { rules: opts.rules } : undefined) || [];
227
+ } catch (err) { fail('lint failed: ' + err.message); return; }
228
+
229
+ if (opts.json) console.log(JSON.stringify(diags, null, 2));
230
+ else if (!opts.quiet) {
231
+ diags.forEach(function (d) {
232
+ var loc = d.position ? d.position.start.line + ':' + d.position.start.column : '-';
233
+ console.log(loc + '\t' + d.severity + '\t' + d.rule + '\t' + d.message);
234
+ });
235
+ if (!diags.length) console.log('OK: no lint findings');
236
+ }
237
+ process.exitCode = diags.some(function (d) { return d && d.severity === 'error'; }) ? 1 : 0;
238
+ }
239
+
240
+ function main(argv) {
241
+ // 子命令分流:不触碰原有渲染参数解析,保证渲染行为零回归。
242
+ if (argv[0] === 'lint') { lintMain(argv.slice(1)); return; }
243
+ var opts;
244
+ try {
245
+ opts = parseArgs(argv);
246
+ } catch (err) {
247
+ fail(err.message);
248
+ process.stderr.write('\n' + HELP);
249
+ return;
250
+ }
251
+
252
+ if (opts.help) {
253
+ process.stdout.write(HELP);
254
+ return;
255
+ }
256
+ if (opts.version) {
257
+ process.stdout.write(VERSION + '\n');
258
+ return;
259
+ }
260
+
261
+ var config;
262
+ var markdown;
263
+ try {
264
+ config = buildOptions(opts);
265
+ markdown = readInput(opts.file);
266
+ } catch (err) {
267
+ fail(err.message);
268
+ return;
269
+ }
270
+
271
+ var html;
272
+ try {
273
+ html = upkram.renderMarkdown(markdown, config);
274
+ } catch (err) {
275
+ fail('render failed: ' + (err && err.message ? err.message : String(err)));
276
+ return;
277
+ }
278
+
279
+ if (opts.standalone) html = standalone(html, opts, opts.file);
280
+ if (html.charAt(html.length - 1) !== '\n') html += '\n';
281
+
282
+ if (opts.output) {
283
+ try {
284
+ fs.writeFileSync(opts.output, html, 'utf8');
285
+ } catch (err) {
286
+ fail('cannot write ' + opts.output + ': ' + err.message);
287
+ }
288
+ return;
289
+ }
290
+
291
+ process.stdout.write(html);
292
+ if (opts.check) {
293
+ var rc = checkMarkdown(markdown);
294
+ if (rc) process.exitCode = 1;
295
+ }
296
+ }
297
+
298
+ if (require.main === module) {
299
+ process.stdout.on('error', function (err) {
300
+ if (err && err.code === 'EPIPE') process.exit(0);
301
+ throw err;
302
+ });
303
+ main(process.argv.slice(2));
304
+ }
305
+
306
+ module.exports = { main: main, lintMain: lintMain, parseArgs: parseArgs, VERSION: VERSION };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "upkram",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "A serious Markdown/GFM engine with 100% CommonMark and GFM conformance, plugin API, lint engine and source-positioned AST.",
5
5
  "license": "MIT",
6
6
  "main": "./upkram-ni.js",
7
7
  "browser": "./upkram-all.js",
8
8
  "types": "./upkram.d.ts",
9
9
  "exports": {
10
+ "./package.json": "./package.json",
10
11
  ".": {
11
12
  "types": "./upkram.d.ts",
12
13
  "import": "./upkram.mjs",
@@ -28,6 +29,7 @@
28
29
  "./webfonts/*": "./webfonts/*"
29
30
  },
30
31
  "files": [
32
+ "bin/cli.js",
31
33
  "upkram-ni.js",
32
34
  "upkram-all.js",
33
35
  "upkram-all.css",
@@ -64,13 +66,36 @@
64
66
  "url": "https://github.com/cuizihang1145/UpKram/issues"
65
67
  },
66
68
  "homepage": "https://github.com/cuizihang1145/UpKram#readme",
69
+ "devDependencies": {
70
+ "commonmark": "0.31.2",
71
+ "markdown-it": "15.0.3",
72
+ "marked": "18.3.0",
73
+ "micromark": "4.0.3",
74
+ "parse5": "8.1.1",
75
+ "showdown": "2.1.0",
76
+ "typescript": "5.6.3"
77
+ },
67
78
  "scripts": {
79
+ "test": "node bench/test.js",
80
+ "test:quick": "node bench/test.js --quick",
81
+ "spec:fetch": "node bench/fetch-specs.js",
82
+ "test:types": "tsc --project tsconfig.json",
68
83
  "lint": "node bench/lint.js",
69
84
  "test:spec": "node bench/spec-test.js",
70
85
  "test:gfm": "node bench/gfm-test.js",
71
86
  "test:determinism": "node bench/determinism.js",
72
87
  "test:extra": "node bench/extra.js",
73
88
  "test:diff": "node bench/differential.js",
74
- "report": "node bench/report.js"
89
+ "report": "node bench/report.js",
90
+ "version": "node scripts/version.js --write --stage",
91
+ "version:check": "node scripts/version.js",
92
+ "version:sync": "node scripts/version.js --write",
93
+ "prepack": "node scripts/version.js"
94
+ },
95
+ "engines": {
96
+ "node": ">=14.0.0"
97
+ },
98
+ "bin": {
99
+ "upkram": "./bin/cli.js"
75
100
  }
76
101
  }