xonecas.io
6533
Yo

yoctomarkdown / files

yoctomarkdown / src / index.ts

7.9 KB
1import stripAnsi from "strip-ansi";
2import stringWidth from "string-width";
3import { type Theme, themes } from "./themes";
4import { type InlineSpan, parseLineToken } from "./tokenizer";
5
6export type { InlineSpan, BlockToken, TokenHighlighter } from "./tokenizer";
7export { tokenize, createTokenizer } from "./tokenizer";
8
9export interface Options {
10  theme?: "16" | "256" | "truecolor" | "minimal" | "none" | Theme;
11  tabWidth?: number;
12  wordWrap?: number | "auto" | 0;
13  yieldEvery?: number;
14}
15
16export interface Highlighter {
17  write(chunk: string | Uint8Array): string;
18  end(): string;
19}
20
21function resolveColumns(options?: Options): number {
22  const wrap = options?.wordWrap ?? "auto";
23  if (typeof wrap === "number" && wrap > 0) return wrap;
24  if (wrap === "auto") {
25    try {
26      return process.stdout.columns || 80;
27    } catch {
28      return 80;
29    }
30  }
31  // wordWrap === 0 (disabled) – use a large value so visual-line count stays 1
32  return Number.MAX_SAFE_INTEGER;
33}
34
35function resolveTheme(options?: Options): Theme {
36  const t = options?.theme || "16";
37  if (typeof t === "string") {
38    return (themes[t] || themes.none) as Theme;
39  }
40  return t as Theme;
41}
42
43// ── Token → ANSI rendering ──────────────────────────────────────────
44
45function renderSpans(spans: InlineSpan[], theme: Theme): string {
46  let result = "";
47  for (const span of spans) {
48    switch (span.type) {
49      case "text":
50        result += span.content;
51        break;
52      case "bold":
53        result += theme.bold(`**${span.content}**`);
54        break;
55      case "italic":
56        result += theme.italic(`*${span.content}*`);
57        break;
58      case "code":
59        result += theme.code(`\`${span.content}\``);
60        break;
61      case "link":
62        result += theme.link(`[${span.text}](${span.url})`);
63        break;
64    }
65  }
66  return result;
67}
68
69function renderLine(
70  line: string,
71  inCodeBlock: boolean,
72  isPartial: boolean,
73  theme: Theme,
74): { rendered: string; newCodeBlockState: boolean } {
75  // Inside a code block — render as code
76  if (inCodeBlock) {
77    const closing = line.trim().startsWith("```");
78    return {
79      rendered: theme.codeBlock(line),
80      newCodeBlockState: !(closing && !isPartial),
81    };
82  }
83
84  // Code block opening fence
85  if (line.trim().startsWith("```")) {
86    return {
87      rendered: theme.codeBlock(line),
88      newCodeBlockState: !isPartial,
89    };
90  }
91
92  // For non-code lines, tokenize and render
93  const token = parseLineToken(line, false);
94  let rendered: string;
95  switch (token.type) {
96    case "hr":
97      rendered = theme.hr(line);
98      break;
99    case "heading": {
100      const hashes = "#".repeat(token.depth);
101      rendered = theme.heading(`${hashes} ${renderSpans(token.spans, theme)}`);
102      break;
103    }
104    case "blockquote": {
105      const bqMatch = line.match(/^(>\s+)/);
106      rendered =
107        theme.blockquote(bqMatch?.[1] ?? "> ") +
108        renderSpans(token.spans, theme);
109      break;
110    }
111    case "list_item": {
112      const markerMatch = line.match(/^((?:[-*]|\d+\.)\s+)/);
113      rendered =
114        theme.listMarker(markerMatch?.[1] ?? "") +
115        renderSpans(token.spans, theme);
116      break;
117    }
118    case "blank":
119      rendered = "";
120      break;
121    case "paragraph":
122      rendered = renderSpans(token.spans, theme);
123      break;
124    default:
125      rendered = line;
126      break;
127  }
128
129  return { rendered, newCodeBlockState: false };
130}
131
132// ── Public API ──────────────────────────────────────────────────────
133
134export function highlightSync(input: string, options?: Options): string {
135  const highlighter = createHighlighter(options);
136  const out = highlighter.write(input);
137  return out + highlighter.end();
138}
139
140export function createHighlighter(options?: Options): Highlighter {
141  const theme = resolveTheme(options);
142  const terminalColumns = resolveColumns(options);
143  const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
144  let buffer = "";
145  let inCodeBlock = false;
146
147  function parseLine(line: string, isPartial: boolean): string {
148    const { rendered, newCodeBlockState } = renderLine(
149      line,
150      inCodeBlock,
151      isPartial,
152      theme,
153    );
154    inCodeBlock = newCodeBlockState;
155    return rendered;
156  }
157
158  let previousPartialRendered = "";
159
160  // Compute actual terminal line count and last-line column,
161  // simulating how the terminal wraps wide characters.
162  // A 2-column char at the last column causes a gap + wrap.
163  function terminalLayout(s: string): { lines: number; lastCol: number } {
164    const stripped = stripAnsi(s);
165    if (stripped.length === 0) return { lines: 1, lastCol: 0 };
166    let lines = 1;
167    let col = 0;
168    for (const { segment } of segmenter.segment(stripped)) {
169      const w = stringWidth(segment);
170      if (w === 0) continue;
171      if (col + w > terminalColumns) {
172        lines++;
173        col = w;
174      } else {
175        col += w;
176      }
177    }
178    return { lines, lastCol: col };
179  }
180
181  // Erase the previous partial render by moving back over exactly the
182  // characters we wrote and clearing from cursor to end of screen.
183  // This preserves any text before our output on the same terminal line
184  // (e.g. a prompt prefix), unlike \x1b[2K which erases the whole line.
185  function erasePreviousPartial(): string {
186    if (previousPartialRendered.length === 0) {
187      return "";
188    }
189    const { lines, lastCol } = terminalLayout(previousPartialRendered);
190    let seq = "";
191    // Move cursor up for any extra wrapped lines beyond the first
192    if (lines > 1) {
193      seq += `\x1b[${lines - 1}A`;
194    }
195    // Move cursor back to start of our partial output
196    if (lastCol > 0) {
197      seq += `\x1b[${lastCol}D`;
198    }
199    // Erase from cursor to end of screen
200    seq += "\x1b[0J";
201    return seq;
202  }
203
204  return {
205    write(chunk: string | Uint8Array): string {
206      const text =
207        typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk);
208      buffer += text;
209
210      const lines = buffer.split("\n");
211      buffer = lines.pop() ?? "";
212
213      let out = "";
214
215      // If we have completed lines, we must erase the previous partial
216      // (which was the in-progress version of the first completed line)
217      // and re-render all completed lines plus the new partial.
218      if (lines.length > 0) {
219        out += erasePreviousPartial();
220        out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
221        previousPartialRendered = "";
222      }
223
224      if (buffer.length > 0) {
225        const rendered = parseLine(buffer, true);
226        // Append-only optimisation: if the new render starts with the
227        // previous render, just emit the new suffix — no erase needed.
228        // This avoids flicker and sidesteps character-width measurement
229        // for the common case of appending plain text.
230        if (
231          lines.length === 0 &&
232          previousPartialRendered.length > 0 &&
233          rendered.startsWith(previousPartialRendered)
234        ) {
235          out += rendered.slice(previousPartialRendered.length);
236        } else {
237          // Render changed (e.g. inline formatting closed) — full redraw
238          if (lines.length === 0) {
239            out += erasePreviousPartial();
240          }
241          out += rendered;
242        }
243        previousPartialRendered = rendered;
244      } else {
245        previousPartialRendered = "";
246      }
247
248      return out;
249    },
250    end(): string {
251      if (buffer.length > 0) {
252        const rendered = parseLine(buffer, false);
253        buffer = "";
254        // If the final render matches the partial, nothing to redo.
255        // If it differs (e.g. unclosed bold emitted as plain), erase and redraw.
256        let out = "";
257        if (rendered !== previousPartialRendered) {
258          out += erasePreviousPartial();
259          out += rendered;
260        }
261        previousPartialRendered = "";
262        return out;
263      }
264      return "";
265    },
266  };
267}