UNPKG

laif-ds

Version:

Design System di Laif con componenti React basati su principi di Atomic Design

531 lines (530 loc) 18.2 kB
"use client"; import { bail as P } from "../../bail/index.js"; import y from "../../../_virtual/index5.js"; import z from "../../is-plain-obj/index.js"; import { CallableInstance as C } from "./callable-instance.js"; import { trough as A } from "../../trough/lib/index.js"; import { VFile as S } from "../../vfile/lib/index.js"; const F = {}.hasOwnProperty; class x extends C { /** * Create a processor. */ constructor() { super("copy"), this.Compiler = void 0, this.Parser = void 0, this.attachers = [], this.compiler = void 0, this.freezeIndex = -1, this.frozen = void 0, this.namespace = {}, this.parser = void 0, this.transformers = A(); } /** * Copy a processor. * * @deprecated * This is a private internal method and should not be used. * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * New *unfrozen* processor ({@linkcode Processor}) that is * configured to work the same as its ancestor. * When the descendant processor is configured in the future it does not * affect the ancestral processor. */ copy() { const e = ( /** @type {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} */ new x() ); let n = -1; for (; ++n < this.attachers.length; ) { const t = this.attachers[n]; e.use(...t); } return e.data(y(!0, {}, this.namespace)), e; } /** * Configure the processor with info available to all plugins. * Information is stored in an object. * * Typically, options can be given to a specific plugin, but sometimes it * makes sense to have information shared with several plugins. * For example, a list of HTML elements that are self-closing, which is * needed during all phases. * * > **Note**: setting information cannot occur on *frozen* processors. * > Call the processor first to create a new unfrozen processor. * * > **Note**: to register custom data in TypeScript, augment the * > {@linkcode Data} interface. * * @example * This example show how to get and set info: * * ```js * import {unified} from 'unified' * * const processor = unified().data('alpha', 'bravo') * * processor.data('alpha') // => 'bravo' * * processor.data() // => {alpha: 'bravo'} * * processor.data({charlie: 'delta'}) * * processor.data() // => {charlie: 'delta'} * ``` * * @template {keyof Data} Key * * @overload * @returns {Data} * * @overload * @param {Data} dataset * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * * @overload * @param {Key} key * @returns {Data[Key]} * * @overload * @param {Key} key * @param {Data[Key]} value * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * * @param {Data | Key} [key] * Key to get or set, or entire dataset to set, or nothing to get the * entire dataset (optional). * @param {Data[Key]} [value] * Value to set (optional). * @returns {unknown} * The current processor when setting, the value at `key` when getting, or * the entire dataset when getting without key. */ data(e, n) { return typeof e == "string" ? arguments.length === 2 ? (b("data", this.frozen), this.namespace[e] = n, this) : F.call(this.namespace, e) && this.namespace[e] || void 0 : e ? (b("data", this.frozen), this.namespace = e, this) : this.namespace; } /** * Freeze a processor. * * Frozen processors are meant to be extended and not to be configured * directly. * * When a processor is frozen it cannot be unfrozen. * New processors working the same way can be created by calling the * processor. * * It’s possible to freeze processors explicitly by calling `.freeze()`. * Processors freeze automatically when `.parse()`, `.run()`, `.runSync()`, * `.stringify()`, `.process()`, or `.processSync()` are called. * * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * The current processor. */ freeze() { if (this.frozen) return this; const e = ( /** @type {Processor} */ /** @type {unknown} */ this ); for (; ++this.freezeIndex < this.attachers.length; ) { const [n, ...t] = this.attachers[this.freezeIndex]; if (t[0] === !1) continue; t[0] === !0 && (t[0] = void 0); const i = n.call(e, ...t); typeof i == "function" && this.transformers.use(i); } return this.frozen = !0, this.freezeIndex = Number.POSITIVE_INFINITY, this; } /** * Parse text to a syntax tree. * * > **Note**: `parse` freezes the processor if not already *frozen*. * * > **Note**: `parse` performs the parse phase, not the run phase or other * > phases. * * @param {Compatible | undefined} [file] * file to parse (optional); typically `string` or `VFile`; any value * accepted as `x` in `new VFile(x)`. * @returns {ParseTree extends undefined ? Node : ParseTree} * Syntax tree representing `file`. */ parse(e) { this.freeze(); const n = m(e), t = this.parser || this.Parser; return w("parse", t), t(String(n), n); } /** * Process the given file as configured on the processor. * * > **Note**: `process` freezes the processor if not already *frozen*. * * > **Note**: `process` performs the parse, run, and stringify phases. * * @overload * @param {Compatible | undefined} file * @param {ProcessCallback<VFileWithOutput<CompileResult>>} done * @returns {undefined} * * @overload * @param {Compatible | undefined} [file] * @returns {Promise<VFileWithOutput<CompileResult>>} * * @param {Compatible | undefined} [file] * File (optional); typically `string` or `VFile`]; any value accepted as * `x` in `new VFile(x)`. * @param {ProcessCallback<VFileWithOutput<CompileResult>> | undefined} [done] * Callback (optional). * @returns {Promise<VFile> | undefined} * Nothing if `done` is given. * Otherwise a promise, rejected with a fatal error or resolved with the * processed file. * * The parsed, transformed, and compiled value is available at * `file.value` (see note). * * > **Note**: unified typically compiles by serializing: most * > compilers return `string` (or `Uint8Array`). * > Some compilers, such as the one configured with * > [`rehype-react`][rehype-react], return other values (in this case, a * > React tree). * > If you’re using a compiler that doesn’t serialize, expect different * > result values. * > * > To register custom results in TypeScript, add them to * > {@linkcode CompileResultMap}. * * [rehype-react]: https://github.com/rehypejs/rehype-react */ process(e, n) { const t = this; return this.freeze(), w("process", this.parser || this.Parser), g("process", this.compiler || this.Compiler), n ? i(void 0, n) : new Promise(i); function i(a, h) { const p = m(e), l = ( /** @type {HeadTree extends undefined ? Node : HeadTree} */ /** @type {unknown} */ t.parse(p) ); t.run(l, p, function(o, c, f) { if (o || !c || !f) return s(o); const u = ( /** @type {CompileTree extends undefined ? Node : CompileTree} */ /** @type {unknown} */ c ), d = t.stringify(u, f); D(d) ? f.value = d : f.result = d, s( o, /** @type {VFileWithOutput<CompileResult>} */ f ); }); function s(o, c) { o || !c ? h(o) : a ? a(c) : n(void 0, c); } } } /** * Process the given file as configured on the processor. * * An error is thrown if asynchronous transforms are configured. * * > **Note**: `processSync` freezes the processor if not already *frozen*. * * > **Note**: `processSync` performs the parse, run, and stringify phases. * * @param {Compatible | undefined} [file] * File (optional); typically `string` or `VFile`; any value accepted as * `x` in `new VFile(x)`. * @returns {VFileWithOutput<CompileResult>} * The processed file. * * The parsed, transformed, and compiled value is available at * `file.value` (see note). * * > **Note**: unified typically compiles by serializing: most * > compilers return `string` (or `Uint8Array`). * > Some compilers, such as the one configured with * > [`rehype-react`][rehype-react], return other values (in this case, a * > React tree). * > If you’re using a compiler that doesn’t serialize, expect different * > result values. * > * > To register custom results in TypeScript, add them to * > {@linkcode CompileResultMap}. * * [rehype-react]: https://github.com/rehypejs/rehype-react */ processSync(e) { let n = !1, t; return this.freeze(), w("processSync", this.parser || this.Parser), g("processSync", this.compiler || this.Compiler), this.process(e, i), T("processSync", "process", n), t; function i(a, h) { n = !0, P(a), t = h; } } /** * Run *transformers* on a syntax tree. * * > **Note**: `run` freezes the processor if not already *frozen*. * * > **Note**: `run` performs the run phase, not other phases. * * @overload * @param {HeadTree extends undefined ? Node : HeadTree} tree * @param {RunCallback<TailTree extends undefined ? Node : TailTree>} done * @returns {undefined} * * @overload * @param {HeadTree extends undefined ? Node : HeadTree} tree * @param {Compatible | undefined} file * @param {RunCallback<TailTree extends undefined ? Node : TailTree>} done * @returns {undefined} * * @overload * @param {HeadTree extends undefined ? Node : HeadTree} tree * @param {Compatible | undefined} [file] * @returns {Promise<TailTree extends undefined ? Node : TailTree>} * * @param {HeadTree extends undefined ? Node : HeadTree} tree * Tree to transform and inspect. * @param {( * RunCallback<TailTree extends undefined ? Node : TailTree> | * Compatible * )} [file] * File associated with `node` (optional); any value accepted as `x` in * `new VFile(x)`. * @param {RunCallback<TailTree extends undefined ? Node : TailTree>} [done] * Callback (optional). * @returns {Promise<TailTree extends undefined ? Node : TailTree> | undefined} * Nothing if `done` is given. * Otherwise, a promise rejected with a fatal error or resolved with the * transformed tree. */ run(e, n, t) { I(e), this.freeze(); const i = this.transformers; return !t && typeof n == "function" && (t = n, n = void 0), t ? a(void 0, t) : new Promise(a); function a(h, p) { const l = m(n); i.run(e, l, s); function s(o, c, f) { const u = ( /** @type {TailTree extends undefined ? Node : TailTree} */ c || e ); o ? p(o) : h ? h(u) : t(void 0, u, f); } } } /** * Run *transformers* on a syntax tree. * * An error is thrown if asynchronous transforms are configured. * * > **Note**: `runSync` freezes the processor if not already *frozen*. * * > **Note**: `runSync` performs the run phase, not other phases. * * @param {HeadTree extends undefined ? Node : HeadTree} tree * Tree to transform and inspect. * @param {Compatible | undefined} [file] * File associated with `node` (optional); any value accepted as `x` in * `new VFile(x)`. * @returns {TailTree extends undefined ? Node : TailTree} * Transformed tree. */ runSync(e, n) { let t = !1, i; return this.run(e, n, a), T("runSync", "run", t), i; function a(h, p) { P(h), i = p, t = !0; } } /** * Compile a syntax tree. * * > **Note**: `stringify` freezes the processor if not already *frozen*. * * > **Note**: `stringify` performs the stringify phase, not the run phase * > or other phases. * * @param {CompileTree extends undefined ? Node : CompileTree} tree * Tree to compile. * @param {Compatible | undefined} [file] * File associated with `node` (optional); any value accepted as `x` in * `new VFile(x)`. * @returns {CompileResult extends undefined ? Value : CompileResult} * Textual representation of the tree (see note). * * > **Note**: unified typically compiles by serializing: most compilers * > return `string` (or `Uint8Array`). * > Some compilers, such as the one configured with * > [`rehype-react`][rehype-react], return other values (in this case, a * > React tree). * > If you’re using a compiler that doesn’t serialize, expect different * > result values. * > * > To register custom results in TypeScript, add them to * > {@linkcode CompileResultMap}. * * [rehype-react]: https://github.com/rehypejs/rehype-react */ stringify(e, n) { this.freeze(); const t = m(n), i = this.compiler || this.Compiler; return g("stringify", i), I(e), i(e, t); } /** * Configure the processor to use a plugin, a list of usable values, or a * preset. * * If the processor is already using a plugin, the previous plugin * configuration is changed based on the options that are passed in. * In other words, the plugin is not added a second time. * * > **Note**: `use` cannot be called on *frozen* processors. * > Call the processor first to create a new unfrozen processor. * * @example * There are many ways to pass plugins to `.use()`. * This example gives an overview: * * ```js * import {unified} from 'unified' * * unified() * // Plugin with options: * .use(pluginA, {x: true, y: true}) * // Passing the same plugin again merges configuration (to `{x: true, y: false, z: true}`): * .use(pluginA, {y: false, z: true}) * // Plugins: * .use([pluginB, pluginC]) * // Two plugins, the second with options: * .use([pluginD, [pluginE, {}]]) * // Preset with plugins and settings: * .use({plugins: [pluginF, [pluginG, {}]], settings: {position: false}}) * // Settings only: * .use({settings: {position: false}}) * ``` * * @template {Array<unknown>} [Parameters=[]] * @template {Node | string | undefined} [Input=undefined] * @template [Output=Input] * * @overload * @param {Preset | null | undefined} [preset] * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * * @overload * @param {PluggableList} list * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * * @overload * @param {Plugin<Parameters, Input, Output>} plugin * @param {...(Parameters | [boolean])} parameters * @returns {UsePlugin<ParseTree, HeadTree, TailTree, CompileTree, CompileResult, Input, Output>} * * @param {PluggableList | Plugin | Preset | null | undefined} value * Usable value. * @param {...unknown} parameters * Parameters, when a plugin is given as a usable value. * @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>} * Current processor. */ use(e, ...n) { const t = this.attachers, i = this.namespace; if (b("use", this.frozen), e != null) if (typeof e == "function") l(e, n); else if (typeof e == "object") Array.isArray(e) ? p(e) : h(e); else throw new TypeError("Expected usable value, not `" + e + "`"); return this; function a(s) { if (typeof s == "function") l(s, []); else if (typeof s == "object") if (Array.isArray(s)) { const [o, ...c] = ( /** @type {PluginTuple<Array<unknown>>} */ s ); l(o, c); } else h(s); else throw new TypeError("Expected usable value, not `" + s + "`"); } function h(s) { if (!("plugins" in s) && !("settings" in s)) throw new Error( "Expected usable value but received an empty preset, which is probably a mistake: presets typically come with `plugins` and sometimes with `settings`, but this has neither" ); p(s.plugins), s.settings && (i.settings = y(!0, i.settings, s.settings)); } function p(s) { let o = -1; if (s != null) if (Array.isArray(s)) for (; ++o < s.length; ) { const c = s[o]; a(c); } else throw new TypeError("Expected a list of plugins, not `" + s + "`"); } function l(s, o) { let c = -1, f = -1; for (; ++c < t.length; ) if (t[c][0] === s) { f = c; break; } if (f === -1) t.push([s, ...o]); else if (o.length > 0) { let [u, ...d] = o; const E = t[f][1]; z(E) && z(u) && (u = y(!0, E, u)), t[f] = [s, u, ...d]; } } } } const R = new x().freeze(); function w(r, e) { if (typeof e != "function") throw new TypeError("Cannot `" + r + "` without `parser`"); } function g(r, e) { if (typeof e != "function") throw new TypeError("Cannot `" + r + "` without `compiler`"); } function b(r, e) { if (e) throw new Error( "Cannot call `" + r + "` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`." ); } function I(r) { if (!z(r) || typeof r.type != "string") throw new TypeError("Expected node, got `" + r + "`"); } function T(r, e, n) { if (!n) throw new Error( "`" + r + "` finished async. Use `" + e + "` instead" ); } function m(r) { return j(r) ? r : new S(r); } function j(r) { return !!(r && typeof r == "object" && "message" in r && "messages" in r); } function D(r) { return typeof r == "string" || L(r); } function L(r) { return !!(r && typeof r == "object" && "byteLength" in r && "byteOffset" in r); } export { x as Processor, R as unified };