@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
221 lines • 7.74 kB
JavaScript
"use strict";
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.PathType = exports.FileInfo = void 0;
exports.newFileInfo = newFileInfo;
const paths_1 = require("../../../domain/paths");
Object.defineProperty(exports, "PathType", { enumerable: true, get: function () { return paths_1.PathType; } });
const crypto = __importStar(require("crypto"));
const path = __importStar(require("path"));
const log_1 = require("../../../../pkg/log");
const log = (0, log_1.getDomainLogger)('content', { component: 'FileInfo' });
const contentFileExtensions = [
'.md',
];
const contentFileExtensionsSet = new Set(contentFileExtensions);
/**
* Check if extension is a content file extension
*/
function isContentExt(ext) {
return contentFileExtensionsSet.has(ext);
}
class FileInfo {
constructor(fi, relName) {
this.lazyInitDone = false;
this.fileMetaInfo = fi;
// Use paths domain to parse path - exact replica of Go version:
// path: paths.Parse(files.ComponentFolderContent, relName)
const pathProcessor = paths_1.PathDomain.createProcessor();
this.pathInfo = pathProcessor.parse(paths_1.PATH_CONSTANTS.COMPONENT_FOLDER_CONTENT, relName);
this.bundleType = paths_1.PathType.File;
this.determineBundleType();
}
// Static factory method - equivalent to NewFileInfo in Go
static newFileInfo(fi) {
const relName = paths_1.PATH_CONSTANTS.normalizePath(fi.relativeFilename());
return new FileInfo(fi, relName);
}
// PageFile method from Go version
pageFile() {
return this;
}
// ShiftToResource method from Go version
shiftToResource() {
if (this.isContent()) {
this.bundleType = paths_1.PathType.ContentResource;
}
}
// FileOverlap interface methods
relPath() {
// Exact replica of Go version: filepath.Join(fi.p().Dir()[1:], fi.p().Name())
const dir = this.pathInfo.dir();
const dirWithoutLeadingSlash = dir.startsWith('/') ? dir.substring(1) : dir;
return path.join(dirWithoutLeadingSlash, this.pathInfo.name());
}
section() {
return this.pathInfo.section();
}
isZero() {
return this.fileMetaInfo == null;
}
// FileWithoutOverlap interface methods
filename() {
// Exact replica of Go version: fi.FileMetaInfo.FileName()
return this.fileMetaInfo.fileName();
}
dir() {
// Exact replica of Go version: fi.pathToDir(fi.p().Dir())
return this.pathToDir(this.pathInfo.dir());
}
ext() {
// Exact replica of Go version: fi.p().Ext()
return this.pathInfo.ext();
}
logicalName() {
// Exact replica of Go version: fi.p().Name()
return this.pathInfo.name();
}
baseFileName() {
// Exact replica of Go version: fi.p().NameNoExt()
return this.pathInfo.nameNoExt();
}
translationBaseName() {
// Exact replica of Go version: fi.p().NameNoIdentifier()
return this.pathInfo.nameNoIdentifier();
}
contentBaseName() {
// Exact replica of Go version: fi.p().BaseNameNoIdentifier()
return this.pathInfo.baseNameNoIdentifier();
}
baseNameNoIdentifier() {
// Exact replica of Go version: fi.p().BaseNameNoIdentifier()
return this.pathInfo.baseNameNoIdentifier();
}
root() {
// Return the root directory - for FileInfo this would be the base directory
return this.pathInfo.dir() || '/';
}
uniqueID() {
this.init();
return this.uniqueIDCache;
}
fileInfo() {
return this.fileMetaInfo;
}
// Additional methods from Go version
type() {
const sect = this.section();
if (sect !== '') {
return sect;
}
return 'page';
}
async open() {
return await this.fileMetaInfo.open();
}
paths() {
// Exact replica of Go version: fi.p()
return this.pathInfo;
}
path() {
// Exact replica of Go version: fi.p().Path()
return this.pathInfo.path();
}
toString() {
// Exact replica of Go version: fi.BaseFileName()
return this.baseFileName();
}
// Bundle type methods - exact replicas from Go version using PathType
getBundleType() {
return this.bundleType;
}
isBundle() {
return this.bundleType >= paths_1.PathType.Leaf;
}
isLeafBundle() {
return this.bundleType === paths_1.PathType.Leaf;
}
isBranchBundle() {
return this.bundleType === paths_1.PathType.Branch;
}
isContentResource() {
return this.bundleType === paths_1.PathType.ContentResource;
}
isContent() {
return this.bundleType >= paths_1.PathType.ContentResource;
}
// Private helper methods
init() {
// Exact replica of Go version lazy initialization
if (!this.lazyInitDone) {
this.lazyInitDone = true;
// helpers.MD5String(filepath.ToSlash(fi.RelPath()))
const normalizedPath = this.relPath().replace(/\\/g, '/');
this.uniqueIDCache = crypto.createHash('md5').update(normalizedPath).digest('hex');
}
}
pathToDir(s) {
// Exact replica of Go version pathToDir method
if (s === '') {
return s;
}
// filepath.FromSlash(s[1:] + "/")
return path.normalize((s.substring(1) + '/'));
}
determineBundleType() {
// Exact replica of Go version bundle type determination logic
const isContent = isContentExt(this.pathInfo.ext());
if (isContent) {
const nameNoId = this.pathInfo.nameNoIdentifier();
switch (nameNoId) {
case 'index':
this.bundleType = paths_1.PathType.Leaf;
break;
case '_index':
this.bundleType = paths_1.PathType.Branch;
break;
default:
this.bundleType = paths_1.PathType.ContentSingle;
break;
}
}
// If not content, bundleType remains as File (initialized in constructor)
}
}
exports.FileInfo = FileInfo;
function newFileInfo(fi) {
return FileInfo.newFileInfo(fi);
}
//# sourceMappingURL=fileinfo.js.map