37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const escape_string_regexp_1 = __importDefault(require("escape-string-regexp"));
|
|
const listShortcuts_1 = __importDefault(require("./listShortcuts"));
|
|
/**
|
|
* Apply a shortcut of block to a template
|
|
* @param {string} content
|
|
* @param {Shortcut} shortcut
|
|
* @return {string}
|
|
*/
|
|
function applyShortcut(content, shortcut) {
|
|
const start = shortcut.getStart();
|
|
const end = shortcut.getEnd();
|
|
const tagStart = shortcut.getStartTag();
|
|
const tagEnd = shortcut.getEndTag();
|
|
const regex = new RegExp(`${(0, escape_string_regexp_1.default)(start)}([\\s\\S]*?[^\\$])${(0, escape_string_regexp_1.default)(end)}`, "g");
|
|
return content.replace(regex, (all, match) => {
|
|
return `{% ${tagStart} %}${match}{% ${tagEnd} %}`;
|
|
});
|
|
}
|
|
/**
|
|
* Replace shortcuts from blocks in a string
|
|
*
|
|
* @param {List<TemplateBlock>} engine
|
|
* @param {string} filePath
|
|
* @param {string} content
|
|
* @return {string}
|
|
*/
|
|
function replaceShortcuts(blocks, filePath, content) {
|
|
const shortcuts = (0, listShortcuts_1.default)(blocks, filePath);
|
|
return shortcuts.reduce(applyShortcut, content);
|
|
}
|
|
exports.default = replaceShortcuts;
|