|
@@ -1,5 +1,6 @@
|
|
|
import { Marked } from "marked";
|
|
import { Marked } from "marked";
|
|
|
import { markedTerminal } from "marked-terminal";
|
|
import { markedTerminal } from "marked-terminal";
|
|
|
|
|
+import { isAppleTerminal } from "./theme.js";
|
|
|
|
|
|
|
|
const marked = new Marked();
|
|
const marked = new Marked();
|
|
|
marked.use(markedTerminal({
|
|
marked.use(markedTerminal({
|
|
@@ -20,19 +21,38 @@ const BOLD_OFF = "\x1b[22m";
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Post-process marked-terminal output:
|
|
* Post-process marked-terminal output:
|
|
|
- * 1. Replace `* ` bullets with `· `
|
|
|
|
|
- * 2. Convert `**text**` markers to ANSI bold (applied after reflow to avoid code splitting)
|
|
|
|
|
|
|
+ * 1. Strip \x1b[0m (full reset) that overrides Ink's <Text color>
|
|
|
|
|
+ * 2. Replace `* ` bullets with `· `
|
|
|
|
|
+ * 3. Convert `**text**` markers to ANSI bold
|
|
|
*/
|
|
*/
|
|
|
function postProcess(text: string): string {
|
|
function postProcess(text: string): string {
|
|
|
return text
|
|
return text
|
|
|
- // Strip \x1b[0m (full reset) — it overrides the color set by Ink's <Text color>.
|
|
|
|
|
- // Table/block-level ANSI (e.g. [90m for borders, [39m for fg reset) is preserved.
|
|
|
|
|
.replace(/\x1b\[0m/g, "")
|
|
.replace(/\x1b\[0m/g, "")
|
|
|
.replace(/^(\s*)\* /gm, "$1· ")
|
|
.replace(/^(\s*)\* /gm, "$1· ")
|
|
|
.replace(/\*\*(.+?)\*\*/g, `${BOLD_ON}$1${BOLD_OFF}`);
|
|
.replace(/\*\*(.+?)\*\*/g, `${BOLD_ON}$1${BOLD_OFF}`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Lightweight plain-text markdown for Terminal.app.
|
|
|
|
|
+ * Strips all ANSI codes to avoid triggering CoreGraphics crashes
|
|
|
|
|
+ * (Terminal.app misinterprets UTF-8 bytes as color space pointers).
|
|
|
|
|
+ */
|
|
|
|
|
+function renderPlain(text: string): string {
|
|
|
|
|
+ return text
|
|
|
|
|
+ .replace(/^#{1,6}\s+/gm, "")
|
|
|
|
|
+ .replace(/\*\*(.+?)\*\*/g, "$1")
|
|
|
|
|
+ .replace(/\*(.+?)\*/g, "$1")
|
|
|
|
|
+ .replace(/`([^`]+)`/g, "$1")
|
|
|
|
|
+ .replace(/^(\s*)[-*]\s/gm, "$1· ");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function renderMarkdown(text: string): string {
|
|
export function renderMarkdown(text: string): string {
|
|
|
|
|
+ // Terminal.app crashes when ANSI escape codes + CJK text hit its
|
|
|
|
|
+ // CoreGraphics rendering pipeline. Use plain text as a workaround.
|
|
|
|
|
+ if (isAppleTerminal) {
|
|
|
|
|
+ return renderPlain(text);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
const rendered = marked.parse(text);
|
|
const rendered = marked.parse(text);
|
|
|
if (typeof rendered !== "string") {
|
|
if (typeof rendered !== "string") {
|