UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

156 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShortcodeWithPage = void 0; const scratch_1 = require("../../../../pkg/maps/scratch"); /** * ShortcodeWithPage is the "." context in a shortcode template. */ class ShortcodeWithPage { constructor(params, inner, page, parent, name, isNamedParams, ordinal, indentation = '', posOffset = 0) { this._innerDeindent = null; this._pos = null; this._scratch = null; this._params = params; this._inner = inner; this._page = page; this._parent = parent; this._name = name; this._isNamedParams = isNamedParams; this._ordinal = ordinal; this._indentation = indentation; this._posOffset = posOffset; } /** * Get shortcode parameters */ get params() { return this._params; } get Params() { return this._params; } get IsNamedParams() { return this._isNamedParams; } /** * Get inner content */ get inner() { return this._inner; } get Inner() { return this._inner; } /** * Get parent page */ get page() { return this._page; } get Page() { return this._page; } /** * Get parent shortcode if any */ get parent() { return this._parent; } get Parent() { return this._parent; } /** * Get shortcode name */ get name() { return this._name; } /** * Check if using named parameters */ get isNamedParams() { return this._isNamedParams; } /** * Get ordinal position */ get ordinal() { return this._ordinal; } get Ordinal() { return this._ordinal; } /** * Returns the (potentially de-indented) inner content of the shortcode */ innerDeindent() { if (this._indentation === '') { return this._inner; } if (this._innerDeindent === null) { const lines = this._inner.split('\n'); this._innerDeindent = lines.map(line => { if (line.startsWith(this._indentation)) { return line.substring(this._indentation.length); } return line; }).join('\n'); } return this._innerDeindent; } get InnerDeindent() { return this.innerDeindent(); } /** * Position returns this shortcode's detailed position. * Note that this information may be expensive to calculate, * so only use this in error situations. */ async position() { if (!this._pos) { this._pos = await this._page.posOffset(this._posOffset); } return this._pos; } /** * Scratch returns a scratch-pad scoped for this shortcode. * This can be used as a temporary storage for variables, counters etc. */ scratch() { if (!this._scratch) { this._scratch = new scratch_1.Scratch(); } return this._scratch; } /** * Get is a convenience method to look up shortcode parameters by its key */ get(key) { if (!this._params) { return null; } if (Array.isArray(this._params)) { if (typeof key === 'number') { if (key >= this._params.length) { return ''; } return this._params[key]; } // If key is string but params is array, return null return null; } else if (typeof this._params === 'object') { if (typeof key === 'string') { return this._params[key] || ''; } // If key is number but params is object, return null return null; } return null; } Get(key) { return this.get(key); } } exports.ShortcodeWithPage = ShortcodeWithPage; //# sourceMappingURL=shortcode.js.map