@signalwire/docusaurus-plugin-llms-txt
Version:
Generate Markdown versions of Docusaurus HTML pages and an llms.txt index file
94 lines (93 loc) • 3.45 kB
JavaScript
/**
* Content classification logic
* Classify routes as docs/blog/pages and apply include/exclude rules
*/
import { getContentConfig } from '../config';
import { DOCUSAURUS_BLOG_PLUGIN, DOCUSAURUS_PAGES_PLUGIN, CONTENT_TYPES, } from '../constants';
/**
* Classify a route by its plugin type, with fallback heuristics for routes without plugin info
* @internal
*/
export function classifyRoute(route) {
const plugin = route.plugin;
// If we have plugin info, use it
if (plugin?.name) {
switch (plugin.name) {
case DOCUSAURUS_BLOG_PLUGIN:
return CONTENT_TYPES.BLOG;
case DOCUSAURUS_PAGES_PLUGIN:
return CONTENT_TYPES.PAGES;
default:
return CONTENT_TYPES.DOCS;
}
}
// Fallback heuristics for routes without plugin info (e.g., versioned docs)
return classifyRouteByHeuristics(route);
}
/**
* Classify routes using reliable component indicators when plugin info is not available
* Only uses reliable indicators, avoiding fragile path/filename matching
* @internal
*/
function classifyRouteByHeuristics(route) {
// Check component type - docs typically use @theme/DocItem
if (route.component === '@theme/DocItem') {
return CONTENT_TYPES.DOCS;
}
// Check for reliable blog component indicators
if (route.component === '@theme/BlogListPage' ||
route.component === '@theme/BlogPostPage') {
return CONTENT_TYPES.BLOG;
}
// Home page is typically a page
if (route.path === '/') {
return CONTENT_TYPES.PAGES;
}
// If no reliable indicators, mark as unknown rather than guessing
return CONTENT_TYPES.UNKNOWN;
}
/**
* Determines if a route should be processed based on plugin configuration
* @internal
*/
export function shouldProcessRoute(route, options) {
const contentConfig = getContentConfig(options);
const routeType = classifyRoute(route);
// First check if this content type should be included
let shouldIncludeType = false;
switch (routeType) {
case CONTENT_TYPES.BLOG:
shouldIncludeType = contentConfig.includeBlog;
break;
case CONTENT_TYPES.PAGES:
shouldIncludeType = contentConfig.includePages;
break;
case CONTENT_TYPES.DOCS:
case CONTENT_TYPES.UNKNOWN:
default:
shouldIncludeType = contentConfig.includeDocs;
break;
}
if (!shouldIncludeType) {
return false;
}
// For docs routes, check versioned docs filtering
if ((routeType === CONTENT_TYPES.DOCS || routeType === CONTENT_TYPES.UNKNOWN) &&
contentConfig.includeVersionedDocs === false) {
// Check if this is a versioned docs route (not current version)
const isVersionedRoute = '__docusaurus_isVersioned' in route ?
route.__docusaurus_isVersioned :
undefined;
if (isVersionedRoute === true) {
return false; // Skip versioned docs when includeVersionedDocs is false
}
}
// Check if this is a generated category index page
if (contentConfig.includeGeneratedIndex === false) {
// Generated index pages have a categoryGeneratedIndex prop
if (route.props?.categoryGeneratedIndex !== undefined) {
return false; // Skip generated index pages when includeGeneratedIndex is false
}
}
return true;
}