@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
109 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Classifier = void 0;
const fileinfo_1 = require("./fileinfo");
class Classifier {
constructor(files) {
this.files = files;
}
static async newClassifier(fis) {
const fsFiles = [];
for (const f of fis) {
try {
const file = (0, fileinfo_1.newFileInfo)(f);
fsFiles.push(file);
}
catch (error) {
throw new Error(`Failed to create FileInfo: ${error}`);
}
}
const fsm = new Classifier(fsFiles);
fsm.sort();
return fsm;
}
/**
* Sort files using the same logic as Go version
*/
sort() {
this.files.sort((i, j) => {
const fi = i;
const fj = j;
const fimi = fi.fileInfo();
const fimj = fj.fileInfo();
// Directory check - directories come first
if (fimi.isDir() !== fimj.isDir()) {
return fimi.isDir() ? -1 : 1;
}
const pii = fi.paths();
const pij = fj.paths();
if (pii) {
// Pull bundles to the top
if (fi.isBundle() !== fj.isBundle()) {
return fi.isBundle() ? -1 : 1;
}
const exti = fi.ext();
const extj = fj.ext();
if (exti !== extj) {
// This pulls .md above .html
return exti > extj ? -1 : 1;
}
const basei = pii.base();
const basej = pij.base();
if (basei !== basej) {
return basei < basej ? -1 : 1;
}
}
// Final fallback: sort by filename
return fimi.name() < fimj.name() ? -1 : 1;
});
}
/**
* GetLeaf - returns the first leaf bundle file
*/
getLeaf() {
for (const file of this.files) {
if (file.isLeafBundle()) {
return file;
}
}
return null;
}
/**
* Get all files
*/
getFiles() {
return this.files;
}
/**
* Get number of files
*/
length() {
return this.files.length;
}
/**
* Get file at index
*/
at(index) {
return this.files[index];
}
/**
* Filter files by predicate
*/
filter(predicate) {
return this.files.filter(predicate);
}
/**
* Find first file matching predicate
*/
find(predicate) {
return this.files.find(predicate);
}
/**
* Check if empty
*/
isEmpty() {
return this.files.length === 0;
}
}
exports.Classifier = Classifier;
//# sourceMappingURL=classifier.js.map