@chromamark/cli 0.1.0
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/LICENSE.md +15 -0
- package/README.md +41 -0
- package/bin/chromamark.js +7 -0
- package/package.json +47 -0
- package/src/cli.js +137 -0
- package/src/index.js +81 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Copyright 2026 CJ Fravel
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
2. **SaaS Provision**: If you use this software to provide a Software-as-a-Service (SaaS) offering, you must provide access to the source code of the modified version of the Software that you are using in your service, under the terms of this license. The access must be provided within a reasonable time, but no later than 30 days, and the access method should be publicly documented.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Third-party components
|
|
14
|
+
|
|
15
|
+
The published browser bundles in `packages/renderer/dist/` and the bundled VS Code extension embed [markdown-it](https://github.com/markdown-it/markdown-it), which is licensed under the [MIT License](https://github.com/markdown-it/markdown-it/blob/master/LICENSE).
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @chromamark/cli
|
|
2
|
+
|
|
3
|
+
Compile [ChromaMark](https://github.com/cjfravel-dev/ChromaMark) (`.cm`) files
|
|
4
|
+
into **self-contained HTML** — the theme is inlined, so there's no CDN or build
|
|
5
|
+
step to deploy.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g @chromamark/cli
|
|
11
|
+
# or run without installing:
|
|
12
|
+
npx @chromamark/cli build report.cm
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
chromamark build report.cm # → report.html (self-contained)
|
|
19
|
+
chromamark build report.cm -o out.html # explicit output
|
|
20
|
+
chromamark build docs/ -o site/ # every .cm in a tree → site/*.html
|
|
21
|
+
chromamark report.cm --stdout > out.html # write to stdout
|
|
22
|
+
cat report.cm | chromamark - # read from stdin
|
|
23
|
+
chromamark build report.cm --watch # rebuild on change
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Options: `-o/--output`, `--stdout`, `--title <text>`, `--watch`, `-h/--help`,
|
|
27
|
+
`-v/--version`.
|
|
28
|
+
|
|
29
|
+
## Programmatic
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { compile, render } from '@chromamark/cli';
|
|
33
|
+
|
|
34
|
+
const page = compile('::: success\nAll good [!pass]\n:::', { title: 'Report' });
|
|
35
|
+
// → complete <!DOCTYPE html> document with the theme inlined
|
|
36
|
+
const fragment = render('[!pass]'); // just the HTML fragment
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
Modified MIT License with a SaaS source-availability provision — see LICENSE.md.
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chromamark/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ChromaMark command-line compiler — turn .cm files into self-contained HTML (theme inlined, no CDN).",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
|
+
"author": "cjfravel-dev",
|
|
7
|
+
"homepage": "https://github.com/cjfravel-dev/ChromaMark#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/cjfravel-dev/ChromaMark.git",
|
|
11
|
+
"directory": "packages/cli"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/cjfravel-dev/ChromaMark/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"bin": {
|
|
24
|
+
"chromamark": "./bin/chromamark.js"
|
|
25
|
+
},
|
|
26
|
+
"main": "./src/index.js",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./src/index.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"bin/",
|
|
32
|
+
"src/"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node --test"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"chromamark",
|
|
39
|
+
"markdown",
|
|
40
|
+
"cli",
|
|
41
|
+
"static-site",
|
|
42
|
+
"html"
|
|
43
|
+
],
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@chromamark/renderer": "^0.1.0"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChromaMark CLI argument handling: build single files, directories, stdin,
|
|
3
|
+
* with optional watch mode.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
readFileSync, writeFileSync, statSync, readdirSync, mkdirSync, watch,
|
|
8
|
+
} from 'node:fs';
|
|
9
|
+
import { join, basename, extname, dirname, relative } from 'node:path';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { compile } from './index.js';
|
|
12
|
+
|
|
13
|
+
const HELP = `chromamark — compile ChromaMark (.cm) to self-contained HTML
|
|
14
|
+
|
|
15
|
+
Usage:
|
|
16
|
+
chromamark [build] <input.cm> [-o <output.html>] compile one file
|
|
17
|
+
chromamark [build] <dir> -o <outdir> compile every .cm in a tree
|
|
18
|
+
chromamark <input.cm> --stdout write HTML to stdout
|
|
19
|
+
cat file.cm | chromamark - read from stdin
|
|
20
|
+
|
|
21
|
+
Options:
|
|
22
|
+
-o, --output <path> output file or directory
|
|
23
|
+
--stdout write HTML to stdout instead of a file
|
|
24
|
+
--title <text> page title (default: derived from the file name)
|
|
25
|
+
--watch rebuild when inputs change
|
|
26
|
+
-h, --help show this help
|
|
27
|
+
-v, --version print the version
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
function version() {
|
|
31
|
+
const url = new URL('../package.json', import.meta.url);
|
|
32
|
+
return JSON.parse(readFileSync(fileURLToPath(url), 'utf8')).version;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseArgs(argv) {
|
|
36
|
+
const opts = { input: null, output: null, stdout: false, title: null, watch: false };
|
|
37
|
+
const rest = argv[0] === 'build' ? argv.slice(1) : argv.slice(0);
|
|
38
|
+
for (let i = 0; i < rest.length; i++) {
|
|
39
|
+
const a = rest[i];
|
|
40
|
+
if (a === '-h' || a === '--help') opts.help = true;
|
|
41
|
+
else if (a === '-v' || a === '--version') opts.version = true;
|
|
42
|
+
else if (a === '--stdout') opts.stdout = true;
|
|
43
|
+
else if (a === '--watch') opts.watch = true;
|
|
44
|
+
else if (a === '-o' || a === '--output') opts.output = rest[++i];
|
|
45
|
+
else if (a === '--title') opts.title = rest[++i];
|
|
46
|
+
else if (a === '-') opts.input = '-';
|
|
47
|
+
else if (a.startsWith('-')) throw new Error(`unknown option: ${a}`);
|
|
48
|
+
else if (opts.input == null) opts.input = a;
|
|
49
|
+
}
|
|
50
|
+
return opts;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function titleFor(file) {
|
|
54
|
+
return basename(file, extname(file));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function listCmFiles(dir) {
|
|
58
|
+
const out = [];
|
|
59
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
60
|
+
const full = join(dir, entry.name);
|
|
61
|
+
if (entry.isDirectory()) out.push(...listCmFiles(full));
|
|
62
|
+
else if (entry.isFile() && extname(entry.name) === '.cm') out.push(full);
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function buildFile(input, output, opts, log) {
|
|
68
|
+
const html = compile(readFileSync(input, 'utf8'), { title: opts.title || titleFor(input) });
|
|
69
|
+
if (opts.stdout) {
|
|
70
|
+
process.stdout.write(html);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const target = output || input.replace(/\.cm$/, '') + '.html';
|
|
74
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
75
|
+
writeFileSync(target, html);
|
|
76
|
+
if (log) process.stderr.write(`built ${target}\n`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function buildDir(input, outdir) {
|
|
80
|
+
if (!outdir) throw new Error('building a directory requires -o <outdir>');
|
|
81
|
+
const files = listCmFiles(input);
|
|
82
|
+
for (const file of files) {
|
|
83
|
+
const rel = relative(input, file).replace(/\.cm$/, '') + '.html';
|
|
84
|
+
const target = join(outdir, rel);
|
|
85
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
86
|
+
writeFileSync(target, compile(readFileSync(file, 'utf8'), { title: titleFor(file) }));
|
|
87
|
+
}
|
|
88
|
+
process.stderr.write(`built ${files.length} file(s) into ${outdir}\n`);
|
|
89
|
+
return files;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function run(argv = process.argv.slice(2)) {
|
|
93
|
+
let opts;
|
|
94
|
+
try {
|
|
95
|
+
opts = parseArgs(argv);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
process.stderr.write(`${err.message}\n\n${HELP}`);
|
|
98
|
+
return 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (opts.help) { process.stdout.write(HELP); return 0; }
|
|
102
|
+
if (opts.version) { process.stdout.write(`${version()}\n`); return 0; }
|
|
103
|
+
|
|
104
|
+
if (opts.input === '-' || (opts.input == null && !process.stdin.isTTY)) {
|
|
105
|
+
process.stdout.write(compile(readFileSync(0, 'utf8'), { title: opts.title || 'ChromaMark' }));
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
if (opts.input == null) { process.stderr.write(HELP); return 1; }
|
|
109
|
+
|
|
110
|
+
let info;
|
|
111
|
+
const rebuild = () => {
|
|
112
|
+
if (info.isDirectory()) buildDir(opts.input, opts.output);
|
|
113
|
+
else buildFile(opts.input, opts.output, opts, true);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
info = statSync(opts.input);
|
|
118
|
+
rebuild();
|
|
119
|
+
} catch (err) {
|
|
120
|
+
process.stderr.write(`error: ${err.message}\n`);
|
|
121
|
+
return 1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (opts.watch && !opts.stdout) {
|
|
125
|
+
process.stderr.write('watching for changes… (Ctrl+C to stop)\n');
|
|
126
|
+
const onChange = () => {
|
|
127
|
+
try { rebuild(); } catch (err) { process.stderr.write(`error: ${err.message}\n`); }
|
|
128
|
+
};
|
|
129
|
+
try {
|
|
130
|
+
watch(opts.input, { recursive: info.isDirectory() }, onChange);
|
|
131
|
+
} catch {
|
|
132
|
+
watch(opts.input, onChange); // non-recursive fallback (older Node / platforms)
|
|
133
|
+
}
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChromaMark CLI core: compile a ChromaMark string into a self-contained HTML
|
|
3
|
+
* page (theme inlined, no CDN). Also re-exports the fragment renderer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import { render as renderFragment } from '@chromamark/renderer';
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
|
|
12
|
+
/** Render ChromaMark to an HTML fragment (no page chrome). */
|
|
13
|
+
export const render = renderFragment;
|
|
14
|
+
|
|
15
|
+
let cachedTheme;
|
|
16
|
+
|
|
17
|
+
/** The ChromaMark theme stylesheet, read from @chromamark/renderer. */
|
|
18
|
+
export function theme() {
|
|
19
|
+
if (cachedTheme === undefined) {
|
|
20
|
+
cachedTheme = readFileSync(require.resolve('@chromamark/renderer/theme.css'), 'utf8');
|
|
21
|
+
}
|
|
22
|
+
return cachedTheme;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function escapeHtml(text) {
|
|
26
|
+
return String(text).replace(/[&<>"]/g, (c) => (
|
|
27
|
+
{ '&': '&', '<': '<', '>': '>', '"': '"' }[c]
|
|
28
|
+
));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const BASE_CSS = `
|
|
32
|
+
body { max-width: 1040px; margin: 0 auto; padding: 28px 20px 96px; line-height: 1.55;
|
|
33
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
|
|
34
|
+
background: var(--pg-bg); color: var(--pg-fg); }
|
|
35
|
+
:root { --pg-bg:#fff; --pg-fg:#1f2328; --pg-panel:#f6f8fa; --pg-line:#d0d7de; }
|
|
36
|
+
[data-theme="dark"] { --pg-bg:#0d1117; --pg-fg:#e6edf3; --pg-panel:#161b22; --pg-line:#30363d; }
|
|
37
|
+
@media (prefers-color-scheme: dark){ :root:not([data-theme="light"]){
|
|
38
|
+
--pg-bg:#0d1117; --pg-fg:#e6edf3; --pg-panel:#161b22; --pg-line:#30363d; } }
|
|
39
|
+
h2 { margin-top: 40px; padding-bottom: 6px; border-bottom: 1px solid var(--pg-line); }
|
|
40
|
+
code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em;
|
|
41
|
+
background: var(--pg-panel); border: 1px solid var(--pg-line); border-radius: 6px; padding: .1em .35em; }
|
|
42
|
+
pre { background: var(--pg-panel); border: 1px solid var(--pg-line); border-radius: 8px; padding: 12px 14px; overflow: auto; }
|
|
43
|
+
pre code { background: none; border: none; padding: 0; }
|
|
44
|
+
table { border-collapse: collapse; } th, td { border: 1px solid var(--pg-line); padding: 6px 10px; }
|
|
45
|
+
thead th { background: var(--pg-panel); }
|
|
46
|
+
blockquote { border-left: 3px solid var(--pg-line); margin: 12px 0; padding: 0 14px; color: #8b949e; }
|
|
47
|
+
.cm-toggle { position: fixed; top: 12px; right: 12px; cursor: pointer; border: 1px solid var(--pg-line);
|
|
48
|
+
background: var(--pg-panel); color: var(--pg-fg); border-radius: 8px; padding: 6px 10px; font-size: 13px; }
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
const TOGGLE_JS =
|
|
52
|
+
"document.querySelector('.cm-toggle').addEventListener('click',function(){" +
|
|
53
|
+
"var r=document.documentElement;r.setAttribute('data-theme'," +
|
|
54
|
+
"r.getAttribute('data-theme')==='dark'?'light':'dark');});";
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Compile ChromaMark source into a complete, self-contained HTML document.
|
|
58
|
+
* @param {string} src ChromaMark source
|
|
59
|
+
* @param {{title?:string, theme?:string, rendererOptions?:object}} [options]
|
|
60
|
+
*/
|
|
61
|
+
export function compile(src, options = {}) {
|
|
62
|
+
const title = escapeHtml(options.title || 'ChromaMark');
|
|
63
|
+
const body = renderFragment(String(src ?? ''), options.rendererOptions);
|
|
64
|
+
const css = options.theme != null ? options.theme : theme();
|
|
65
|
+
return `<!DOCTYPE html>
|
|
66
|
+
<html lang="en">
|
|
67
|
+
<head>
|
|
68
|
+
<meta charset="utf-8">
|
|
69
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
70
|
+
<title>${title}</title>
|
|
71
|
+
<style>${BASE_CSS}</style>
|
|
72
|
+
<style>${css}</style>
|
|
73
|
+
</head>
|
|
74
|
+
<body>
|
|
75
|
+
<button class="cm-toggle" type="button" title="Toggle theme">\u25d0 Theme</button>
|
|
76
|
+
${body}
|
|
77
|
+
<script>${TOGGLE_JS}</script>
|
|
78
|
+
</body>
|
|
79
|
+
</html>
|
|
80
|
+
`;
|
|
81
|
+
}
|