11import type { StarlightPlugin } from '@astrojs/starlight/types'
22import { AstroError } from 'astro/errors'
3- import { normalizePath } from 'vite'
4- import { viteStaticCopy } from 'vite-plugin-static-copy'
53
64interface Actions {
75 chatgpt ?: boolean
@@ -23,6 +21,7 @@ export interface PageActionsConfig {
2321 actions ?: Actions
2422 title ?: string
2523 description ?: string
24+ maxDepth ?: number
2625}
2726
2827/**
@@ -37,7 +36,7 @@ export interface PageActionsConfig {
3736 * @param {string } [userConfig.prompt] - The prompt template for AI chat services. Use `{url}` as placeholder for the markdown URL.
3837 * @param {string } [userConfig.baseUrl] - The base URL of your site, required for generating the `llms.txt` file.
3938 * @param {Actions } [userConfig.actions] - Configure which built-in actions to display and define custom actions.
40- *
39+ * @param { number } [userConfig.maxDepth] - The maximum depth of the documentation tree to include in the `llms.txt` file. Defaults to 4.
4140 *
4241 * @example
4342 * ```javascript
@@ -52,6 +51,7 @@ export interface PageActionsConfig {
5251 * llmsPlugin({
5352 * prompt: "Read {url} and explain its main points briefly.",
5453 * baseUrl: "https://mydocs.example.com",
54+ * maxDepth: 3,
5555 * actions: {
5656 * chatgpt: false,
5757 * v0: true,
@@ -125,128 +125,45 @@ export function llmsPlugin(userConfig?: PageActionsConfig): StarlightPlugin {
125125 }
126126
127127 addIntegration ( {
128- name : 'llms-plugin-integration ' ,
128+ name : 'llms' ,
129129 hooks : {
130- 'astro:config:setup' : ( { updateConfig } ) => {
130+ 'astro:config:setup' ( { updateConfig, injectRoute } ) {
131131 updateConfig ( {
132132 vite : {
133133 plugins : [
134134 {
135135 name : 'llms-plugin-config' ,
136- resolveId ( id ) {
136+ resolveId ( id : string ) {
137137 if ( id === 'virtual:llms-plugin/config' ) {
138138 return `\0${ id } `
139139 }
140+ return undefined
140141 } ,
141- load ( id ) {
142+ load ( id : string ) {
142143 if ( id === '\0virtual:llms-plugin/config' ) {
143144 return `export default ${ JSON . stringify ( config ) } `
144145 }
146+ return undefined
145147 } ,
146148 } ,
147- viteStaticCopy ( {
148- targets : [
149- {
150- src : 'src/content/docs/**/*.{md,mdx}' ,
151- dest : '' ,
152- transform : ( content : string ) => {
153- const frontmatterRegex =
154- / ^ - - - \s * \n ( [ \s \S ] * ?) \n - - - \s * \n ( [ \s \S ] * ) $ /
155- const match = content . match ( frontmatterRegex )
156-
157- let title = ''
158- let markdownContent = content
159-
160- if (
161- match &&
162- match [ 1 ] !== undefined &&
163- match [ 2 ] !== undefined
164- ) {
165- const frontmatter = match [ 1 ]
166- markdownContent = match [ 2 ]
167-
168- const titleMatch = frontmatter . match (
169- / t i t l e : \s * [ " ' ] ? ( [ ^ " ' \n ] + ) [ " ' ] ? /
170- )
171- if ( titleMatch && titleMatch [ 1 ] !== undefined ) {
172- title = titleMatch [ 1 ] . trim ( )
173- }
174- }
175-
176- const contentWithoutImports = markdownContent
177- // .split('\n')
178- // .filter(
179- // (line) => !line.trim().startsWith('import ')
180- // )
181- // .join('\n')
182-
183- let newContent = ''
184-
185- if ( title ) {
186- newContent = `# ${ title } \n\n`
187- }
188-
189- newContent += contentWithoutImports . trim ( )
190-
191- return newContent
192- } ,
193- rename : (
194- fileName : string ,
195- fileExtension : string ,
196- fullPath : string
197- ) => {
198- const fullPathNormalized = normalizePath ( fullPath )
199- const relativePath = (
200- fullPathNormalized . split (
201- 'src/content/docs/'
202- ) [ 1 ] as string
203- ) . replace ( new RegExp ( `\\.${ fileExtension } $` ) , '' )
204- const pathSegments = relativePath . split ( '/' )
205-
206- if ( fileName === 'index' ) {
207- if ( pathSegments . length === 1 ) {
208- return 'index.md'
209- } else {
210- const directories = pathSegments
211- . slice ( 0 , - 2 )
212- . join ( '/' )
213- const folderName =
214- pathSegments [ pathSegments . length - 2 ]
215-
216- return directories
217- ? `${ directories } /${ folderName } .md`
218- : `${ folderName } .md`
219- }
220- }
221-
222- const directories = pathSegments
223- . slice ( 0 , - 1 )
224- . join ( '/' )
225- const finalPath = directories
226- ? `${ directories } /${ fileName } .md`
227- : `${ fileName } .md`
228-
229- return finalPath . replace ( '@' , '' ) . toLowerCase ( )
230- } ,
231- } ,
232- ] ,
233- } ) ,
234149 ] ,
235150 } ,
236151 } )
237- } ,
238- } ,
239- } )
240152
241- addIntegration ( {
242- name : 'llms' ,
243- hooks : {
244- 'astro:config:setup' ( { injectRoute } ) {
245- const entrypoint = new URL ( 'llms.txt.ts' , import . meta. url )
246153 injectRoute ( {
247- entrypoint,
154+ entrypoint : '@hugomrdias/docs/llms.txt' ,
248155 pattern : '/llms.txt' ,
249156 } )
157+
158+ injectRoute ( {
159+ entrypoint : '@hugomrdias/docs/llms-full.txt' ,
160+ pattern : '/llms-full.txt' ,
161+ } )
162+
163+ injectRoute ( {
164+ entrypoint : '@hugomrdias/docs/markdown' ,
165+ pattern : '/[...slug].md' ,
166+ } )
250167 } ,
251168 } ,
252169 } )
0 commit comments