@nuasite/llm-enhancements 0.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/paths.ts ADDED
@@ -0,0 +1,90 @@
1
+ import path from 'node:path'
2
+
3
+ /** Well-known path for LLM discovery endpoint */
4
+ export const LLM_ENDPOINT_PATH = '/.well-known/llm.md'
5
+
6
+ /** Path for llms.txt endpoint */
7
+ export const LLMS_TXT_PATH = '/llms.txt'
8
+
9
+ /**
10
+ * Normalize a URL path by removing query strings, hashes, and trailing slashes
11
+ */
12
+ export function normalizePath(url: string): string {
13
+ let pagePath = url.split('?')[0]?.split('#')[0] ?? ''
14
+ if (pagePath.length > 1 && pagePath.endsWith('/')) {
15
+ pagePath = pagePath.slice(0, -1)
16
+ }
17
+ return pagePath || '/'
18
+ }
19
+
20
+ /**
21
+ * Get the markdown URL for a given page path
22
+ */
23
+ export function getMarkdownUrl(pagePath: string): string {
24
+ if (pagePath === '/') {
25
+ return '/index.md'
26
+ }
27
+ const cleanPath = pagePath.endsWith('/') ? pagePath.slice(0, -1) : pagePath
28
+ return `${cleanPath}.md`
29
+ }
30
+
31
+ /**
32
+ * Convert a .md URL back to a page path
33
+ */
34
+ export function mdUrlToPagePath(url: string): string {
35
+ const match = url.match(/^\/(.*)\.md$/)
36
+ if (!match) return url
37
+
38
+ const pagePath = '/' + match[1]
39
+ return pagePath === '/index' ? '/' : pagePath
40
+ }
41
+
42
+ /**
43
+ * Get the output path for a .md file in dist
44
+ */
45
+ export function getMdOutputPath(distDir: string, pagePath: string): string {
46
+ if (pagePath === '/') {
47
+ return path.join(distDir, 'index.md')
48
+ }
49
+ return path.join(distDir, `${pagePath.slice(1)}.md`)
50
+ }
51
+
52
+ /**
53
+ * Get the HTML file path for a page in dist
54
+ */
55
+ export function getHtmlPath(distDir: string, pagePath: string): string {
56
+ if (pagePath === '/') {
57
+ return path.join(distDir, 'index.html')
58
+ }
59
+ return path.join(distDir, pagePath.slice(1), 'index.html')
60
+ }
61
+
62
+ /**
63
+ * Get the output path for llm.md in dist
64
+ */
65
+ export function getLlmOutputPath(distDir: string): string {
66
+ return path.join(distDir, '.well-known', 'llm.md')
67
+ }
68
+
69
+ /**
70
+ * Get the output path for llms.txt in dist
71
+ */
72
+ export function getLlmsTxtOutputPath(distDir: string): string {
73
+ return path.join(distDir, 'llms.txt')
74
+ }
75
+
76
+ /**
77
+ * Inject markdown alternate link into HTML head
78
+ */
79
+ export function injectMarkdownLink(html: string, pagePath: string): string {
80
+ const mdUrl = getMarkdownUrl(pagePath)
81
+ const linkTag = `<link rel="alternate" type="text/markdown" href="${mdUrl}">`
82
+
83
+ if (html.includes('</head>')) {
84
+ return html.replace('</head>', `${linkTag}\n</head>`)
85
+ }
86
+ if (html.includes('<head>')) {
87
+ return html.replace('<head>', `<head>\n${linkTag}`)
88
+ }
89
+ return html
90
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../tsconfig.settings.json",
3
+ "compilerOptions": {
4
+ "outDir": "../dist/types"
5
+ }
6
+ }
package/src/types.ts ADDED
@@ -0,0 +1,67 @@
1
+ export interface PageMarkdownOptions {
2
+ /** Directory containing content collections (default: 'src/content') */
3
+ contentDir?: string
4
+ /** Whether to include static (non-collection) pages (default: true) */
5
+ includeStaticPages?: boolean
6
+ /** Whether to include frontmatter in output (default: true) */
7
+ includeFrontmatter?: boolean
8
+ /** Enable /.well-known/llm.md endpoint (default: true) */
9
+ llmEndpoint?: boolean | LlmEndpointOptions
10
+ /** Enable /llms.txt endpoint (default: true) */
11
+ llmsTxt?: boolean | LlmsTxtOptions
12
+ }
13
+
14
+ export interface LlmEndpointOptions {
15
+ /** Site name override */
16
+ siteName?: string
17
+ /** Site description override */
18
+ description?: string
19
+ /** Base URL override (defaults to Astro's site config) */
20
+ baseUrl?: string
21
+ /** Additional content to append */
22
+ additionalContent?: string
23
+ }
24
+
25
+ export interface LlmsTxtOptions {
26
+ /** Site name override */
27
+ siteName?: string
28
+ /** Site description override */
29
+ description?: string
30
+ /** Base URL override (defaults to Astro's site config) */
31
+ baseUrl?: string
32
+ /** Whether crawling is allowed (default: true) */
33
+ allowCrawling?: boolean
34
+ /** Custom instructions for LLMs */
35
+ instructions?: string
36
+ /** Additional content to append */
37
+ additionalContent?: string
38
+ }
39
+
40
+ export interface MarkdownOutput {
41
+ /** YAML frontmatter fields */
42
+ frontmatter: Record<string, unknown>
43
+ /** Markdown body content */
44
+ body: string
45
+ /** Path to the original source file (if from collection) */
46
+ sourcePath?: string
47
+ }
48
+
49
+ export interface ResolvedOptions {
50
+ contentDir: string
51
+ includeStaticPages: boolean
52
+ includeFrontmatter: boolean
53
+ llmEndpoint: false | LlmEndpointOptions
54
+ llmsTxt: false | LlmsTxtOptions
55
+ }
56
+
57
+ export function resolveOptions(options: PageMarkdownOptions = {}): ResolvedOptions {
58
+ const llmEndpoint = options.llmEndpoint ?? true
59
+ const llmsTxt = options.llmsTxt ?? true
60
+ return {
61
+ contentDir: options.contentDir ?? 'src/content',
62
+ includeStaticPages: options.includeStaticPages ?? true,
63
+ includeFrontmatter: options.includeFrontmatter ?? true,
64
+ llmEndpoint: llmEndpoint === false ? false : llmEndpoint === true ? {} : llmEndpoint,
65
+ llmsTxt: llmsTxt === false ? false : llmsTxt === true ? {} : llmsTxt,
66
+ }
67
+ }