markdansi 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -1,14 +1,12 @@
1
- # Markdansi
1
+ # 🎨 Markdansi: Wraps, colors, links—no baggage.
2
+ ![npm](https://img.shields.io/npm/v/markdansi) ![license MIT](https://img.shields.io/badge/license-MIT-blue.svg) ![node >=22](https://img.shields.io/badge/node-%3E%3D22-brightgreen) ![tests vitest](https://img.shields.io/badge/tests-vitest-blue?logo=vitest)
2
3
 
3
- 🎨 Markdansi: Wraps, colors, links—no baggage.
4
- ![npm unpublished](https://img.shields.io/badge/npm-unpublished-lightgrey) ![license MIT](https://img.shields.io/badge/license-MIT-blue.svg) ![node >=22](https://img.shields.io/badge/node-%3E%3D22-brightgreen) ![tests vitest](https://img.shields.io/badge/tests-vitest-blue?logo=vitest)
4
+ Tiny, dependency-light Markdown → ANSI renderer and CLI for modern Node (>=22). Focuses on readable terminal output with sensible wrapping, GFM support (tables, task lists, strikethrough), optional OSC‑8 hyperlinks, and zero built‑in syntax highlighting (pluggable hook). Written in TypeScript, ships ESM.
5
5
 
6
- Tiny, dependency-light Markdown → ANSI renderer and CLI for modern Node (>=22). Focuses on readable terminal output with sensible wrapping, GFM support (tables, task lists, strikethrough), optional OSC‑8 hyperlinks, and zero built‑in syntax highlighting (pluggable hook).
6
+ Published on npm as `markdansi`.
7
7
 
8
8
  ## Install
9
9
 
10
- > Not yet published to npm (name available as of November 16, 2025). Install from git or local path until released.
11
-
12
10
  ```bash
13
11
  pnpm add markdansi
14
12
  # or
@@ -60,21 +58,26 @@ console.log(custom('`inline`\n\n```\nblock code\n```'));
60
58
  - `width`: used only when `wrap===true`; default TTY columns or 80.
61
59
  - `color` (default TTY): `false` removes all ANSI/OSC.
62
60
  - `hyperlinks` (default auto): enable/disable OSC‑8 links.
63
- - `theme`: `default | dim | bright` or custom theme object.
61
+ - `theme`: `default | dim | bright | solarized | monochrome | contrast` or custom theme object.
64
62
  - `listIndent`: spaces per nesting level (default 2).
65
63
  - `quotePrefix`: blockquote line prefix (default `│ `).
64
+ - `tableBorder`: `unicode` (default) | `ascii` | `none`.
65
+ - `tablePadding`: spaces inside cells (default 1); `tableDense` drops extra separators.
66
+ - `tableTruncate`: cap cells to column width (default `true`, ellipsis `…`).
67
+ - `codeBox`: draw a box around fenced code (default true); `codeGutter` shows line numbers; `codeWrap` wraps code lines by default.
66
68
  - `highlighter(code, lang)`: optional hook to recolor code blocks; must not add/remove newlines.
67
69
 
68
70
  ## Status
69
71
 
70
- Version: `0.1.0`
72
+ Version: `0.1.2` (released)
71
73
  Tests: `pnpm test`
72
74
  License: MIT
73
75
 
74
76
  ## Notes
75
77
 
76
- - Code blocks never hard‑wrap; long lines may overflow. If `lang` is present, a faint `[lang]` label is shown.
77
- - Tables are ASCII boxed, align using GFM alignment, and wrap cell text on spaces; very long words may overflow.
78
+ - Code blocks wrap to the render width by default; disable with `codeWrap=false`. If `lang` is present, a faint `[lang]` label is shown and boxes use unicode borders.
79
+ - Link/reference definitions that spill their titles onto indented lines are merged back into one line so copied notes don’t turn into boxed code.
80
+ - Tables use unicode borders by default, include padding, respect GFM alignment, and truncate long cells with `…` so layouts stay tidy. Turn off truncation with `tableTruncate=false`.
78
81
  - Tight vs loose lists follow GFM; task items render `[ ]` / `[x]`.
79
82
 
80
83
  See `docs/spec.md` for full behavior details.*** End Patch
package/dist/cli.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+ import type { RenderOptions } from "./types.js";
3
+ type CliArgs = Partial<RenderOptions> & {
4
+ in?: string;
5
+ out?: string;
6
+ help?: boolean;
7
+ };
8
+ /**
9
+ * Ignore EPIPE when downstream (e.g., `head`) closes early.
10
+ */
11
+ export declare function handleStdoutEpipe(): void;
12
+ /**
13
+ * Parse CLI arguments into RenderOptions-ish object (plus in/out paths).
14
+ */
15
+ export declare function parseArgs(argv: string[]): CliArgs;
16
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,166 @@
1
+ #!/usr/bin/env node
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { render } from "./index.js";
6
+ /**
7
+ * Ignore EPIPE when downstream (e.g., `head`) closes early.
8
+ */
9
+ export function handleStdoutEpipe() {
10
+ process.stdout.on("error", (err) => {
11
+ if (err && err.code === "EPIPE") {
12
+ process.exit(0);
13
+ return;
14
+ }
15
+ // For other stdout errors, fail fast but don't throw unhandled.
16
+ console.error(err instanceof Error ? err.message : err);
17
+ process.exit(1);
18
+ });
19
+ }
20
+ /**
21
+ * Parse CLI arguments into RenderOptions-ish object (plus in/out paths).
22
+ */
23
+ export function parseArgs(argv) {
24
+ const args = {};
25
+ for (let i = 2; i < argv.length; i += 1) {
26
+ const a = argv[i];
27
+ if (!a)
28
+ continue;
29
+ if (a === "--no-wrap")
30
+ args.wrap = false;
31
+ else if (a === "--no-color")
32
+ args.color = false;
33
+ else if (a === "--no-links")
34
+ args.hyperlinks = false;
35
+ else if (a === "--code-wrap=false")
36
+ args.codeWrap = false;
37
+ else if (a === "--code-wrap=true")
38
+ args.codeWrap = true;
39
+ else if (a === "--code-box=false")
40
+ args.codeBox = false;
41
+ else if (a === "--code-box=true")
42
+ args.codeBox = true;
43
+ else if (a === "--code-gutter=true")
44
+ args.codeGutter = true;
45
+ else if (a === "--code-gutter=false")
46
+ args.codeGutter = false;
47
+ else if (a.startsWith("--table-border=")) {
48
+ const val = a.split("=")[1];
49
+ if (val === "unicode" || val === "ascii" || val === "none")
50
+ args.tableBorder = val;
51
+ }
52
+ else if (a === "--table-dense")
53
+ args.tableDense = true;
54
+ else if (a === "--table-truncate=false")
55
+ args.tableTruncate = false;
56
+ else if (a === "--table-truncate=true")
57
+ args.tableTruncate = true;
58
+ else if (a === "--table-padding") {
59
+ const next = argv[i + 1];
60
+ if (next)
61
+ args.tablePadding = Number(next);
62
+ i += 1;
63
+ }
64
+ else if (a === "--table-ellipsis") {
65
+ const next = argv[i + 1];
66
+ if (next)
67
+ args.tableEllipsis = next;
68
+ i += 1;
69
+ }
70
+ else if (a === "--in") {
71
+ const next = argv[i + 1];
72
+ if (next)
73
+ args.in = next;
74
+ i += 1;
75
+ }
76
+ else if (a === "--out") {
77
+ const next = argv[i + 1];
78
+ if (next)
79
+ args.out = next;
80
+ i += 1;
81
+ }
82
+ else if (a === "--width") {
83
+ const next = argv[i + 1];
84
+ if (next)
85
+ args.width = Number(next);
86
+ i += 1;
87
+ }
88
+ else if (a.startsWith("--theme=")) {
89
+ const themeVal = a.split("=")[1];
90
+ if (themeVal)
91
+ args.theme = themeVal;
92
+ }
93
+ else if (a === "--list-indent") {
94
+ const next = argv[i + 1];
95
+ if (next)
96
+ args.listIndent = Number(next);
97
+ i += 1;
98
+ }
99
+ else if (a === "--quote-prefix") {
100
+ const next = argv[i + 1];
101
+ if (next)
102
+ args.quotePrefix = next;
103
+ i += 1;
104
+ }
105
+ else if (a === "--help" || a === "-h")
106
+ args.help = true;
107
+ }
108
+ return args;
109
+ }
110
+ /**
111
+ * CLI entrypoint.
112
+ */
113
+ function main() {
114
+ handleStdoutEpipe();
115
+ const args = parseArgs(process.argv);
116
+ if (args.help) {
117
+ process.stdout.write(`markdansi options:
118
+ --in FILE Input file (default: stdin)
119
+ --out FILE Output file (default: stdout)
120
+ --width N Wrap width (default: TTY cols or 80)
121
+ --no-wrap Disable hard wrapping
122
+ --no-color Disable ANSI/OSC output
123
+ --no-links Disable OSC-8 hyperlinks
124
+ --theme NAME Theme (default|dim|bright)
125
+ --list-indent N Spaces per list nesting level (default: 2)
126
+ --quote-prefix STR Prefix for blockquotes (default: "│ ")
127
+ --table-border STR unicode|ascii|none
128
+ --table-padding N Spaces around table cell content
129
+ --table-dense Fewer separator rows
130
+ --table-truncate Default true; pass --table-truncate=false to disable
131
+ --table-ellipsis STR Ellipsis text for truncation
132
+ --code-wrap[=true|false] Wrap code lines (default true)
133
+ --code-box[=true|false] Box code blocks (default true)
134
+ --code-gutter[=true|false] Show code line numbers (default false)
135
+ `);
136
+ process.exit(0);
137
+ }
138
+ const input = args.in && args.in !== "-"
139
+ ? fs.readFileSync(path.resolve(args.in), "utf8")
140
+ : fs.readFileSync(0, "utf8");
141
+ const renderOptions = {
142
+ ...(args.wrap !== undefined ? { wrap: args.wrap } : {}),
143
+ ...(args.width !== undefined ? { width: args.width } : {}),
144
+ ...(args.color !== undefined ? { color: args.color } : {}),
145
+ ...(args.hyperlinks !== undefined ? { hyperlinks: args.hyperlinks } : {}),
146
+ ...(args.theme !== undefined ? { theme: args.theme } : {}),
147
+ ...(args.listIndent !== undefined ? { listIndent: args.listIndent } : {}),
148
+ ...(args.quotePrefix !== undefined
149
+ ? { quotePrefix: args.quotePrefix }
150
+ : {}),
151
+ };
152
+ const output = render(input, renderOptions);
153
+ if (args.out) {
154
+ fs.writeFileSync(path.resolve(args.out), output, "utf8");
155
+ }
156
+ else {
157
+ process.stdout.write(output);
158
+ }
159
+ }
160
+ // Only run the CLI when executed directly, not when imported for tests.
161
+ const entryHref = process.argv[1]
162
+ ? pathToFileURL(process.argv[1]).href
163
+ : undefined;
164
+ if (import.meta.url === entryHref) {
165
+ main();
166
+ }
@@ -0,0 +1,9 @@
1
+ import type { WriteStream } from "node:tty";
2
+ /**
3
+ * Detect OSC-8 hyperlink support for a given stream (defaults to stdout).
4
+ */
5
+ export declare function hyperlinkSupported(stream?: WriteStream): boolean;
6
+ /**
7
+ * Build an OSC-8 hyperlink sequence.
8
+ */
9
+ export declare function osc8(url: string, text: string): string;
@@ -0,0 +1,26 @@
1
+ import supportsHyperlinks from "supports-hyperlinks";
2
+ /**
3
+ * Detect OSC-8 hyperlink support for a given stream (defaults to stdout).
4
+ */
5
+ export function hyperlinkSupported(stream = process.stdout) {
6
+ const helper = supportsHyperlinks;
7
+ try {
8
+ if (typeof supportsHyperlinks === "function") {
9
+ return Boolean(supportsHyperlinks(stream));
10
+ }
11
+ if (helper.stdout)
12
+ return Boolean(helper.stdout(stream));
13
+ if (helper.default && typeof helper.default === "function")
14
+ return Boolean(helper.default(stream));
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ return false;
20
+ }
21
+ /**
22
+ * Build an OSC-8 hyperlink sequence.
23
+ */
24
+ export function osc8(url, text) {
25
+ return `\u001B]8;;${url}\u0007${text}\u001B]8;;\u0007`;
26
+ }
package/dist/index.d.ts CHANGED
@@ -1,46 +1,9 @@
1
- export type ColorName = "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | `#${string}` | `${number}`;
2
- export type StyleIntent = {
3
- color?: ColorName;
4
- bgColor?: ColorName;
5
- bold?: boolean;
6
- italic?: boolean;
7
- underline?: boolean;
8
- dim?: boolean;
9
- strike?: boolean;
10
- };
11
- export type Theme = {
12
- heading?: StyleIntent;
13
- strong?: StyleIntent;
14
- emph?: StyleIntent;
15
- inlineCode?: StyleIntent;
16
- blockCode?: StyleIntent;
17
- code?: StyleIntent;
18
- link?: StyleIntent;
19
- quote?: StyleIntent;
20
- hr?: StyleIntent;
21
- listMarker?: StyleIntent;
22
- tableHeader?: StyleIntent;
23
- tableCell?: StyleIntent;
24
- };
25
- export type ThemeName = "default" | "dim" | "bright";
26
- export type Highlighter = (code: string, lang?: string) => string;
27
- export interface RenderOptions {
28
- wrap?: boolean;
29
- width?: number;
30
- hyperlinks?: boolean;
31
- color?: boolean;
32
- theme?: ThemeName | Theme;
33
- /**
34
- * Spaces per nesting level for lists (default 2).
35
- */
36
- listIndent?: number;
37
- /**
38
- * Prefix used for blockquotes (default "│ ").
39
- */
40
- quotePrefix?: string;
41
- highlighter?: Highlighter;
42
- }
43
- export declare function render(markdown: string, options?: RenderOptions): string;
44
- export declare function createRenderer(options?: RenderOptions): (markdown: string) => string;
1
+ import { createRenderer, render as renderMarkdown } from "./render.js";
2
+ import { themes } from "./theme.js";
3
+ import type { RenderOptions, Theme, ThemeName } from "./types.js";
4
+ export { renderMarkdown as render, createRenderer, themes };
5
+ export type { RenderOptions, Theme, ThemeName };
6
+ /**
7
+ * Render Markdown to plain text (no ANSI, no hyperlinks) while preserving layout/wrapping.
8
+ */
45
9
  export declare function strip(markdown: string, options?: RenderOptions): string;
46
- export declare const themes: Record<ThemeName | string, Theme>;
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import { createRenderer, render as renderMarkdown } from "./render.js";
2
+ import { themes } from "./theme.js";
3
+ export { renderMarkdown as render, createRenderer, themes };
4
+ /**
5
+ * Render Markdown to plain text (no ANSI, no hyperlinks) while preserving layout/wrapping.
6
+ */
7
+ export function strip(markdown, options = {}) {
8
+ return renderMarkdown(markdown, {
9
+ ...options,
10
+ color: false,
11
+ hyperlinks: false,
12
+ // ensure flags like codeWrap/tableTruncate not lost
13
+ wrap: options.wrap ?? true,
14
+ });
15
+ }
@@ -0,0 +1,2 @@
1
+ import type { Root } from "mdast";
2
+ export declare function parse(markdown: string): Root;
@@ -1,10 +1,9 @@
1
1
  import { fromMarkdown } from "mdast-util-from-markdown";
2
2
  import { gfmFromMarkdown } from "mdast-util-gfm";
3
3
  import { gfm as gfmSyntax } from "micromark-extension-gfm";
4
-
5
4
  export function parse(markdown) {
6
- return fromMarkdown(markdown, {
7
- extensions: [gfmSyntax()],
8
- mdastExtensions: [gfmFromMarkdown()],
9
- });
5
+ return fromMarkdown(markdown, {
6
+ extensions: [gfmSyntax()],
7
+ mdastExtensions: [gfmFromMarkdown()],
8
+ });
10
9
  }
@@ -0,0 +1,9 @@
1
+ import type { RenderOptions } from "./types.js";
2
+ /**
3
+ * Render Markdown input to an ANSI string.
4
+ */
5
+ export declare function render(markdown: string, userOptions?: RenderOptions): string;
6
+ /**
7
+ * Create a reusable renderer with fixed options.
8
+ */
9
+ export declare function createRenderer(options?: RenderOptions): (md: string) => string;