"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const unifiedLatexCtan = require("@unified-latex/unified-latex-ctan"); const unifiedLatexUtilTrim = require("@unified-latex/unified-latex-util-trim"); const unifiedLatexUtilPegjs = require("@unified-latex/unified-latex-util-pegjs"); const unifiedLatexUtilVisit = require("@unified-latex/unified-latex-util-visit"); const unifiedLatexUtilMatch = require("@unified-latex/unified-latex-util-match"); const unifiedLatexUtilPrintRaw = require("@unified-latex/unified-latex-util-print-raw"); const unifiedLatexUtilArguments = require("@unified-latex/unified-latex-util-arguments"); const unifiedLatexUtilEnvironments = require("@unified-latex/unified-latex-util-environments"); const unifiedLatexUtilCatcode = require("@unified-latex/unified-latex-util-catcode"); const unifiedLatexAstComplier = function unifiedLatexAstComplier2() { Object.assign(this, { Compiler: (x) => x }); }; function bail(error) { if (error) { throw error; } } function getDefaultExportFromCjs(x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x; } /*! * Determine if an object is a Buffer * * @author Feross Aboukhadijeh * @license MIT */ var isBuffer$1; var hasRequiredIsBuffer; function requireIsBuffer() { if (hasRequiredIsBuffer) return isBuffer$1; hasRequiredIsBuffer = 1; isBuffer$1 = function isBuffer2(obj) { return obj != null && obj.constructor != null && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj); }; return isBuffer$1; } var isBufferExports = requireIsBuffer(); const isBuffer = /* @__PURE__ */ getDefaultExportFromCjs(isBufferExports); var extend$1; var hasRequiredExtend; function requireExtend() { if (hasRequiredExtend) return extend$1; hasRequiredExtend = 1; var hasOwn = Object.prototype.hasOwnProperty; var toStr = Object.prototype.toString; var defineProperty = Object.defineProperty; var gOPD = Object.getOwnPropertyDescriptor; var isArray = function isArray2(arr) { if (typeof Array.isArray === "function") { return Array.isArray(arr); } return toStr.call(arr) === "[object Array]"; }; var isPlainObject2 = function isPlainObject3(obj) { if (!obj || toStr.call(obj) !== "[object Object]") { return false; } var hasOwnConstructor = hasOwn.call(obj, "constructor"); var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, "isPrototypeOf"); if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) { return false; } var key; for (key in obj) { } return typeof key === "undefined" || hasOwn.call(obj, key); }; var setProperty = function setProperty2(target, options) { if (defineProperty && options.name === "__proto__") { defineProperty(target, options.name, { enumerable: true, configurable: true, value: options.newValue, writable: true }); } else { target[options.name] = options.newValue; } }; var getProperty = function getProperty2(obj, name) { if (name === "__proto__") { if (!hasOwn.call(obj, name)) { return void 0; } else if (gOPD) { return gOPD(obj, name).value; } } return obj[name]; }; extend$1 = function extend2() { var options, name, src, copy, copyIsArray, clone; var target = arguments[0]; var i = 1; var length = arguments.length; var deep = false; if (typeof target === "boolean") { deep = target; target = arguments[1] || {}; i = 2; } if (target == null || typeof target !== "object" && typeof target !== "function") { target = {}; } for (; i < length; ++i) { options = arguments[i]; if (options != null) { for (name in options) { src = getProperty(target, name); copy = getProperty(options, name); if (target !== copy) { if (deep && copy && (isPlainObject2(copy) || (copyIsArray = isArray(copy)))) { if (copyIsArray) { copyIsArray = false; clone = src && isArray(src) ? src : []; } else { clone = src && isPlainObject2(src) ? src : {}; } setProperty(target, { name, newValue: extend2(deep, clone, copy) }); } else if (typeof copy !== "undefined") { setProperty(target, { name, newValue: copy }); } } } } } return target; }; return extend$1; } var extendExports = requireExtend(); const extend = /* @__PURE__ */ getDefaultExportFromCjs(extendExports); function isPlainObject(value) { if (typeof value !== "object" || value === null) { return false; } const prototype = Object.getPrototypeOf(value); return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); } function trough() { const fns = []; const pipeline = { run, use }; return pipeline; function run(...values) { let middlewareIndex = -1; const callback = values.pop(); if (typeof callback !== "function") { throw new TypeError("Expected function as last argument, not " + callback); } next(null, ...values); function next(error, ...output) { const fn = fns[++middlewareIndex]; let index2 = -1; if (error) { callback(error); return; } while (++index2 < values.length) { if (output[index2] === null || output[index2] === void 0) { output[index2] = values[index2]; } } values = output; if (fn) { wrap(fn, next)(...output); } else { callback(null, ...output); } } } function use(middelware) { if (typeof middelware !== "function") { throw new TypeError( "Expected `middelware` to be a function, not " + middelware ); } fns.push(middelware); return pipeline; } } function wrap(middleware, callback) { let called; return wrapped; function wrapped(...parameters) { const fnExpectsCallback = middleware.length > parameters.length; let result; if (fnExpectsCallback) { parameters.push(done); } try { result = middleware.apply(this, parameters); } catch (error) { const exception = ( /** @type {Error} */ error ); if (fnExpectsCallback && called) { throw exception; } return done(exception); } if (!fnExpectsCallback) { if (result && result.then && typeof result.then === "function") { result.then(then, done); } else if (result instanceof Error) { done(result); } else { then(result); } } } function done(error, ...output) { if (!called) { called = true; callback(error, ...output); } } function then(value) { done(null, value); } } function stringifyPosition(value) { if (!value || typeof value !== "object") { return ""; } if ("position" in value || "type" in value) { return position(value.position); } if ("start" in value || "end" in value) { return position(value); } if ("line" in value || "column" in value) { return point(value); } return ""; } function point(point2) { return index(point2 && point2.line) + ":" + index(point2 && point2.column); } function position(pos) { return point(pos && pos.start) + "-" + point(pos && pos.end); } function index(value) { return value && typeof value === "number" ? value : 1; } class VFileMessage extends Error { /** * Create a message for `reason` at `place` from `origin`. * * When an error is passed in as `reason`, the `stack` is copied. * * @param {string | Error | VFileMessage} reason * Reason for message, uses the stack and message of the error if given. * * > 👉 **Note**: you should use markdown. * @param {Node | NodeLike | Position | Point | null | undefined} [place] * Place in file where the message occurred. * @param {string | null | undefined} [origin] * Place in code where the message originates (example: * `'my-package:my-rule'` or `'my-rule'`). * @returns * Instance of `VFileMessage`. */ // To do: next major: expose `undefined` everywhere instead of `null`. constructor(reason, place, origin) { const parts = [null, null]; let position2 = { // @ts-expect-error: we always follows the structure of `position`. start: { line: null, column: null }, // @ts-expect-error: " end: { line: null, column: null } }; super(); if (typeof place === "string") { origin = place; place = void 0; } if (typeof origin === "string") { const index2 = origin.indexOf(":"); if (index2 === -1) { parts[1] = origin; } else { parts[0] = origin.slice(0, index2); parts[1] = origin.slice(index2 + 1); } } if (place) { if ("type" in place || "position" in place) { if (place.position) { position2 = place.position; } } else if ("start" in place || "end" in place) { position2 = place; } else if ("line" in place || "column" in place) { position2.start = place; } } this.name = stringifyPosition(place) || "1:1"; this.message = typeof reason === "object" ? reason.message : reason; this.stack = ""; if (typeof reason === "object" && reason.stack) { this.stack = reason.stack; } this.reason = this.message; this.fatal; this.line = position2.start.line; this.column = position2.start.column; this.position = position2; this.source = parts[0]; this.ruleId = parts[1]; this.file; this.actual; this.expected; this.url; this.note; } } VFileMessage.prototype.file = ""; VFileMessage.prototype.name = ""; VFileMessage.prototype.reason = ""; VFileMessage.prototype.message = ""; VFileMessage.prototype.stack = ""; VFileMessage.prototype.fatal = null; VFileMessage.prototype.column = null; VFileMessage.prototype.line = null; VFileMessage.prototype.source = null; VFileMessage.prototype.ruleId = null; VFileMessage.prototype.position = null; const path = { basename, dirname, extname, join, sep: "/" }; function basename(path2, ext) { if (ext !== void 0 && typeof ext !== "string") { throw new TypeError('"ext" argument must be a string'); } assertPath$1(path2); let start = 0; let end = -1; let index2 = path2.length; let seenNonSlash; if (ext === void 0 || ext.length === 0 || ext.length > path2.length) { while (index2--) { if (path2.charCodeAt(index2) === 47) { if (seenNonSlash) { start = index2 + 1; break; } } else if (end < 0) { seenNonSlash = true; end = index2 + 1; } } return end < 0 ? "" : path2.slice(start, end); } if (ext === path2) { return ""; } let firstNonSlashEnd = -1; let extIndex = ext.length - 1; while (index2--) { if (path2.charCodeAt(index2) === 47) { if (seenNonSlash) { start = index2 + 1; break; } } else { if (firstNonSlashEnd < 0) { seenNonSlash = true; firstNonSlashEnd = index2 + 1; } if (extIndex > -1) { if (path2.charCodeAt(index2) === ext.charCodeAt(extIndex--)) { if (extIndex < 0) { end = index2; } } else { extIndex = -1; end = firstNonSlashEnd; } } } } if (start === end) { end = firstNonSlashEnd; } else if (end < 0) { end = path2.length; } return path2.slice(start, end); } function dirname(path2) { assertPath$1(path2); if (path2.length === 0) { return "."; } let end = -1; let index2 = path2.length; let unmatchedSlash; while (--index2) { if (path2.charCodeAt(index2) === 47) { if (unmatchedSlash) { end = index2; break; } } else if (!unmatchedSlash) { unmatchedSlash = true; } } return end < 0 ? path2.charCodeAt(0) === 47 ? "/" : "." : end === 1 && path2.charCodeAt(0) === 47 ? "//" : path2.slice(0, end); } function extname(path2) { assertPath$1(path2); let index2 = path2.length; let end = -1; let startPart = 0; let startDot = -1; let preDotState = 0; let unmatchedSlash; while (index2--) { const code = path2.charCodeAt(index2); if (code === 47) { if (unmatchedSlash) { startPart = index2 + 1; break; } continue; } if (end < 0) { unmatchedSlash = true; end = index2 + 1; } if (code === 46) { if (startDot < 0) { startDot = index2; } else if (preDotState !== 1) { preDotState = 1; } } else if (startDot > -1) { preDotState = -1; } } if (startDot < 0 || end < 0 || // We saw a non-dot character immediately before the dot. preDotState === 0 || // The (right-most) trimmed path component is exactly `..`. preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) { return ""; } return path2.slice(startDot, end); } function join(...segments) { let index2 = -1; let joined; while (++index2 < segments.length) { assertPath$1(segments[index2]); if (segments[index2]) { joined = joined === void 0 ? segments[index2] : joined + "/" + segments[index2]; } } return joined === void 0 ? "." : normalize(joined); } function normalize(path2) { assertPath$1(path2); const absolute = path2.charCodeAt(0) === 47; let value = normalizeString(path2, !absolute); if (value.length === 0 && !absolute) { value = "."; } if (value.length > 0 && path2.charCodeAt(path2.length - 1) === 47) { value += "/"; } return absolute ? "/" + value : value; } function normalizeString(path2, allowAboveRoot) { let result = ""; let lastSegmentLength = 0; let lastSlash = -1; let dots = 0; let index2 = -1; let code; let lastSlashIndex; while (++index2 <= path2.length) { if (index2 < path2.length) { code = path2.charCodeAt(index2); } else if (code === 47) { break; } else { code = 47; } if (code === 47) { if (lastSlash === index2 - 1 || dots === 1) ; else if (lastSlash !== index2 - 1 && dots === 2) { if (result.length < 2 || lastSegmentLength !== 2 || result.charCodeAt(result.length - 1) !== 46 || result.charCodeAt(result.length - 2) !== 46) { if (result.length > 2) { lastSlashIndex = result.lastIndexOf("/"); if (lastSlashIndex !== result.length - 1) { if (lastSlashIndex < 0) { result = ""; lastSegmentLength = 0; } else { result = result.slice(0, lastSlashIndex); lastSegmentLength = result.length - 1 - result.lastIndexOf("/"); } lastSlash = index2; dots = 0; continue; } } else if (result.length > 0) { result = ""; lastSegmentLength = 0; lastSlash = index2; dots = 0; continue; } } if (allowAboveRoot) { result = result.length > 0 ? result + "/.." : ".."; lastSegmentLength = 2; } } else { if (result.length > 0) { result += "/" + path2.slice(lastSlash + 1, index2); } else { result = path2.slice(lastSlash + 1, index2); } lastSegmentLength = index2 - lastSlash - 1; } lastSlash = index2; dots = 0; } else if (code === 46 && dots > -1) { dots++; } else { dots = -1; } } return result; } function assertPath$1(path2) { if (typeof path2 !== "string") { throw new TypeError( "Path must be a string. Received " + JSON.stringify(path2) ); } } const proc = { cwd }; function cwd() { return "/"; } function isUrl(fileUrlOrPath) { return fileUrlOrPath !== null && typeof fileUrlOrPath === "object" && // @ts-expect-error: indexable. fileUrlOrPath.href && // @ts-expect-error: indexable. fileUrlOrPath.origin; } function urlToPath(path2) { if (typeof path2 === "string") { path2 = new URL(path2); } else if (!isUrl(path2)) { const error = new TypeError( 'The "path" argument must be of type string or an instance of URL. Received `' + path2 + "`" ); error.code = "ERR_INVALID_ARG_TYPE"; throw error; } if (path2.protocol !== "file:") { const error = new TypeError("The URL must be of scheme file"); error.code = "ERR_INVALID_URL_SCHEME"; throw error; } return getPathFromURLPosix(path2); } function getPathFromURLPosix(url) { if (url.hostname !== "") { const error = new TypeError( 'File URL host must be "localhost" or empty on darwin' ); error.code = "ERR_INVALID_FILE_URL_HOST"; throw error; } const pathname = url.pathname; let index2 = -1; while (++index2 < pathname.length) { if (pathname.charCodeAt(index2) === 37 && pathname.charCodeAt(index2 + 1) === 50) { const third = pathname.charCodeAt(index2 + 2); if (third === 70 || third === 102) { const error = new TypeError( "File URL path must not include encoded / characters" ); error.code = "ERR_INVALID_FILE_URL_PATH"; throw error; } } } return decodeURIComponent(pathname); } const order = ["history", "path", "basename", "stem", "extname", "dirname"]; class VFile { /** * Create a new virtual file. * * `options` is treated as: * * * `string` or `Buffer` — `{value: options}` * * `URL` — `{path: options}` * * `VFile` — shallow copies its data over to the new file * * `object` — all fields are shallow copied over to the new file * * Path related fields are set in the following order (least specific to * most specific): `history`, `path`, `basename`, `stem`, `extname`, * `dirname`. * * You cannot set `dirname` or `extname` without setting either `history`, * `path`, `basename`, or `stem` too. * * @param {Compatible | null | undefined} [value] * File value. * @returns * New instance. */ constructor(value) { let options; if (!value) { options = {}; } else if (typeof value === "string" || buffer(value)) { options = { value }; } else if (isUrl(value)) { options = { path: value }; } else { options = value; } this.data = {}; this.messages = []; this.history = []; this.cwd = proc.cwd(); this.value; this.stored; this.result; this.map; let index2 = -1; while (++index2 < order.length) { const prop2 = order[index2]; if (prop2 in options && options[prop2] !== void 0 && options[prop2] !== null) { this[prop2] = prop2 === "history" ? [...options[prop2]] : options[prop2]; } } let prop; for (prop in options) { if (!order.includes(prop)) { this[prop] = options[prop]; } } } /** * Get the full path (example: `'~/index.min.js'`). * * @returns {string} */ get path() { return this.history[this.history.length - 1]; } /** * Set the full path (example: `'~/index.min.js'`). * * Cannot be nullified. * You can set a file URL (a `URL` object with a `file:` protocol) which will * be turned into a path with `url.fileURLToPath`. * * @param {string | URL} path */ set path(path2) { if (isUrl(path2)) { path2 = urlToPath(path2); } assertNonEmpty(path2, "path"); if (this.path !== path2) { this.history.push(path2); } } /** * Get the parent path (example: `'~'`). */ get dirname() { return typeof this.path === "string" ? path.dirname(this.path) : void 0; } /** * Set the parent path (example: `'~'`). * * Cannot be set if there’s no `path` yet. */ set dirname(dirname2) { assertPath(this.basename, "dirname"); this.path = path.join(dirname2 || "", this.basename); } /** * Get the basename (including extname) (example: `'index.min.js'`). */ get basename() { return typeof this.path === "string" ? path.basename(this.path) : void 0; } /** * Set basename (including extname) (`'index.min.js'`). * * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'` * on windows). * Cannot be nullified (use `file.path = file.dirname` instead). */ set basename(basename2) { assertNonEmpty(basename2, "basename"); assertPart(basename2, "basename"); this.path = path.join(this.dirname || "", basename2); } /** * Get the extname (including dot) (example: `'.js'`). */ get extname() { return typeof this.path === "string" ? path.extname(this.path) : void 0; } /** * Set the extname (including dot) (example: `'.js'`). * * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'` * on windows). * Cannot be set if there’s no `path` yet. */ set extname(extname2) { assertPart(extname2, "extname"); assertPath(this.dirname, "extname"); if (extname2) { if (extname2.charCodeAt(0) !== 46) { throw new Error("`extname` must start with `.`"); } if (extname2.includes(".", 1)) { throw new Error("`extname` cannot contain multiple dots"); } } this.path = path.join(this.dirname, this.stem + (extname2 || "")); } /** * Get the stem (basename w/o extname) (example: `'index.min'`). */ get stem() { return typeof this.path === "string" ? path.basename(this.path, this.extname) : void 0; } /** * Set the stem (basename w/o extname) (example: `'index.min'`). * * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'` * on windows). * Cannot be nullified (use `file.path = file.dirname` instead). */ set stem(stem) { assertNonEmpty(stem, "stem"); assertPart(stem, "stem"); this.path = path.join(this.dirname || "", stem + (this.extname || "")); } /** * Serialize the file. * * @param {BufferEncoding | null | undefined} [encoding='utf8'] * Character encoding to understand `value` as when it’s a `Buffer` * (default: `'utf8'`). * @returns {string} * Serialized file. */ toString(encoding) { return (this.value || "").toString(encoding || void 0); } /** * Create a warning message associated with the file. * * Its `fatal` is set to `false` and `file` is set to the current file path. * Its added to `file.messages`. * * @param {string | Error | VFileMessage} reason * Reason for message, uses the stack and message of the error if given. * @param {Node | NodeLike | Position | Point | null | undefined} [place] * Place in file where the message occurred. * @param {string | null | undefined} [origin] * Place in code where the message originates (example: * `'my-package:my-rule'` or `'my-rule'`). * @returns {VFileMessage} * Message. */ message(reason, place, origin) { const message = new VFileMessage(reason, place, origin); if (this.path) { message.name = this.path + ":" + message.name; message.file = this.path; } message.fatal = false; this.messages.push(message); return message; } /** * Create an info message associated with the file. * * Its `fatal` is set to `null` and `file` is set to the current file path. * Its added to `file.messages`. * * @param {string | Error | VFileMessage} reason * Reason for message, uses the stack and message of the error if given. * @param {Node | NodeLike | Position | Point | null | undefined} [place] * Place in file where the message occurred. * @param {string | null | undefined} [origin] * Place in code where the message originates (example: * `'my-package:my-rule'` or `'my-rule'`). * @returns {VFileMessage} * Message. */ info(reason, place, origin) { const message = this.message(reason, place, origin); message.fatal = null; return message; } /** * Create a fatal error associated with the file. * * Its `fatal` is set to `true` and `file` is set to the current file path. * Its added to `file.messages`. * * > 👉 **Note**: a fatal error means that a file is no longer processable. * * @param {string | Error | VFileMessage} reason * Reason for message, uses the stack and message of the error if given. * @param {Node | NodeLike | Position | Point | null | undefined} [place] * Place in file where the message occurred. * @param {string | null | undefined} [origin] * Place in code where the message originates (example: * `'my-package:my-rule'` or `'my-rule'`). * @returns {never} * Message. * @throws {VFileMessage} * Message. */ fail(reason, place, origin) { const message = this.message(reason, place, origin); message.fatal = true; throw message; } } function assertPart(part, name) { if (part && part.includes(path.sep)) { throw new Error( "`" + name + "` cannot be a path: did not expect `" + path.sep + "`" ); } } function assertNonEmpty(part, name) { if (!part) { throw new Error("`" + name + "` cannot be empty"); } } function assertPath(path2, name) { if (!path2) { throw new Error("Setting `" + name + "` requires `path` to be set too"); } } function buffer(value) { return isBuffer(value); } const unified = base().freeze(); const own = {}.hasOwnProperty; function base() { const transformers = trough(); const attachers = []; let namespace = {}; let frozen; let freezeIndex = -1; processor.data = data; processor.Parser = void 0; processor.Compiler = void 0; processor.freeze = freeze; processor.attachers = attachers; processor.use = use; processor.parse = parse2; processor.stringify = stringify; processor.run = run; processor.runSync = runSync; processor.process = process; processor.processSync = processSync; return processor; function processor() { const destination = base(); let index2 = -1; while (++index2 < attachers.length) { destination.use(...attachers[index2]); } destination.data(extend(true, {}, namespace)); return destination; } function data(key, value) { if (typeof key === "string") { if (arguments.length === 2) { assertUnfrozen("data", frozen); namespace[key] = value; return processor; } return own.call(namespace, key) && namespace[key] || null; } if (key) { assertUnfrozen("data", frozen); namespace = key; return processor; } return namespace; } function freeze() { if (frozen) { return processor; } while (++freezeIndex < attachers.length) { const [attacher, ...options] = attachers[freezeIndex]; if (options[0] === false) { continue; } if (options[0] === true) { options[0] = void 0; } const transformer = attacher.call(processor, ...options); if (typeof transformer === "function") { transformers.use(transformer); } } frozen = true; freezeIndex = Number.POSITIVE_INFINITY; return processor; } function use(value, ...options) { let settings; assertUnfrozen("use", frozen); if (value === null || value === void 0) ; else if (typeof value === "function") { addPlugin(value, ...options); } else if (typeof value === "object") { if (Array.isArray(value)) { addList(value); } else { addPreset(value); } } else { throw new TypeError("Expected usable value, not `" + value + "`"); } if (settings) { namespace.settings = Object.assign(namespace.settings || {}, settings); } return processor; function add(value2) { if (typeof value2 === "function") { addPlugin(value2); } else if (typeof value2 === "object") { if (Array.isArray(value2)) { const [plugin, ...options2] = value2; addPlugin(plugin, ...options2); } else { addPreset(value2); } } else { throw new TypeError("Expected usable value, not `" + value2 + "`"); } } function addPreset(result) { addList(result.plugins); if (result.settings) { settings = Object.assign(settings || {}, result.settings); } } function addList(plugins) { let index2 = -1; if (plugins === null || plugins === void 0) ; else if (Array.isArray(plugins)) { while (++index2 < plugins.length) { const thing = plugins[index2]; add(thing); } } else { throw new TypeError("Expected a list of plugins, not `" + plugins + "`"); } } function addPlugin(plugin, value2) { let index2 = -1; let entry; while (++index2 < attachers.length) { if (attachers[index2][0] === plugin) { entry = attachers[index2]; break; } } if (entry) { if (isPlainObject(entry[1]) && isPlainObject(value2)) { value2 = extend(true, entry[1], value2); } entry[1] = value2; } else { attachers.push([...arguments]); } } } function parse2(doc) { processor.freeze(); const file = vfile(doc); const Parser = processor.Parser; assertParser("parse", Parser); if (newable(Parser, "parse")) { return new Parser(String(file), file).parse(); } return Parser(String(file), file); } function stringify(node, doc) { processor.freeze(); const file = vfile(doc); const Compiler = processor.Compiler; assertCompiler("stringify", Compiler); assertNode(node); if (newable(Compiler, "compile")) { return new Compiler(node, file).compile(); } return Compiler(node, file); } function run(node, doc, callback) { assertNode(node); processor.freeze(); if (!callback && typeof doc === "function") { callback = doc; doc = void 0; } if (!callback) { return new Promise(executor); } executor(null, callback); function executor(resolve, reject) { transformers.run(node, vfile(doc), done); function done(error, tree, file) { tree = tree || node; if (error) { reject(error); } else if (resolve) { resolve(tree); } else { callback(null, tree, file); } } } } function runSync(node, file) { let result; let complete; processor.run(node, file, done); assertDone("runSync", "run", complete); return result; function done(error, tree) { bail(error); result = tree; complete = true; } } function process(doc, callback) { processor.freeze(); assertParser("process", processor.Parser); assertCompiler("process", processor.Compiler); if (!callback) { return new Promise(executor); } executor(null, callback); function executor(resolve, reject) { const file = vfile(doc); processor.run(processor.parse(file), file, (error, tree, file2) => { if (error || !tree || !file2) { done(error); } else { const result = processor.stringify(tree, file2); if (result === void 0 || result === null) ; else if (looksLikeAVFileValue(result)) { file2.value = result; } else { file2.result = result; } done(error, file2); } }); function done(error, file2) { if (error || !file2) { reject(error); } else if (resolve) { resolve(file2); } else { callback(null, file2); } } } } function processSync(doc) { let complete; processor.freeze(); assertParser("processSync", processor.Parser); assertCompiler("processSync", processor.Compiler); const file = vfile(doc); processor.process(file, done); assertDone("processSync", "process", complete); return file; function done(error) { complete = true; bail(error); } } } function newable(value, name) { return typeof value === "function" && // Prototypes do exist. // type-coverage:ignore-next-line value.prototype && // A function with keys in its prototype is probably a constructor. // Classes’ prototype methods are not enumerable, so we check if some value // exists in the prototype. // type-coverage:ignore-next-line (keys(value.prototype) || name in value.prototype); } function keys(value) { let key; for (key in value) { if (own.call(value, key)) { return true; } } return false; } function assertParser(name, value) { if (typeof value !== "function") { throw new TypeError("Cannot `" + name + "` without `Parser`"); } } function assertCompiler(name, value) { if (typeof value !== "function") { throw new TypeError("Cannot `" + name + "` without `Compiler`"); } } function assertUnfrozen(name, frozen) { if (frozen) { throw new Error( "Cannot call `" + name + "` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`." ); } } function assertNode(node) { if (!isPlainObject(node) || typeof node.type !== "string") { throw new TypeError("Expected node, got `" + node + "`"); } } function assertDone(name, asyncName, complete) { if (!complete) { throw new Error( "`" + name + "` finished async. Use `" + asyncName + "` instead" ); } } function vfile(value) { return looksLikeAVFile(value) ? value : new VFile(value); } function looksLikeAVFile(value) { return Boolean( value && typeof value === "object" && "message" in value && "messages" in value ); } function looksLikeAVFileValue(value) { return typeof value === "string" || isBuffer(value); } function parseMinimal(str) { return unifiedLatexUtilPegjs.LatexPegParser.parse(str); } function parseMathMinimal(str) { return unifiedLatexUtilPegjs.LatexPegParser.parse(str, { startRule: "math" }); } const unifiedLatexFromStringMinimal = function unifiedLatexFromStringMinimal2(options) { const parser2 = (str) => { if ((options == null ? void 0 : options.mode) === "math") { return { type: "root", content: parseMathMinimal(str), _renderInfo: { inMathMode: true } }; } return parseMinimal(str); }; Object.assign(this, { Parser: parser2 }); }; const unifiedLatexReparseMath = function unifiedLatexReparseMath2(options) { const { mathEnvs = [], mathMacros = [] } = options || {}; return unifiedLatexReparseMathConstructPlugin({ mathMacros, mathEnvs }); }; function unifiedLatexReparseMathConstructPlugin({ mathEnvs, mathMacros }) { const isMathEnvironment = unifiedLatexUtilMatch.match.createEnvironmentMatcher(mathEnvs); const isMathMacro = unifiedLatexUtilMatch.match.createMacroMatcher(mathMacros); return (tree) => { unifiedLatexUtilVisit.visit( tree, (node) => { if (unifiedLatexUtilMatch.match.anyMacro(node)) { for (const arg of node.args || []) { if (arg.content.length > 0 && !wasParsedInMathMode(arg.content)) { arg.content = parseMathMinimal( unifiedLatexUtilPrintRaw.printRaw(arg.content) ); } } } if (unifiedLatexUtilMatch.match.anyEnvironment(node)) { if (!wasParsedInMathMode(node.content)) { node.content = parseMathMinimal(unifiedLatexUtilPrintRaw.printRaw(node.content)); } } }, { test: (node) => isMathEnvironment(node) || isMathMacro(node) } ); }; } function wasParsedInMathMode(nodes) { return !nodes.some( (node) => ( // If there are multi-char strings or ^ and _ have been parsed as strings, we know // that we were not parsed in math mode. unifiedLatexUtilMatch.match.anyString(node) && node.content.length > 1 || unifiedLatexUtilMatch.match.string(node, "^") || unifiedLatexUtilMatch.match.string(node, "_") ) ); } const unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse = function unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse2(options) { const { environments = {}, macros = {} } = options || {}; const mathMacros = Object.fromEntries( Object.entries(macros).filter( ([_, info]) => { var _a; return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; } ) ); const mathEnvs = Object.fromEntries( Object.entries(environments).filter( ([_, info]) => { var _a; return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; } ) ); const mathReparser = unifiedLatexReparseMathConstructPlugin({ mathEnvs: Object.keys(mathEnvs), mathMacros: Object.keys(mathMacros) }); const isRelevantEnvironment = unifiedLatexUtilMatch.match.createEnvironmentMatcher(environments); const isRelevantMathEnvironment = unifiedLatexUtilMatch.match.createEnvironmentMatcher(mathEnvs); return (tree) => { unifiedLatexUtilVisit.visit( tree, { enter: (nodes) => { if (!Array.isArray(nodes)) { return; } unifiedLatexUtilArguments.attachMacroArgsInArray(nodes, mathMacros); }, leave: (node) => { if (!isRelevantMathEnvironment(node)) { return; } const envName = unifiedLatexUtilPrintRaw.printRaw(node.env); const envInfo = environments[envName]; if (!envInfo) { throw new Error( `Could not find environment info for environment "${envName}"` ); } unifiedLatexUtilEnvironments.processEnvironment(node, envInfo); } }, { includeArrays: true } ); mathReparser(tree); unifiedLatexUtilVisit.visit( tree, { enter: (nodes) => { if (!Array.isArray(nodes)) { return; } unifiedLatexUtilArguments.attachMacroArgsInArray(nodes, macros); }, leave: (node) => { if (!isRelevantEnvironment(node)) { return; } const envName = unifiedLatexUtilPrintRaw.printRaw(node.env); const envInfo = environments[envName]; if (!envInfo) { throw new Error( `Could not find environment info for environment "${envName}"` ); } unifiedLatexUtilEnvironments.processEnvironment(node, envInfo); } }, { includeArrays: true } ); }; }; const unifiedLatexProcessAtLetterAndExplMacros = function unifiedLatexProcessAtLetterAndExplMacros2(options) { let { atLetter = false, expl3 = false, autodetectExpl3AndAtLetter = false } = options || {}; return (tree) => { unifiedLatexUtilCatcode.reparseExpl3AndAtLetterRegions(tree); if (atLetter || expl3) { autodetectExpl3AndAtLetter = false; } if (autodetectExpl3AndAtLetter) { atLetter = unifiedLatexUtilCatcode.hasReparsableMacroNames(tree, "@"); expl3 = unifiedLatexUtilCatcode.hasReparsableMacroNames(tree, "_"); } const charSet = /* @__PURE__ */ new Set(); if (atLetter) { charSet.add("@"); } if (expl3) { charSet.add(":"); charSet.add("_"); } if (charSet.size > 0) { unifiedLatexUtilCatcode.reparseMacroNames(tree, charSet); } }; }; const unifiedLatexFromString = function unifiedLatexFromString2(options) { const { mode = "regular", macros = {}, environments = {}, flags: { atLetter = false, expl3 = false, autodetectExpl3AndAtLetter = false } = {} } = options || {}; const allMacroInfo = Object.assign( {}, ...Object.values(unifiedLatexCtan.macroInfo), macros ); const allEnvInfo = Object.assign( {}, ...Object.values(unifiedLatexCtan.environmentInfo), environments ); const fullParser = unified().use(unifiedLatexFromStringMinimal, { mode }).use(unifiedLatexProcessAtLetterAndExplMacros, { atLetter, expl3, autodetectExpl3AndAtLetter }).use(unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse, { macros: allMacroInfo, environments: allEnvInfo }).use(unifiedLatexUtilTrim.unifiedLatexTrimEnvironmentContents).use(unifiedLatexUtilTrim.unifiedLatexTrimRoot).use(unifiedLatexAstComplier); const parser2 = (str) => { const file = fullParser.processSync({ value: str }); return file.result; }; Object.assign(this, { Parser: parser2 }); }; let parser = unified().use(unifiedLatexFromString).freeze(); function parse(str) { return parser.parse(str); } function getParser(options) { return options ? unified().use(unifiedLatexFromString, options).freeze() : parser; } function parseMath(str) { if (typeof str !== "string") { str = unifiedLatexUtilPrintRaw.printRaw(str); } const file = unified().use(unifiedLatexFromString, { mode: "math" }).use(unifiedLatexAstComplier).processSync({ value: str }); return file.result.content; } exports.getParser = getParser; exports.parse = parse; exports.parseMath = parseMath; exports.parseMathMinimal = parseMathMinimal; exports.parseMinimal = parseMinimal; exports.unifiedLatexAstComplier = unifiedLatexAstComplier; exports.unifiedLatexFromString = unifiedLatexFromString; exports.unifiedLatexFromStringMinimal = unifiedLatexFromStringMinimal; exports.unifiedLatexProcessAtLetterAndExplMacros = unifiedLatexProcessAtLetterAndExplMacros; exports.unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse = unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse; exports.unifiedLatexReparseMath = unifiedLatexReparseMath; exports.unifiedLatexReparseMathConstructPlugin = unifiedLatexReparseMathConstructPlugin; //# sourceMappingURL=index.cjs.map