@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
147 lines • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortByDefault = sortByDefault;
exports.sortByWeight = sortByWeight;
exports.sortByLanguage = sortByLanguage;
/**
* Sort pages by the default sort (by title).
* Exact replica of Go version: SortByDefault
*/
function sortByDefault(pages) {
pageBy(lessPageTitle).sort(pages);
}
/**
* Sort pages by weight.
* Exact replica of Go version: SortByWeight
*/
function sortByWeight(pages) {
pageBy(weight).sort(pages);
}
/**
* Sort pages by language.
* Exact replica of Go version: SortByLanguage
* TODO: Implement language-specific sorting
*/
function sortByLanguage(pages) {
// TODO
pageBy(lessPageTitle).sort(pages);
}
/**
* pageBy is a closure used in the Sort.Less method.
* Exact replica of Go version: type pageBy func(p1, p2 contenthub.Page) bool
*/
function pageBy(by) {
return new PageByWrapper(by);
}
/**
* PageByWrapper wraps the pageBy function and provides sort method
* Equivalent to Go's pageBy type with Sort method
*/
class PageByWrapper {
constructor(by) {
this.by = by;
}
/**
* Sort stable sorts the pages given the receiver's sort order.
* Exact replica of Go version: func (by pageBy) Sort(pages contenthub.Pages)
*/
sort(pages) {
const ps = new PageSorterImpl(pages, this.by);
// JavaScript's Array.sort is stable in modern engines
// This matches Go's sort.Stable behavior
stableSort(pages, ps);
}
}
/**
* Comparison functions - exact replicas from Go version
*/
/**
* Compare pages by title using collator string comparison
* Exact replica of Go version: lessPageTitle
*/
const lessPageTitle = (p1, p2) => {
return collatorStringCompare((p) => p.title(), p1, p2) < 0;
};
/**
* Compare pages by weight
* Exact replica of Go version: weight
*/
const weight = (p1, p2) => {
return p1.pageWeight() < p2.pageWeight();
};
/**
* String comparison with collation support
* Exact replica of Go version: collatorStringCompare
* Uses strings.Compare equivalent (simple lexicographic comparison)
* TODO: Use language collator instead of simple string comparison
*/
const collatorStringCompare = (getString, p1, p2) => {
// Exact replica of Go version: strings.Compare(getString(p1), getString(p2))
// TODO: use language collator
const str1 = getString(p1);
const str2 = getString(p2);
// strings.Compare in Go returns:
// -1 if str1 < str2
// 0 if str1 == str2
// 1 if str1 > str2
if (str1 < str2)
return -1;
if (str1 > str2)
return 1;
return 0;
};
/**
* PageSorterImpl implements the PageSorter interface for Pages
* Exact replica of Go version: pageSorter struct
*/
class PageSorterImpl {
constructor(pages, by) {
this.pages = pages;
this.by = by;
}
/**
* Get the length of pages array
* Exact replica of Go version: func (ps *pageSorter) Len() int
*/
len() {
return this.pages.length;
}
/**
* Swap two pages in the array
* Exact replica of Go version: func (ps *pageSorter) Swap(i, j int)
*/
swap(i, j) {
[this.pages[i], this.pages[j]] = [this.pages[j], this.pages[i]];
}
/**
* Compare two pages using the comparison function
* Exact replica of Go version: func (ps *pageSorter) Less(i, j int) bool
*/
less(i, j) {
return this.by(this.pages[i], this.pages[j]);
}
}
/**
* Stable sort implementation using the PageSorter interface
* Mimics Go's sort.Stable behavior
*/
function stableSort(pages, sorter) {
// Create array of indices with original order preserved for stability
const indices = Array.from({ length: sorter.len() }, (_, i) => i);
// Sort indices based on the comparison function
indices.sort((i, j) => {
const comparison = sorter.less(i, j);
if (comparison)
return -1;
if (sorter.less(j, i))
return 1;
// If neither is less than the other, preserve original order (stability)
return i - j;
});
// Reorder the pages array based on sorted indices
const originalPages = [...pages];
for (let i = 0; i < indices.length; i++) {
pages[i] = originalPages[indices[i]];
}
}
//# sourceMappingURL=sort.js.map