Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/llms-txt-hub / apps/web/lib/resources.ts |
| Date | Not specified in source |
| Repository | Janady13/llms-txt-hub |
| Revision | e0f4bf978d4df168369c7601d9c5f5ea89e84377 |
| Branch | main |
| Path or archive member | apps/web/lib/resources.ts |
| Original SHA-256 | 8fbad844359e9b96401dea813a3fef5c8c6ad2f31ec48caa111763846cfb2df1 |
| Git blob SHA | 8117d0b9f68e8db8e578756da0e048a4de24cffe |
| Reading-copy SHA-256 | 8fbad844359e9b96401dea813a3fef5c8c6ad2f31ec48caa111763846cfb2df1 |
Recovered source transcript
Download complete source text ↓109 lines · 2.6 KB
import fs from 'node:fs'
import path from 'node:path'
import { logger } from '@thedaviddias/logging'
import matter from 'gray-matter'
// Inline server-only function to avoid import issues
function resolveFromRoot(...paths: string[]): string {
return path.resolve(process.cwd(), ...paths)
}
const resourcesDirectory = resolveFromRoot('content/resources')
export interface Resource {
slug: string
title: string
description: string
link: string
type: string
}
/**
* Type guard to validate resource data
*
* @param data - The data to validate
* @returns True if the data is a valid resource
*/
function isValidResourceData(data: any): data is Omit<Resource, 'slug'> {
return (
typeof data === 'object' &&
data !== null &&
typeof data.title === 'string' &&
typeof data.description === 'string' &&
typeof data.link === 'string' &&
typeof data.type === 'string'
)
}
/**
* Get all resources from the resources directory
*/
export async function getAllResources(): Promise<Resource[]> {
if (!fs.existsSync(resourcesDirectory)) {
logger.error('Resources directory does not exist:', {
data: resourcesDirectory,
tags: { type: 'library' }
})
return []
}
const fileNames = fs.readdirSync(resourcesDirectory)
if (fileNames.length === 0) {
console.warn('No resource files found in directory')
return []
}
const resources = fileNames
.filter((fileName: string) => fileName.endsWith('.mdx'))
.map((fileName: string) => {
const slug = fileName.replace(/\.mdx$/, '')
const fullPath = path.join(resourcesDirectory, fileName)
const fileContents = fs.readFileSync(fullPath, 'utf8')
const { data } = matter(fileContents)
if (!isValidResourceData(data)) {
logger.warn('Invalid resource data found:', {
data: { slug, frontmatter: data },
tags: { type: 'library' }
})
return null
}
return {
slug,
...data
}
})
.filter((resource): resource is Resource => resource !== null)
return resources
}
/**
* Get a specific resource by slug
*/
export async function getResourceBySlug(slug: string): Promise<Resource | null> {
const fullPath = path.join(resourcesDirectory, `${slug}.mdx`)
if (!fs.existsSync(fullPath)) {
return null
}
const fileContents = fs.readFileSync(fullPath, 'utf8')
const { data } = matter(fileContents)
if (!isValidResourceData(data)) {
logger.warn('Invalid resource data found for slug:', {
data: { slug, frontmatter: data },
tags: { type: 'library' }
})
return null
}
return {
slug,
...data
}
}