@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
124 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateInfo = void 0;
exports.loadTemplate = loadTemplate;
exports.resolveTemplateType = resolveTemplateType;
exports.isShortcode = isShortcode;
const type_1 = require("../type");
class TemplateInfo {
constructor(name, template, fi) {
this.name = name;
this.template = template;
this.fi = fi;
}
/**
* Get identifier base
*/
identifierBase() {
return this.name;
}
/**
* Create error with file context
*/
errWithFileContext(what, err) {
const message = `${what}: ${err.message}`;
const templateError = new type_1.TemplateError(message, 'TEMPLATE_FILE_ERROR');
// Add file information to error
Object.defineProperty(templateError, 'fileName', {
value: this.name,
enumerable: true
});
Object.defineProperty(templateError, 'fileContent', {
value: this.template,
enumerable: true
});
return templateError;
}
/**
* Check if template info is zero value
*/
isZero() {
return this.name === '';
}
/**
* Create a copy of this template info
*/
copy() {
return new TemplateInfo(this.name, this.template, this.fi);
}
/**
* Get template content without BOM
*/
getCleanContent() {
return removeLeadingBOM(this.template);
}
}
exports.TemplateInfo = TemplateInfo;
/**
* Load template from file meta info
*/
async function loadTemplate(name, fim) {
let file = null;
try {
file = await fim.open();
const fileInfo = await file.stat();
const buffer = new Uint8Array(fileInfo.size());
const result = await file.read(buffer);
const content = new TextDecoder('utf-8').decode(result.buffer);
const cleanContent = removeLeadingBOM(content);
return new TemplateInfo(name, cleanContent, fim);
}
catch (error) {
const templateInfo = new TemplateInfo(name, '', fim);
throw templateInfo.errWithFileContext('failed to load template', error);
}
finally {
if (file) {
try {
await file.close();
}
catch (closeError) {
console.warn(`Failed to close template file: ${closeError}`);
}
}
}
}
/**
* Remove leading BOM (Byte Order Mark) from string
*/
function removeLeadingBOM(s) {
const BOM = '\ufeff';
for (let i = 0; i < s.length; i++) {
const char = s[i];
if (i === 0 && char !== BOM) {
return s;
}
if (i > 0) {
return s.substring(i);
}
}
return s;
}
/**
* Resolve template type based on template name
* @param name - Template name/path
* @returns Template type
*/
function resolveTemplateType(name) {
if (isShortcode(name)) {
return type_1.TemplateType.TypeShortcode;
}
if (name.includes('_partials/')) {
return type_1.TemplateType.TypePartial;
}
return type_1.TemplateType.TypeUndefined;
}
/**
* Check if template name indicates a shortcode
* @param name - Template name/path
* @returns true if it's a shortcode template
*/
function isShortcode(name) {
return name.includes('_shortcodes/');
}
//# sourceMappingURL=info.js.map