UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

136 lines 4.85 kB
"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.ResourcePaths = void 0; const path = __importStar(require("path")); class ResourcePaths { constructor(dir = '', file = '', baseDirTarget = '', baseDirLink = '', targetBasePaths = [], baseUrl = '') { this.baseUrl = ''; this.dir = dir; this.file = file; this.baseDirTarget = baseDirTarget; this.baseDirLink = baseDirLink; this.targetBasePaths = targetBasePaths; this.baseUrl = baseUrl; } static newResourcePaths(targetPath, svc) { // Normalize the path to ensure it works across different OS, TODO: Windows/Linux const normalizedPath = targetPath.replace(/\\/g, '/'); const parsedPath = path.posix.parse(normalizedPath); let dir = parsedPath.dir; if (dir === '/') { dir = ''; } return new ResourcePaths(dir, parsedPath.base, svc.baseUrl(), svc.baseUrl(), [], svc.baseUrl()); } join(...parts) { let result = ''; for (let i = 0; i < parts.length; i++) { const part = parts[i]; if (!part) continue; if (result === '') { // 第一个有效部分,直接添加 result = part; } else { // 后续部分需要处理连接 const needsSlash = !result.endsWith('/') && !part.startsWith('/'); const hasDoubleSlash = result.endsWith('/') && part.startsWith('/'); if (hasDoubleSlash) { // 移除重复的斜杠 result += part.substring(1); } else if (needsSlash) { // 添加缺失的斜杠 result += '/' + part; } else { // 直接连接 result += part; } } } // 移除开头的斜杠 if (result.startsWith('/')) { result = result.substring(1); } // 移除结尾的斜杠(除非结果只是单个斜杠) if (result.endsWith('/') && result.length > 1) { result = result.substring(0, result.length - 1); } return result; } targetLink() { let link = this.join(this.baseDirLink, this.dir, this.file); if (link.startsWith("http://") || link.startsWith("https://")) { return link; } if (!link.startsWith('/')) { link = '/' + link; } return link; } targetPath() { return this.join(this.dir, this.file); } fromTargetPath(targetPath) { const normalizedPath = targetPath.replace(/\\/g, '/'); const parsedPath = path.posix.parse(normalizedPath); let dir = parsedPath.dir; if (dir === '/') { dir = ''; } return new ResourcePaths(dir, parsedPath.base, this.baseUrl, this.baseUrl, this.targetBasePaths, this.baseUrl); } // Interface implementations pathDir() { return this.dir; } pathBaseDirTarget() { return this.baseDirTarget; } pathBaseDirLink() { return this.baseDirLink; } pathTargetBasePaths() { return this.targetBasePaths; } pathFile() { return this.file; } } exports.ResourcePaths = ResourcePaths; //# sourceMappingURL=resourcepaths.js.map