edge.js
Version:
Template engine
138 lines (137 loc) • 4.51 kB
JavaScript
import { d as parseJsArg, g as __toESM, h as __export, i as htmlSafe, l as isSubsetOf, p as unallowedExpression, r as escape, t as require_js_stringify } from "../../js-stringify-IDe7srW5.js";
import string from "@poppinss/utils/string";
import { expressions } from "edge-parser";
import { EdgeError } from "edge-error";
import lodash from "@poppinss/utils/lodash";
import inspect from "@poppinss/inspect";
const setTag = {
block: false,
seekable: true,
tagName: "set",
noNewLine: true,
compile(parser, buffer, token) {
const parsed = parseJsArg(parser, token);
isSubsetOf(parsed, [expressions.SequenceExpression], () => {
throw unallowedExpression(`"${token.properties.jsArg}" is not a valid key-value pair for the @slot tag`, token.filename, parser.utils.getExpressionLoc(parsed));
});
if (parsed.expressions.length < 2 || parsed.expressions.length > 3) throw new EdgeError("@set tag accepts a minimum of 2 or maximum or 3 arguments", "E_INVALID_ARGUMENTS_COUNT", {
line: parsed.loc.start.line,
col: parsed.loc.start.column,
filename: token.filename
});
let collection;
let key;
let value;
if (parsed.expressions.length === 3) {
collection = parsed.expressions[0];
key = parsed.expressions[1];
value = parsed.expressions[2];
} else {
key = parsed.expressions[0];
value = parsed.expressions[1];
}
if (collection) {
buffer.writeExpression(`template.setValue(${parser.utils.stringify(collection)}, '${key.value}', ${parser.utils.stringify(value)})`, token.filename, token.loc.start.line);
return;
}
const expression = parser.stack.has(key.value) ? `${key.value} = ${parser.utils.stringify(value)}` : `let ${key.value} = ${parser.utils.stringify(value)}`;
buffer.writeExpression(expression, token.filename, token.loc.start.line);
parser.stack.defineVariable(key.value);
},
boot(template) {
template.macro("setValue", lodash.set);
}
};
const superTag = {
block: false,
seekable: false,
tagName: "super",
compile(_, __, token) {
throw new EdgeError("@super tag must appear as top level tag inside the @section tag", "E_ORPHAN_SUPER_TAG", {
line: token.loc.start.line,
col: token.loc.start.col,
filename: token.filename
});
}
};
const layoutTag = {
block: false,
seekable: true,
tagName: "layout",
noNewLine: true,
compile() {}
};
const sectionTag = {
block: true,
seekable: true,
tagName: "section",
compile(parser, buffer, token) {
token.children.forEach((child) => parser.processToken(child, buffer));
}
};
var main_exports = /* @__PURE__ */ __export({
layout: () => layoutTag,
section: () => sectionTag,
set: () => setTag,
super: () => superTag
});
var import_js_stringify = /* @__PURE__ */ __toESM(require_js_stringify(), 1);
const { string: prettyPrintHtml } = inspect;
const GLOBALS = {
nl2br: (value) => {
if (!value) return;
return String(value).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br>");
},
inspect: (value) => {
return htmlSafe(prettyPrintHtml.html(value));
},
truncate: (value, length = 20, options) => {
options = options || {};
return string.truncate(value, length, {
completeWords: options.completeWords !== void 0 ? options.completeWords : !options.strict,
suffix: options.suffix
});
},
raise: (message, options) => {
if (!options) throw new Error(message);
else throw new EdgeError(message, "E_RUNTIME_EXCEPTION", options);
},
excerpt: (value, length = 20, options) => {
options = options || {};
return string.excerpt(value, length, {
completeWords: options.completeWords !== void 0 ? options.completeWords : !options.strict,
suffix: options.suffix
});
},
e: escape,
stringify: import_js_stringify.default,
safe: htmlSafe,
camelCase: string.camelCase,
snakeCase: string.snakeCase,
dashCase: string.dashCase,
pascalCase: string.pascalCase,
capitalCase: string.capitalCase,
sentenceCase: string.sentenceCase,
dotCase: string.dotCase,
noCase: string.noCase,
titleCase: string.titleCase,
pluralize: string.pluralize,
sentence: string.sentence,
prettyMs: string.milliseconds.format,
toMs: string.milliseconds.parse,
prettyBytes: string.bytes.format,
toBytes: string.bytes.parse,
ordinal: string.ordinal
};
const migrate = (edge) => {
edge.compat = true;
edge.compiler.compat = true;
edge.asyncCompiler.compat = true;
Object.keys(GLOBALS).forEach((name) => {
edge.global(name, GLOBALS[name]);
});
Object.keys(main_exports).forEach((name) => {
edge.registerTag(main_exports[name]);
});
};
export { migrate };