UNPKG

@openinc/parse-server-opendash

Version:
143 lines (142 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IMPORT_PRESETS = exports.DEFAULT_FILE_CONFIG = exports.DEFAULT_IMPORT_CONFIG = exports.FILE_CONFIG_ALLOWED_KEYS = exports.FOLDER_CONFIG_ALLOWED_KEYS = void 0; exports.createImportConfig = createImportConfig; exports.sanitizeFolderConfig = sanitizeFolderConfig; exports.sanitizeFileConfig = sanitizeFileConfig; // --------------------------------------------------------------------------- // Allowed Config Keys (central source of truth) // --------------------------------------------------------------------------- exports.FOLDER_CONFIG_ALLOWED_KEYS = [ "title", "order", "icon", "files", "feature", ]; exports.FILE_CONFIG_ALLOWED_KEYS = [ "order", "icon", "title", "locationPattern", "locations", "feature", ]; /** * Default configuration for documentation import */ exports.DEFAULT_IMPORT_CONFIG = { organization: "open-inc", repository: "documentation", branch: process.env.OI_DOCUMENTATION_GITHUB_BRANCH || "main", rootPath: "docs", // Start from the docs folder fileFilter: { // Only include markdown and JSON files by default includeExtensions: ["md", "json"], // Exclude common non-documentation files excludeExtensions: [], // Path patterns (examples) // includePaths: ["docs/**", "*.md"], // excludePaths: ["node_modules/**", ".git/**", "**/.DS_Store"], }, defaultFolderConfig: { order: 999, }, }; exports.DEFAULT_FILE_CONFIG = { order: 999, icon: "fa:file", }; /** * Preset configurations for different use cases */ exports.IMPORT_PRESETS = { // Documentation + configuration DOCS_AND_CONFIG: { includeExtensions: ["md", "json"], defaultFolderConfig: { order: 999, icon: "fa:folder", }, }, // Media files MEDIA_ONLY: { includeExtensions: ["png", "jpg", "jpeg", "gif", "svg", "pdf"], defaultFolderConfig: { template: "media-gallery", tags: ["media", "assets"], }, }, }; /** * Helper function to merge configuration */ function createImportConfig(overrides = {}, preset) { const baseConfig = { ...exports.DEFAULT_IMPORT_CONFIG }; // Apply preset if provided if (preset && exports.IMPORT_PRESETS[preset]) { baseConfig.fileFilter = { ...baseConfig.fileFilter, ...exports.IMPORT_PRESETS[preset], }; // Apply preset's default folder config if it exists if (exports.IMPORT_PRESETS[preset].defaultFolderConfig) { baseConfig.defaultFolderConfig = { ...baseConfig.defaultFolderConfig, ...exports.IMPORT_PRESETS[preset].defaultFolderConfig, }; } } // Apply user overrides const finalConfig = { ...baseConfig, ...overrides, fileFilter: { ...baseConfig.fileFilter, ...overrides.fileFilter, }, defaultFolderConfig: { ...baseConfig.defaultFolderConfig, ...overrides.defaultFolderConfig, }, }; // Ensure required fields are present if (!finalConfig.token) { finalConfig.token = process.env.OI_DOCUMENTATION_GITHUB_ACCESS_TOKEN || ""; } return finalConfig; } /** * Sanitize a raw (parsed) folder config object by picking only allowed keys * and recursively sanitizing file configs. */ function sanitizeFolderConfig(raw) { if (!raw || typeof raw !== "object") return {}; const folder = {}; for (const key of exports.FOLDER_CONFIG_ALLOWED_KEYS) { if (key in raw) { // files handled specially below if (key !== "files") folder[key] = raw[key]; } } if (raw.files && typeof raw.files === "object") { const files = {}; for (const [base, cfg] of Object.entries(raw.files)) { files[base] = sanitizeFileConfig(cfg); } folder.files = files; } return folder; } function sanitizeFileConfig(raw) { if (!raw || typeof raw !== "object") return {}; const fileCfg = {}; for (const key of exports.FILE_CONFIG_ALLOWED_KEYS) { if (key in raw) fileCfg[key] = raw[key]; } return fileCfg; }