@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
223 lines • 7.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathComponentsUtils = exports.PathComponentsFactory = exports.LowHighImpl = exports.PathPositionsImpl = exports.PathComponentsImpl = void 0;
const type_1 = require("../type");
/**
* PathComponentsImpl implements the PathComponents interface
* Represents the parsed components of a path with position information
*/
class PathComponentsImpl {
constructor(original, normalized, positions, identifiers, bundleType, disabled = false, component) {
this.original = original;
this.normalized = normalized;
this.positions = positions;
this.identifiers = identifiers;
this.bundleType = bundleType;
this.disabled = disabled;
this.component = component;
}
/**
* Create a new PathComponents with updated bundle type
*/
withBundleType(bundleType) {
return new PathComponentsImpl(this.original, this.normalized, this.positions, this.identifiers, bundleType, this.disabled);
}
/**
* Create a new PathComponents with disabled flag
*/
withDisabled(disabled) {
return new PathComponentsImpl(this.original, this.normalized, this.positions, this.identifiers, this.bundleType, disabled);
}
/**
* Check if this path has any identifiers
*/
hasIdentifiers() {
return this.identifiers.length > 0;
}
/**
* Get the first identifier (usually the file extension)
*/
firstIdentifier() {
return this.identifiers.length > 0 ? this.identifiers[0] : null;
}
/**
* Get the last identifier
*/
lastIdentifier() {
return this.identifiers.length > 0 ? this.identifiers[this.identifiers.length - 1] : null;
}
/**
* Get identifier at specific index
*/
getIdentifier(index) {
return index >= 0 && index < this.identifiers.length ? this.identifiers[index] : null;
}
/**
* Check if this is a content component
*/
isContentComponent(component) {
return component === 'content' || component === 'archetypes';
}
/**
* Create a copy of PathComponents
*/
clone() {
return new PathComponentsImpl(this.original, this.normalized, { ...this.positions }, [...this.identifiers], this.bundleType, this.disabled, this.component);
}
/**
* Convert to string representation for debugging
*/
toString() {
return `PathComponents{original="${this.original}", normalized="${this.normalized}", bundleType=${type_1.PathType[this.bundleType]}, identifiers=${this.identifiers.length}}`;
}
}
exports.PathComponentsImpl = PathComponentsImpl;
/**
* PathPositionsImpl implements the PathPositions interface
*/
class PathPositionsImpl {
constructor(containerLow = -1, containerHigh = -1, sectionHigh = -1, identifierLanguage = -1) {
this.containerLow = containerLow;
this.containerHigh = containerHigh;
this.sectionHigh = sectionHigh;
this.identifierLanguage = identifierLanguage;
}
/**
* Reset all positions to default values
*/
reset() {
this.containerLow = -1;
this.containerHigh = -1;
this.sectionHigh = -1;
this.identifierLanguage = -1;
}
/**
* Check if container positions are set
*/
hasContainer() {
return this.containerLow !== -1 && this.containerHigh !== -1;
}
/**
* Check if section position is set
*/
hasSection() {
return this.sectionHigh > 0;
}
/**
* Check if language identifier position is set
*/
hasLanguageIdentifier() {
return this.identifierLanguage !== -1;
}
/**
* Create a copy of PathPositions
*/
clone() {
return new PathPositionsImpl(this.containerLow, this.containerHigh, this.sectionHigh, this.identifierLanguage);
}
}
exports.PathPositionsImpl = PathPositionsImpl;
/**
* LowHighImpl implements the LowHigh interface
*/
class LowHighImpl {
constructor(low, high) {
this.low = low;
this.high = high;
if (low > high) {
throw new Error(`Invalid range: low (${low}) must be <= high (${high})`);
}
}
/**
* Get the length of the range
*/
length() {
return this.high - this.low;
}
/**
* Check if the range is empty
*/
isEmpty() {
return this.low === this.high;
}
/**
* Check if a position is within this range
*/
contains(position) {
return position >= this.low && position < this.high;
}
/**
* Create a substring from the given string using this range
*/
substring(str) {
return str.substring(this.low, this.high);
}
/**
* Convert to string representation
*/
toString() {
return `[${this.low}, ${this.high})`;
}
}
exports.LowHighImpl = LowHighImpl;
/**
* Factory functions for creating path components
*/
class PathComponentsFactory {
/**
* Create empty PathComponents
*/
static createEmpty(original = '', bundleType = type_1.PathType.File) {
return new PathComponentsImpl(original, original, new PathPositionsImpl(), [], bundleType, false);
}
/**
* Create PathComponents from basic information
*/
static create(original, normalized, bundleType = type_1.PathType.File) {
return new PathComponentsImpl(original, normalized || original, new PathPositionsImpl(), [], bundleType, false);
}
/**
* Create PathComponents with full information
*/
static createFull(original, normalized, positions, identifiers, bundleType, disabled = false) {
return new PathComponentsImpl(original, normalized, positions, identifiers, bundleType, disabled);
}
}
exports.PathComponentsFactory = PathComponentsFactory;
/**
* Utility functions for working with path components
*/
class PathComponentsUtils {
/**
* Extract string value using LowHigh range
*/
static extractString(str, range) {
return str.substring(range.low, range.high);
}
/**
* Extract multiple string values using LowHigh ranges
*/
static extractStrings(str, ranges) {
return ranges.map(range => str.substring(range.low, range.high));
}
/**
* Find identifier index by position
*/
static findIdentifierIndex(identifiers, position) {
for (let i = 0; i < identifiers.length; i++) {
const range = identifiers[i];
if (position >= range.low && position < range.high) {
return i;
}
}
return -1;
}
/**
* Merge two PathComponents (useful for inheritance scenarios)
*/
static merge(base, override) {
return new PathComponentsImpl(override.original ?? base.original, override.normalized ?? base.normalized, override.positions ?? base.positions, override.identifiers ?? base.identifiers, override.bundleType ?? base.bundleType, override.disabled ?? base.disabled);
}
}
exports.PathComponentsUtils = PathComponentsUtils;
//# sourceMappingURL=pathcomponents.js.map