md4x 0.0.20 → 0.0.21
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/build/md4x.darwin-arm64.node +0 -0
- package/build/md4x.darwin-x64.node +0 -0
- package/build/md4x.linux-arm.node +0 -0
- package/build/md4x.linux-arm64-musl.node +0 -0
- package/build/md4x.linux-arm64.node +0 -0
- package/build/md4x.linux-x64-musl.node +0 -0
- package/build/md4x.linux-x64.node +0 -0
- package/build/md4x.wasm +0 -0
- package/build/md4x.win32-arm64.node +0 -0
- package/build/md4x.win32-x64.node +0 -0
- package/lib/_shared.mjs +66 -7
- package/lib/cli.mjs +9 -1
- package/lib/napi.d.mts +4 -4
- package/lib/napi.mjs +16 -3
- package/lib/types.d.mts +34 -0
- package/lib/wasm/common.mjs +42 -22
- package/lib/wasm/index.d.mts +3 -4
- package/package.json +1 -1
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/build/md4x.wasm
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/_shared.mjs
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
const decoder = new TextDecoder();
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
function parseCodeMeta(bytes, extraFields) {
|
|
4
4
|
const nullIdx = bytes.indexOf(0);
|
|
5
5
|
if (nullIdx === -1) {
|
|
6
|
-
return {
|
|
6
|
+
return { output: decoder.decode(bytes), codeBlocks: [] };
|
|
7
7
|
}
|
|
8
|
-
const
|
|
8
|
+
const outBytes = bytes.subarray(0, nullIdx);
|
|
9
9
|
const metaBytes = bytes.subarray(nullIdx + 1);
|
|
10
|
-
const
|
|
10
|
+
const output = decoder.decode(outBytes);
|
|
11
11
|
const meta = JSON.parse(decoder.decode(metaBytes));
|
|
12
12
|
const codeBlocks = meta.map((m) => {
|
|
13
|
-
const start = decoder.decode(
|
|
14
|
-
const end = decoder.decode(
|
|
13
|
+
const start = decoder.decode(outBytes.subarray(0, m.s)).length;
|
|
14
|
+
const end = decoder.decode(outBytes.subarray(0, m.e)).length;
|
|
15
15
|
const block = { start, end, lang: m.l || "" };
|
|
16
16
|
if (m.f) block.filename = m.f;
|
|
17
17
|
if (m.h) block.highlights = m.h;
|
|
18
|
+
if (extraFields) extraFields(block, m);
|
|
18
19
|
return block;
|
|
19
20
|
});
|
|
20
|
-
return {
|
|
21
|
+
return { output, codeBlocks };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseHtmlMeta(bytes) {
|
|
25
|
+
const { output, codeBlocks } = parseCodeMeta(bytes);
|
|
26
|
+
return { html: output, codeBlocks };
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
export function parseHtmlWithHighlighting(bytes, highlighter) {
|
|
@@ -54,3 +60,56 @@ function unescapeHtml(str) {
|
|
|
54
60
|
.replaceAll(">", ">")
|
|
55
61
|
.replaceAll(""", '"');
|
|
56
62
|
}
|
|
63
|
+
|
|
64
|
+
const DIM = "\x1b[2m";
|
|
65
|
+
const DIM_OFF = "\x1b[22m";
|
|
66
|
+
|
|
67
|
+
export function parseAnsiMeta(bytes) {
|
|
68
|
+
const { output, codeBlocks } = parseCodeMeta(bytes, (block, m) => {
|
|
69
|
+
if (m.i) block.prefix = m.i;
|
|
70
|
+
});
|
|
71
|
+
return { ansi: output, codeBlocks };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseAnsiWithHighlighting(bytes, highlighter) {
|
|
75
|
+
const { ansi, codeBlocks } = parseAnsiMeta(bytes);
|
|
76
|
+
if (codeBlocks.length === 0) return ansi;
|
|
77
|
+
let out = "";
|
|
78
|
+
let pos = 0;
|
|
79
|
+
for (const block of codeBlocks) {
|
|
80
|
+
const region = ansi.slice(block.start, block.end);
|
|
81
|
+
const prefix = block.prefix || " ";
|
|
82
|
+
// Strip DIM wrapper and extract raw code by removing prefix from each line
|
|
83
|
+
let inner = region;
|
|
84
|
+
if (inner.startsWith(DIM)) inner = inner.slice(DIM.length);
|
|
85
|
+
if (inner.endsWith(DIM_OFF)) inner = inner.slice(0, -DIM_OFF.length);
|
|
86
|
+
const code = inner
|
|
87
|
+
.split("\n")
|
|
88
|
+
.filter((l) => l.length > 0)
|
|
89
|
+
.map((l) => (l.startsWith(prefix) ? l.slice(prefix.length) : l))
|
|
90
|
+
.join("\n");
|
|
91
|
+
const highlighted = highlighter(code, block);
|
|
92
|
+
if (highlighted === undefined) {
|
|
93
|
+
out += ansi.slice(pos, block.end);
|
|
94
|
+
} else {
|
|
95
|
+
out += ansi.slice(pos, block.start);
|
|
96
|
+
// Wrap each line with the indent prefix
|
|
97
|
+
const lines = highlighted.split("\n");
|
|
98
|
+
for (let i = 0; i < lines.length; i++) {
|
|
99
|
+
if (lines[i].length > 0) {
|
|
100
|
+
out += prefix + lines[i];
|
|
101
|
+
}
|
|
102
|
+
if (i < lines.length - 1) {
|
|
103
|
+
out += "\n";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Ensure trailing newline
|
|
107
|
+
if (!highlighted.endsWith("\n")) out += "\n";
|
|
108
|
+
pos = block.end;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
pos = block.end;
|
|
112
|
+
}
|
|
113
|
+
out += ansi.slice(pos);
|
|
114
|
+
return out;
|
|
115
|
+
}
|
package/lib/cli.mjs
CHANGED
|
@@ -24,6 +24,8 @@ const { values, positionals } = parseArgs({
|
|
|
24
24
|
"html-title": { type: "string" },
|
|
25
25
|
"html-css": { type: "string" },
|
|
26
26
|
heal: { type: "boolean", default: false },
|
|
27
|
+
"show-urls": { type: "boolean", default: false },
|
|
28
|
+
"show-frontmatter": { type: "boolean", default: false },
|
|
27
29
|
stat: { type: "boolean", short: "s", default: false },
|
|
28
30
|
help: { type: "boolean", short: "h", default: false },
|
|
29
31
|
version: { type: "boolean", short: "v", default: false },
|
|
@@ -47,6 +49,8 @@ ${_g("General options:")}
|
|
|
47
49
|
${_c("-o")}, ${_c("--output")}=${_d("FILE")} Output file ${_d("(default: stdout)")}
|
|
48
50
|
${_c("-t")}, ${_c("--format")}=${_d("FORMAT")} Output format: ${_c("html")}, ${_c("text")}, ${_c("ast")}, ${_c("ansi")}, ${_c("meta")}, ${_c("heal")} ${_d("(default: ansi for TTY, text otherwise)")}
|
|
49
51
|
${_c("--heal")} Heal incomplete markdown before rendering
|
|
52
|
+
${_c("--show-urls")} Show link URLs after link text ${_d("(ANSI only)")}
|
|
53
|
+
${_c("--show-frontmatter")} Show frontmatter content ${_d("(ANSI only)")}
|
|
50
54
|
${_c("-s")}, ${_c("--stat")} Measure parsing time
|
|
51
55
|
${_c("-h")}, ${_c("--help")} Display this help and exit
|
|
52
56
|
${_c("-v")}, ${_c("--version")} Display version and exit
|
|
@@ -165,7 +169,11 @@ switch (format) {
|
|
|
165
169
|
output = renderToHtml(input, healOpt);
|
|
166
170
|
break;
|
|
167
171
|
case "ansi":
|
|
168
|
-
output = renderToAnsi(input,
|
|
172
|
+
output = renderToAnsi(input, {
|
|
173
|
+
...healOpt,
|
|
174
|
+
showUrls: values["show-urls"],
|
|
175
|
+
showFrontmatter: values["show-frontmatter"],
|
|
176
|
+
});
|
|
169
177
|
break;
|
|
170
178
|
case "text":
|
|
171
179
|
output = renderToText(input, healOpt);
|
package/lib/napi.d.mts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
ComarkTree,
|
|
3
3
|
ComarkMeta,
|
|
4
4
|
HtmlOptions,
|
|
5
|
+
AnsiOptions,
|
|
5
6
|
RenderOptions,
|
|
6
7
|
} from "./types.mjs";
|
|
7
8
|
|
|
@@ -14,6 +15,7 @@ export type {
|
|
|
14
15
|
ComarkHeading,
|
|
15
16
|
ComarkMeta,
|
|
16
17
|
HtmlOptions,
|
|
18
|
+
AnsiOptions,
|
|
17
19
|
RenderOptions,
|
|
18
20
|
} from "./types.mjs";
|
|
19
21
|
|
|
@@ -24,6 +26,7 @@ export interface NAPIBinding {
|
|
|
24
26
|
renderToHtmlMeta(input: string): Buffer;
|
|
25
27
|
renderToAST(input: string, flags?: number): string;
|
|
26
28
|
renderToAnsi(input: string, flags?: number): string;
|
|
29
|
+
renderToAnsiMeta(input: string): Buffer;
|
|
27
30
|
renderToMeta(input: string, flags?: number): string;
|
|
28
31
|
renderToText(input: string, flags?: number): string;
|
|
29
32
|
heal(input: string): string;
|
|
@@ -43,10 +46,7 @@ export declare function parseAST(
|
|
|
43
46
|
input: string,
|
|
44
47
|
opts?: RenderOptions,
|
|
45
48
|
): ComarkTree;
|
|
46
|
-
export declare function renderToAnsi(
|
|
47
|
-
input: string,
|
|
48
|
-
opts?: RenderOptions,
|
|
49
|
-
): string;
|
|
49
|
+
export declare function renderToAnsi(input: string, opts?: AnsiOptions): string;
|
|
50
50
|
export declare function renderToMeta(
|
|
51
51
|
input: string,
|
|
52
52
|
opts?: RenderOptions,
|
package/lib/napi.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
parseHtmlWithHighlighting,
|
|
3
|
+
parseAnsiWithHighlighting,
|
|
4
|
+
} from "./_shared.mjs";
|
|
2
5
|
|
|
3
6
|
// --- internal ---
|
|
4
7
|
|
|
@@ -62,8 +65,18 @@ export function parseAST(input, opts) {
|
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
export function renderToAnsi(input, opts) {
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
let flags = opts?.heal ? HEAL_FLAG : 0;
|
|
69
|
+
if (opts?.showUrls) flags |= 0x0010;
|
|
70
|
+
if (opts?.showFrontmatter) flags |= 0x0020;
|
|
71
|
+
if (!opts?.highlighter) {
|
|
72
|
+
return getBinding().renderToAnsi(str(input), flags);
|
|
73
|
+
}
|
|
74
|
+
const s = opts?.heal ? getBinding().heal(str(input)) : str(input);
|
|
75
|
+
const buf = getBinding().renderToAnsiMeta(s);
|
|
76
|
+
return parseAnsiWithHighlighting(
|
|
77
|
+
new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength),
|
|
78
|
+
opts.highlighter,
|
|
79
|
+
);
|
|
67
80
|
}
|
|
68
81
|
|
|
69
82
|
export function renderToMeta(input, opts) {
|
package/lib/types.d.mts
CHANGED
|
@@ -34,6 +34,18 @@ export interface RenderOptions {
|
|
|
34
34
|
heal?: boolean;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
export interface AnsiOptions extends RenderOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Custom highlighter function for fenced code blocks. If provided, code blocks
|
|
40
|
+
* are passed through this callback which can return custom ANSI-highlighted output.
|
|
41
|
+
*/
|
|
42
|
+
highlighter?: AnsiCodeBlockHighlighter;
|
|
43
|
+
/** Show link URLs after link text (e.g. `text (url)`). Default: false (links are clickable via OSC 8). */
|
|
44
|
+
showUrls?: boolean;
|
|
45
|
+
/** Show frontmatter content as dim text. Default: false (frontmatter is suppressed). */
|
|
46
|
+
showFrontmatter?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
37
49
|
export interface HtmlOptions extends RenderOptions {
|
|
38
50
|
/** Generate a full HTML document with `<!DOCTYPE html>`, `<head>`, and `<body>`. */
|
|
39
51
|
full?: boolean;
|
|
@@ -70,3 +82,25 @@ export interface HtmlWithCodeBlocks {
|
|
|
70
82
|
/** Metadata for each fenced code block in document order */
|
|
71
83
|
codeBlocks: CodeBlock[];
|
|
72
84
|
}
|
|
85
|
+
|
|
86
|
+
export interface AnsiCodeBlock {
|
|
87
|
+
/** Character offset in ANSI string where code block starts (including DIM escape) */
|
|
88
|
+
start: number;
|
|
89
|
+
/** Character offset in ANSI string where code block ends (including DIM_OFF escape) */
|
|
90
|
+
end: number;
|
|
91
|
+
/** Language identifier (empty string if none) */
|
|
92
|
+
lang: string;
|
|
93
|
+
/** Filename from `[filename]` syntax */
|
|
94
|
+
filename?: string;
|
|
95
|
+
/** Highlighted line numbers from `{1-3,5}` syntax */
|
|
96
|
+
highlights?: number[];
|
|
97
|
+
/** Line indent prefix (includes ANSI escapes for colored bars in nested contexts) */
|
|
98
|
+
prefix?: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type AnsiCodeBlockHighlighter = (
|
|
102
|
+
/** Raw code content (indentation stripped) */
|
|
103
|
+
code: string,
|
|
104
|
+
/** Code block metadata (lang, filename, highlights, offsets) */
|
|
105
|
+
block: AnsiCodeBlock,
|
|
106
|
+
) => string | undefined;
|
package/lib/wasm/common.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
parseHtmlWithHighlighting,
|
|
3
|
+
parseAnsiWithHighlighting,
|
|
4
|
+
} from "../_shared.mjs";
|
|
2
5
|
|
|
3
6
|
// --- internal ---
|
|
4
7
|
|
|
@@ -57,27 +60,14 @@ function render(exports, fn, input, ...extra) {
|
|
|
57
60
|
return result;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (opts?.heal) flags |= HEAL_FLAG;
|
|
65
|
-
const exports = _getExports();
|
|
66
|
-
if (!opts?.highlighter) {
|
|
67
|
-
return render(exports, exports.md4x_to_html, input, flags);
|
|
68
|
-
}
|
|
69
|
-
const {
|
|
70
|
-
memory,
|
|
71
|
-
md4x_alloc,
|
|
72
|
-
md4x_free,
|
|
73
|
-
md4x_to_html_meta,
|
|
74
|
-
md4x_result_ptr,
|
|
75
|
-
md4x_result_size,
|
|
76
|
-
} = exports;
|
|
63
|
+
/* Render with a meta function, returning raw bytes for highlighter processing. */
|
|
64
|
+
function renderMetaBytes(exports, metaFn, input) {
|
|
65
|
+
const { memory, md4x_alloc, md4x_free, md4x_result_ptr, md4x_result_size } =
|
|
66
|
+
exports;
|
|
77
67
|
const encoded = new TextEncoder().encode(str(input));
|
|
78
68
|
const ptr = md4x_alloc(encoded.length);
|
|
79
69
|
new Uint8Array(memory.buffer).set(encoded, ptr);
|
|
80
|
-
const ret =
|
|
70
|
+
const ret = metaFn(ptr, encoded.length);
|
|
81
71
|
md4x_free(ptr);
|
|
82
72
|
if (ret !== 0) {
|
|
83
73
|
throw new Error("md4x: render failed");
|
|
@@ -85,8 +75,25 @@ export function renderToHtml(input, opts) {
|
|
|
85
75
|
const outPtr = md4x_result_ptr();
|
|
86
76
|
const outSize = md4x_result_size();
|
|
87
77
|
const bytes = new Uint8Array(memory.buffer, outPtr, outSize);
|
|
78
|
+
return { bytes, outPtr };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const HEAL_FLAG = 0x0100;
|
|
82
|
+
|
|
83
|
+
export function renderToHtml(input, opts) {
|
|
84
|
+
let flags = opts?.full ? 0x0008 : 0;
|
|
85
|
+
if (opts?.heal) flags |= HEAL_FLAG;
|
|
86
|
+
const exports = _getExports();
|
|
87
|
+
if (!opts?.highlighter) {
|
|
88
|
+
return render(exports, exports.md4x_to_html, input, flags);
|
|
89
|
+
}
|
|
90
|
+
const { bytes, outPtr } = renderMetaBytes(
|
|
91
|
+
exports,
|
|
92
|
+
exports.md4x_to_html_meta,
|
|
93
|
+
input,
|
|
94
|
+
);
|
|
88
95
|
const result = parseHtmlWithHighlighting(bytes, opts.highlighter);
|
|
89
|
-
md4x_free(outPtr);
|
|
96
|
+
exports.md4x_free(outPtr);
|
|
90
97
|
return result;
|
|
91
98
|
}
|
|
92
99
|
|
|
@@ -101,9 +108,22 @@ export function parseAST(input, opts) {
|
|
|
101
108
|
}
|
|
102
109
|
|
|
103
110
|
export function renderToAnsi(input, opts) {
|
|
104
|
-
|
|
111
|
+
let flags = opts?.heal ? HEAL_FLAG : 0;
|
|
112
|
+
if (opts?.showUrls) flags |= 0x0010;
|
|
113
|
+
if (opts?.showFrontmatter) flags |= 0x0020;
|
|
105
114
|
const exports = _getExports();
|
|
106
|
-
|
|
115
|
+
if (!opts?.highlighter) {
|
|
116
|
+
return render(exports, exports.md4x_to_ansi, input, flags);
|
|
117
|
+
}
|
|
118
|
+
const s = opts?.heal ? heal(str(input)) : str(input);
|
|
119
|
+
const { bytes, outPtr } = renderMetaBytes(
|
|
120
|
+
exports,
|
|
121
|
+
exports.md4x_to_ansi_meta,
|
|
122
|
+
s,
|
|
123
|
+
);
|
|
124
|
+
const result = parseAnsiWithHighlighting(bytes, opts.highlighter);
|
|
125
|
+
exports.md4x_free(outPtr);
|
|
126
|
+
return result;
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
export function renderToMeta(input, opts) {
|
package/lib/wasm/index.d.mts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
ComarkTree,
|
|
3
3
|
ComarkMeta,
|
|
4
4
|
HtmlOptions,
|
|
5
|
+
AnsiOptions,
|
|
5
6
|
RenderOptions,
|
|
6
7
|
} from "../types.mjs";
|
|
7
8
|
|
|
@@ -14,6 +15,7 @@ export type {
|
|
|
14
15
|
ComarkHeading,
|
|
15
16
|
ComarkMeta,
|
|
16
17
|
HtmlOptions,
|
|
18
|
+
AnsiOptions,
|
|
17
19
|
RenderOptions,
|
|
18
20
|
} from "../types.mjs";
|
|
19
21
|
|
|
@@ -36,10 +38,7 @@ export declare function parseAST(
|
|
|
36
38
|
input: string,
|
|
37
39
|
opts?: RenderOptions,
|
|
38
40
|
): ComarkTree;
|
|
39
|
-
export declare function renderToAnsi(
|
|
40
|
-
input: string,
|
|
41
|
-
opts?: RenderOptions,
|
|
42
|
-
): string;
|
|
41
|
+
export declare function renderToAnsi(input: string, opts?: AnsiOptions): string;
|
|
43
42
|
export declare function renderToMeta(
|
|
44
43
|
input: string,
|
|
45
44
|
opts?: RenderOptions,
|