1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| 'use strict'
hexo.extend.generator.register('ai-api', function (locals) { const posts = locals.posts const config = hexo.config const siteUrl = (config.url || '').replace(/\/$/, '') const resultList = [] const jsonList = [] const mdList = [] let llmsLines = [] let llmsFullLines = []
const siteTitle = config.title || 'Blog' const siteDesc = config.description || '' llmsLines.push(`# ${siteTitle}`, '') llmsLines.push(`> ${siteDesc}`, '') llmsLines.push('', '## Articles', '') llmsFullLines.push(`# ${siteTitle}`, '') llmsFullLines.push(`> ${siteDesc}`, '')
posts.sort('-date').forEach(function (post) { const slug = post.slug const title = post.title || slug const dateStr = post.date ? post.date.format('YYYY-MM-DD') : '' const updatedStr = post.updated ? post.updated.format('YYYY-MM-DD') : dateStr const categories = (post.categories && post.categories.data) ? post.categories.data.map(function (c) { return c.name }) : [] const tags = (post.tags && post.tags.data) ? post.tags.data.map(function (t) { return t.name }) : [] const desc = post.description || '' const isEncrypted = !!post.password const postUrl = siteUrl + '/' + post.path const apiJsonUrl = '/api/posts/' + slug + '.json' const apiMdUrl = '/api/posts/' + slug + '.md'
let rawMd = '' if (post.raw) { rawMd = post.raw.replace(/^---[\s\S]*?---\s*/, '') } else if (post.content) { rawMd = post.content }
const jsonObj = { title: title, slug: slug, date: dateStr, updated: updatedStr, categories: categories, tags: tags, description: desc, url: postUrl, api_json_url: siteUrl + apiJsonUrl, api_md_url: siteUrl + apiMdUrl, encrypted: isEncrypted, content: isEncrypted ? '' : rawMd }
const mdContent = isEncrypted ? '<!-- This post is encrypted. Content is not available. -->' : rawMd
const listEntry = { title: title, slug: slug, date: dateStr, categories: categories, tags: tags, description: desc, url: postUrl, api_json_url: siteUrl + apiJsonUrl, api_md_url: siteUrl + apiMdUrl, encrypted: isEncrypted } resultList.push(listEntry)
llmsLines.push(`- [${title}](${apiMdUrl}): ${desc || title}`)
if (!isEncrypted) { llmsFullLines.push('', '---', '') llmsFullLines.push(`# ${title}`, '') llmsFullLines.push(`Date: ${dateStr} | Tags: ${tags.join(', ')} | URL: ${postUrl}`, '') llmsFullLines.push(mdContent) }
jsonList.push({ path: 'api/posts/' + slug + '.json', data: JSON.stringify(jsonObj, null, 2) })
mdList.push({ path: 'api/posts/' + slug + '.md', data: mdContent }) })
const apiIndex = { site: siteTitle, description: siteDesc, url: siteUrl, post_count: resultList.length, posts: resultList }
const result = [ { path: 'api/index.json', data: JSON.stringify(apiIndex, null, 2) }, { path: 'llms.txt', data: llmsLines.join('\n') + '\n' }, { path: 'llms-full.txt', data: llmsFullLines.join('\n') + '\n' } ]
jsonList.forEach(function (f) { result.push(f) }) mdList.forEach(function (f) { result.push(f) })
return result })
|