@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
184 lines • 5.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseOf = void 0;
exports.newBaseOf = newBaseOf;
const type_1 = require("../type");
const path = __importStar(require("path"));
/**
* BaseOf value object for managing base template inheritance
* TypeScript version of Go's BaseOf struct
*/
class BaseOf {
constructor() {
this.baseof = new Map();
this.needsBaseof = new Map();
}
/**
* Get template search order for finding base templates
*/
getTemplateSearchOrder(templateName) {
const searchOrder = [];
// TODO: Make this more flexible based on template name
// Add _default/baseof.html (matching actual template file extension)
searchOrder.push(`${type_1.BASE_DEFAULT}/baseof.html`);
return searchOrder;
}
/**
* Get base template info by key
*/
getBaseOf(key) {
return this.baseof.get(key) || null;
}
/**
* Get template that needs base template by key
*/
getNeedsBaseOf(key) {
return this.needsBaseof.get(key) || null;
}
/**
* Add base template
*/
addBaseOf(key, info) {
this.baseof.set(key, info);
}
/**
* Add template that needs base template
*/
addNeedsBaseOf(key, info) {
this.needsBaseof.set(key, info);
}
/**
* Check if path is a base template path
*/
isBaseTemplatePath(filePath) {
return path.basename(filePath).includes(type_1.BASE_FILE_BASE);
}
/**
* Check if template needs base template
*/
needsBaseOf(name, rawContent) {
return !this.noBaseNeeded(name) && this.needsBaseTemplate(rawContent);
}
/**
* Check if template doesn't need base template
*/
noBaseNeeded(name) {
if (name.startsWith(type_1.PARTIALS_PREFIX)) {
return true;
}
return false;
}
/**
* Check if template content needs base template
* Returns true if the first non-comment template block is a define block
*/
needsBaseTemplate(templ) {
const baseTemplateDefineRe = /^{{-?\s*define/;
let idx = -1;
let inComment = false;
for (let i = 0; i < templ.length;) {
if (!inComment && templ.substring(i).startsWith('{{/*')) {
inComment = true;
i += 4;
}
else if (!inComment && templ.substring(i).startsWith('{{- /*')) {
inComment = true;
i += 6;
}
else if (inComment && templ.substring(i).startsWith('*/}}')) {
inComment = false;
i += 4;
}
else if (inComment && templ.substring(i).startsWith('*/ -}}')) {
inComment = false;
i += 6;
}
else {
const char = templ[i];
if (!inComment) {
if (templ.substring(i).startsWith('{{')) {
idx = i;
break;
}
else if (!/\s/.test(char)) {
break;
}
}
i++;
}
}
if (idx === -1) {
return false;
}
return baseTemplateDefineRe.test(templ.substring(idx));
}
/**
* Get all base templates
*/
getAllBaseOf() {
return new Map(this.baseof);
}
/**
* Get all templates that need base templates
*/
getAllNeedsBaseOf() {
return new Map(this.needsBaseof);
}
/**
* Clear all base template data
*/
clear() {
this.baseof.clear();
this.needsBaseof.clear();
}
/**
* Get statistics
*/
getStats() {
return {
baseOfCount: this.baseof.size,
needsBaseOfCount: this.needsBaseof.size,
};
}
}
exports.BaseOf = BaseOf;
/**
* Creates a new BaseOf instance
*/
function newBaseOf() {
return new BaseOf();
}
//# sourceMappingURL=baseof.js.map