|
| 1 | +'use server' |
| 2 | + |
| 3 | +import { promises as fs } from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { getPostData } from '../content' |
| 7 | + |
| 8 | +// Utility to clean frontmatter delimiters and adjust heading levels |
| 9 | +const cleanFrontmatter = (raw: string): string => |
| 10 | + raw.trim().replace(/---\n/g, '').replace(/\n## /g, '\n### ') |
| 11 | + |
| 12 | +// Build the About section for both markdown and llms-full contents |
| 13 | +const buildAboutSection = async (): Promise<string> => { |
| 14 | + let llms = '---\n/about\n---\n\n' |
| 15 | + |
| 16 | + try { |
| 17 | + const data = await getPostData('About') |
| 18 | + if (data !== null) { |
| 19 | + const content = cleanFrontmatter(data.contentRaw) |
| 20 | + llms += `# About\n\n${content}` |
| 21 | + return llms |
| 22 | + } |
| 23 | + } |
| 24 | + catch { |
| 25 | + // Fallback content |
| 26 | + } |
| 27 | + |
| 28 | + const fallback = 'This is a personal blog with technical articles and thoughts.' |
| 29 | + llms += `# About\n\n${fallback}` |
| 30 | + return llms |
| 31 | +} |
| 32 | + |
| 33 | +// Build the Posts section |
| 34 | +const buildPostsSection = ( |
| 35 | + posts: PostListData[], |
| 36 | + siteUrl: string, |
| 37 | +): { md: string, llms: string } => { |
| 38 | + const mdLines: string[] = ['## Posts', ''] |
| 39 | + const llmsParts: string[] = [] |
| 40 | + |
| 41 | + posts.forEach((post) => { |
| 42 | + const url = `${siteUrl}/${post.slug}` |
| 43 | + const { title, date, redirect } = post.frontmatter |
| 44 | + const abstract = post.postAbstract?.replace(/\n/g, ' ') ?? '' |
| 45 | + |
| 46 | + mdLines.push(`- [${title}](${url}): ${abstract} (${date})`) |
| 47 | + llmsParts.push(`---\n${url}\n---\n`) |
| 48 | + |
| 49 | + const redirected = Boolean(redirect?.trim()) |
| 50 | + |
| 51 | + if (redirected) { |
| 52 | + llmsParts.push( |
| 53 | + `# ${title} (Redirect)\n\nThis post is a redirect to ${redirect}`, |
| 54 | + ) |
| 55 | + } |
| 56 | + else { |
| 57 | + const content = post.contentRaw.trim().replace(/---\n/g, '') |
| 58 | + llmsParts.push(`#${title}\n\n${content}`) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + return { md: mdLines.join('\n'), llms: llmsParts.join('\n') } |
| 63 | +} |
| 64 | + |
| 65 | +// Build Friends & Posts archive section |
| 66 | +const buildFriendsSection = async ( |
| 67 | + siteUrl: string, |
| 68 | + author: string, |
| 69 | +): Promise<{ md: string, llms: string }> => { |
| 70 | + const md = [ |
| 71 | + `- [About](${siteUrl}/about): About page with personal information for ${author}`, |
| 72 | + `- [Friends](${siteUrl}/friends): A curated list of linked blogs and friend sites`, |
| 73 | + `- [All Posts](${siteUrl}/posts): Chronological listing of all blog entries`, |
| 74 | + ].join('\n') |
| 75 | + |
| 76 | + let llms = '---\n/friends\n---\n\n' |
| 77 | + try { |
| 78 | + const data = await getPostData('Friends') |
| 79 | + if (data !== null) { |
| 80 | + const content = data.contentRaw.trim().replace(/---\n/g, '') |
| 81 | + llms += `# Friends\n\n${content}` |
| 82 | + return { md, llms } |
| 83 | + } |
| 84 | + } |
| 85 | + catch { |
| 86 | + // Fallback |
| 87 | + } |
| 88 | + |
| 89 | + llms += '# Friends\n\nA curated list of linked blogs and friend sites.' |
| 90 | + return { md, llms } |
| 91 | +} |
| 92 | + |
| 93 | +// Main function to generate llms.txt and llms-full.txt |
| 94 | +const generateLLMsTXTs = async ( |
| 95 | + posts: PostListData[], |
| 96 | + config: Config, |
| 97 | +): Promise<void> => { |
| 98 | + const { siteUrl, title, subTitle, description, author, slogan, anilist_username } = config |
| 99 | + const showAnime = Boolean(anilist_username?.trim()) |
| 100 | + |
| 101 | + // Header |
| 102 | + const headerMd = [ |
| 103 | + `# ${title} - ${subTitle}`, |
| 104 | + '', |
| 105 | + `> ${description ?? 'Another SuzuBlog based personal blog.'} - ${author.name}. Personal slogan: ${slogan}`, |
| 106 | + '', |
| 107 | + `This website is using SuzuBlog, which is developed by [ZL Asica](https://zla.pub/) (she/her) based on Next.js.`, |
| 108 | + '', |
| 109 | + ].join('\n') |
| 110 | + |
| 111 | + let markdownContent = `${headerMd}\n` |
| 112 | + const llmsContents: string[] = [] |
| 113 | + |
| 114 | + // About |
| 115 | + const about = await buildAboutSection() |
| 116 | + llmsContents.push(about) |
| 117 | + |
| 118 | + // Posts |
| 119 | + const postsSection = buildPostsSection(posts, siteUrl) |
| 120 | + markdownContent += `\n${postsSection.md}\n` |
| 121 | + llmsContents.push(postsSection.llms) |
| 122 | + |
| 123 | + // Friends & archive |
| 124 | + const friendsSection = await buildFriendsSection(siteUrl, config.author.name) |
| 125 | + markdownContent += `\n## Optional\n\n${friendsSection.md}\n` |
| 126 | + llmsContents.push(friendsSection.llms) |
| 127 | + |
| 128 | + // Archive |
| 129 | + llmsContents.push( |
| 130 | + ['---', '/posts', '---', '', '# Posts', '', 'Chronological listing of all blog entries'].join('\n'), |
| 131 | + ) |
| 132 | + |
| 133 | + // Anime |
| 134 | + if (showAnime) { |
| 135 | + markdownContent += `- [Anime](${siteUrl}/about/anime): Automatically updated anime watch list from AniList\n` |
| 136 | + llmsContents.push( |
| 137 | + ['---', '/about/anime', '---', '', '# Anime', '', 'Automatically updated anime watch list from AniList'].join('\n'), |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + // Write to disk |
| 142 | + try { |
| 143 | + const outputDir = path.join(process.cwd(), 'public') |
| 144 | + await fs.mkdir(outputDir, { recursive: true }) |
| 145 | + await fs.writeFile(path.join(outputDir, 'llms.txt'), markdownContent, 'utf8') |
| 146 | + await fs.writeFile( |
| 147 | + path.join(outputDir, 'llms-full.txt'), |
| 148 | + llmsContents.join('\n\n'), |
| 149 | + 'utf8', |
| 150 | + ) |
| 151 | + // eslint-disable-next-line no-console |
| 152 | + console.info('llms.txt & llms-full.txt generated in /public 🎉') |
| 153 | + } |
| 154 | + catch (err) { |
| 155 | + console.error( |
| 156 | + 'Failed to write LLMs files:', |
| 157 | + err instanceof Error ? err.message : err, |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export default generateLLMsTXTs |
0 commit comments