UNPKG

@signalwire/docusaurus-plugin-llms-txt

Version:

Generate Markdown versions of Docusaurus HTML pages and an llms.txt index file

130 lines (129 loc) 5.13 kB
/** * Simplified cache management service * Uses focused modules for validation and I/O operations * @internal */ import path from 'path'; import packageJson from '../../package.json'; import { CACHE_FILENAME } from '../constants'; import { classifyRoute } from '../discovery/content-classifier'; import { routePathToHtmlPath } from '../discovery/route-filter'; import { PathManager, htmlPathToMdPath } from '../filesystem/paths'; import { CacheIO } from './cache-io'; import { isCachedRouteValid, calcConfigHash } from './cache-validation'; /** * Simplified cache management service * @internal */ export class CacheManager { constructor(siteDir, generatedFilesDir, config, logger, outDir, siteConfig) { this.pathManager = new PathManager(siteDir, config, outDir); this.siteConfig = siteConfig; const cacheDir = path.join(generatedFilesDir, 'docusaurus-plugin-llms-txt'); const cachePath = path.join(cacheDir, CACHE_FILENAME); this.cacheIO = new CacheIO(cachePath, logger); } /** Load cache from disk */ async loadCache() { return this.cacheIO.loadCache(); } /** Save cache to disk */ async saveCache(cache) { return this.cacheIO.saveCache(cache); } /** Check if cached route is still valid using focused validation */ async isCachedRouteValid(cachedRoute, currentConfig) { return isCachedRouteValid(cachedRoute, currentConfig, this.pathManager.directories); } /** Check if cached routes are available */ hasCachedRoutes(cache) { return cache.routes?.length > 0; } /** Check if the cached configuration matches the current configuration */ isCacheConfigValid(cache, currentConfig) { const currentConfigHash = calcConfigHash(currentConfig); return cache.configHash === currentConfigHash; } /** Calculate configuration hash */ calcConfigHash(options) { return calcConfigHash(options); } /** Create cached route info from routes with metadata for filtering */ createCachedRouteInfo(routes) { const cachedRoutes = routes.map((route) => { // Safe access to route properties - cast to access plugin info const pluginName = route.plugin?.name; const isGeneratedIndex = route.props && typeof route.props === 'object' && 'categoryGeneratedIndex' in route.props; const isVersioned = '__docusaurus_isVersioned' in route ? route.__docusaurus_isVersioned : undefined; const baseInfo = { path: route.path, htmlPath: routePathToHtmlPath(route.path, this.siteConfig?.baseUrl ?? '/', this.siteConfig?.trailingSlash), }; const pluginInfo = pluginName ? { plugin: pluginName } : {}; // Extract metadata for cache-based filtering const metadata = { contentType: classifyRoute(route), isVersioned, isGeneratedIndex, }; return { ...baseInfo, ...pluginInfo, ...metadata, }; }); return cachedRoutes; } /** Update cached route info with processing results */ updateCachedRouteWithDoc(cachedRoute, doc, hash, enableMarkdownFiles) { const baseUpdate = { ...cachedRoute, hash, title: doc.title, description: doc.description, }; if (enableMarkdownFiles && doc.markdownFile) { return { ...baseUpdate, markdownFile: doc.markdownFile }; } else if (enableMarkdownFiles && cachedRoute.htmlPath) { const mdPath = htmlPathToMdPath(cachedRoute.htmlPath, this.pathManager.directories.mdOutDir); const relativeMdPath = this.pathManager.getRelativeMarkdownPath(mdPath); return { ...baseUpdate, markdownFile: relativeMdPath }; } return baseUpdate; } /** Convert cached route info to DocInfo for processing */ cachedRouteToDocInfo(cachedRoute) { if (!cachedRoute.htmlPath || !cachedRoute.title) { return null; } const baseDocInfo = { routePath: cachedRoute.path, htmlPath: cachedRoute.htmlPath, title: cachedRoute.title, description: cachedRoute.description ?? '', }; const markdownInfo = cachedRoute.markdownFile ? { markdownFile: cachedRoute.markdownFile } : {}; return { ...baseDocInfo, ...markdownInfo }; } /** Update cache with processed routes and save to disk */ async updateCacheWithRoutes(config, cachedRoutes) { const updatedCache = { pluginVersion: packageJson.version, configHash: calcConfigHash(config), routes: cachedRoutes, }; await this.saveCache(updatedCache); } /** Get cache file information for debugging */ getCacheInfo() { return this.cacheIO.getCacheInfo(); } } export default CacheManager;