UNPKG

rehype-katex-svelte

Version:

rehype plugin to transform .math-inline, .math-display with KaTeX to Svelte markup

37 lines (36 loc) 1.92 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { selectAll } from "hast-util-select"; import { toString as hastToString } from "hast-util-to-string"; import { fromString as hastFromString } from "hast-util-from-string"; // TODO fix katex types: currently `@types/katex`'s type stubs pretend katex // exports properties on a module level (i.e. `export const`), but actually it // `export default`s a single object with properties. The difference is that // `export const renderToString` allows syntax like `import {renderToString} // from 'katex'` to work, whereas `export default {renderToString}` doesn't and // instead requires `import Katex` (default) and `Katex.renderToString`. // Currently this relies on `allowSyntheticDefaultImports` to type-check, but // it really shouldn't, because the type declaration is wrong. import Katex from "katex"; var rehypeKatexSvelte = function (options) { if (options === void 0) { options = {}; } return function (tree) { var _a, _b; for (var _i = 0, _c = selectAll(".math-inline,.math-display", tree); _i < _c.length; _i++) { var node = _c[_i]; var displayMode = (_b = (((_a = node.properties) === null || _a === void 0 ? void 0 : _a.className))) === null || _b === void 0 ? void 0 : _b.includes("math-display"); var rendered = Katex.renderToString(hastToString(node), __assign(__assign({}, options), { displayMode: displayMode })); hastFromString(node, "{@html ".concat(JSON.stringify(rendered), "}")); } }; }; export default rehypeKatexSvelte;