Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dist/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stripAnsi from "strip-ansi";
import { hyperlinkSupported, osc8 } from "./hyperlink.js";
import { parse } from "./parser.js";
import { convertLatexToUnicode } from "./latex.js";
import { renderDisplayLatex } from "./txm.js";
import { createStyler, themes } from "./theme.js";
import { visibleWidth, wrapText, wrapWithPrefix } from "./wrap.js";
function dedent(markdown) {
Expand Down Expand Up @@ -461,8 +462,8 @@ function renderCodeBlock(node, ctx) {
return [`${top}\n${boxLines.join("\n")}\n${bottom}\n\n`];
}
function renderDisplayMath(node, ctx) {
const converted = convertLatexToUnicode(node.value);
const styled = ctx.style(converted, ctx.options.theme.math || ctx.options.theme.inlineCode);
const rendered = renderDisplayLatex(node.value);
const styled = ctx.style(rendered, ctx.options.theme.math || ctx.options.theme.inlineCode);
const lines = styled.split("\n");
return [`\n${lines.map((l) => ` ${l}`).join("\n")}\n`];
}
Expand Down
3 changes: 3 additions & 0 deletions dist/txm-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
4 changes: 4 additions & 0 deletions dist/txm-wasm/txm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* tslint:disable */
/* eslint-disable */

export function render_latex(latex: string): string;
134 changes: 134 additions & 0 deletions dist/txm-wasm/txm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* @ts-self-types="./txm.d.ts" */

/**
* @param {string} latex
* @returns {string}
*/
function render_latex(latex) {
let deferred3_0;
let deferred3_1;
try {
const ptr0 = passStringToWasm0(latex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.render_latex(ptr0, len0);
var ptr2 = ret[0];
var len2 = ret[1];
if (ret[3]) {
ptr2 = 0; len2 = 0;
throw takeFromExternrefTable0(ret[2]);
}
deferred3_0 = ptr2;
deferred3_1 = len2;
return getStringFromWasm0(ptr2, len2);
} finally {
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
}
}
exports.render_latex = render_latex;

function __wbg_get_imports() {
const import0 = {
__proto__: null,
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
// Cast intrinsic for `Ref(String) -> Externref`.
const ret = getStringFromWasm0(arg0, arg1);
return ret;
},
__wbindgen_init_externref_table: function() {
const table = wasm.__wbindgen_externrefs;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
},
};
return {
__proto__: null,
"./txm_bg.js": import0,
};
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return decodeText(ptr, len);
}

let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8ArrayMemory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = cachedTextEncoder.encodeInto(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
return ptr;
}

function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_externrefs.get(idx);
wasm.__externref_table_dealloc(idx);
return value;
}

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
function decodeText(ptr, len) {
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

const cachedTextEncoder = new TextEncoder();

if (!('encodeInto' in cachedTextEncoder)) {
cachedTextEncoder.encodeInto = function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
};
}

let WASM_VECTOR_LEN = 0;

const wasmPath = `${__dirname}/txm_bg.wasm`;
const wasmBytes = require('fs').readFileSync(wasmPath);
const wasmModule = new WebAssembly.Module(wasmBytes);
let wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
wasm.__wbindgen_start();
Binary file added dist/txm-wasm/txm_bg.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions dist/txm-wasm/txm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export const render_latex: (a: number, b: number) => [number, number, number, number];
export const __wbindgen_externrefs: WebAssembly.Table;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __externref_table_dealloc: (a: number) => void;
export const __wbindgen_free: (a: number, b: number, c: number) => void;
export const __wbindgen_start: () => void;
1 change: 1 addition & 0 deletions dist/txm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function renderDisplayLatex(latex: string): string;
17 changes: 17 additions & 0 deletions dist/txm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createRequire } from "node:module";
import { convertLatexToUnicode } from "./latex.js";
const require = createRequire(import.meta.url);
const txm = require("./txm-wasm/txm.js");
export function renderDisplayLatex(latex) {
try {
return txm
.render_latex(latex)
.trimEnd()
.split("\n")
.map((line) => line.trimEnd())
.join("\n");
}
catch {
return convertLatexToUnicode(latex);
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
"scripts": {
"build": "pnpm lint && pnpm typecheck && pnpm test && pnpm compile",
"clean": "rm -rf dist",
"format": "oxfmt --write .",
"format:check": "oxfmt --check .",
"format": "oxfmt --write src/*.ts test scripts package.json",
"format:check": "oxfmt --check src/*.ts test scripts package.json",
"lint": "rm -rf dist coverage && pnpm format:check && oxlint --deny-warnings src test",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"typecheck": "tsc -p tsconfig.json --noEmit",
"types": "tsc -p tsconfig.json --emitDeclarationOnly",
"compile": "tsc -p tsconfig.json",
"compile": "tsc -p tsconfig.json && node scripts/copy-txm-wasm.mjs",
"markdansi": "tsx src/cli.ts",
"release": "node scripts/release.mjs"
},
Expand Down
9 changes: 9 additions & 0 deletions scripts/copy-txm-wasm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cp } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

await cp(path.join(projectRoot, "src", "txm-wasm"), path.join(projectRoot, "dist", "txm-wasm"), {
recursive: true,
});
5 changes: 3 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { hyperlinkSupported, osc8 } from "./hyperlink.js";
import { parse } from "./parser.js";
import { convertLatexToUnicode } from "./latex.js";
import type { Styler } from "./theme.js";
import { renderDisplayLatex } from "./txm.js";
import { createStyler, themes } from "./theme.js";
import type { RenderOptions, StyleIntent, Theme } from "./types.js";
import { visibleWidth, wrapText, wrapWithPrefix } from "./wrap.js";
Expand Down Expand Up @@ -590,8 +591,8 @@ function renderCodeBlock(node: Code, ctx: RenderContext): string[] {
}

function renderDisplayMath(node: { value: string }, ctx: RenderContext): string[] {
const converted = convertLatexToUnicode(node.value);
const styled = ctx.style(converted, ctx.options.theme.math || ctx.options.theme.inlineCode);
const rendered = renderDisplayLatex(node.value);
const styled = ctx.style(rendered, ctx.options.theme.math || ctx.options.theme.inlineCode);
const lines = styled.split("\n");
return [`\n${lines.map((l) => ` ${l}`).join("\n")}\n`];
}
Expand Down
3 changes: 3 additions & 0 deletions src/txm-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
4 changes: 4 additions & 0 deletions src/txm-wasm/txm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* tslint:disable */
/* eslint-disable */

export function render_latex(latex: string): string;
134 changes: 134 additions & 0 deletions src/txm-wasm/txm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* @ts-self-types="./txm.d.ts" */

/**
* @param {string} latex
* @returns {string}
*/
function render_latex(latex) {
let deferred3_0;
let deferred3_1;
try {
const ptr0 = passStringToWasm0(latex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.render_latex(ptr0, len0);
var ptr2 = ret[0];
var len2 = ret[1];
if (ret[3]) {
ptr2 = 0; len2 = 0;
throw takeFromExternrefTable0(ret[2]);
}
deferred3_0 = ptr2;
deferred3_1 = len2;
return getStringFromWasm0(ptr2, len2);
} finally {
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
}
}
exports.render_latex = render_latex;

function __wbg_get_imports() {
const import0 = {
__proto__: null,
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
// Cast intrinsic for `Ref(String) -> Externref`.
const ret = getStringFromWasm0(arg0, arg1);
return ret;
},
__wbindgen_init_externref_table: function() {
const table = wasm.__wbindgen_externrefs;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
},
};
return {
__proto__: null,
"./txm_bg.js": import0,
};
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return decodeText(ptr, len);
}

let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8ArrayMemory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = cachedTextEncoder.encodeInto(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
return ptr;
}

function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_externrefs.get(idx);
wasm.__externref_table_dealloc(idx);
return value;
}

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
function decodeText(ptr, len) {
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

const cachedTextEncoder = new TextEncoder();

if (!('encodeInto' in cachedTextEncoder)) {
cachedTextEncoder.encodeInto = function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
};
}

let WASM_VECTOR_LEN = 0;

const wasmPath = `${__dirname}/txm_bg.wasm`;
const wasmBytes = require('fs').readFileSync(wasmPath);
const wasmModule = new WebAssembly.Module(wasmBytes);
let wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
wasm.__wbindgen_start();
Binary file added src/txm-wasm/txm_bg.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions src/txm-wasm/txm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export const render_latex: (a: number, b: number) => [number, number, number, number];
export const __wbindgen_externrefs: WebAssembly.Table;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __externref_table_dealloc: (a: number) => void;
export const __wbindgen_free: (a: number, b: number, c: number) => void;
export const __wbindgen_start: () => void;
Loading