@nuasite/llm-enhancements 0.60.0 → 0.61.0

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.
@@ -0,0 +1,59 @@
1
+ import { type CollectionPages, getCollectionContent } from './cms-marker'
2
+ import type { HtmlToMarkdownResult } from './html-to-markdown'
3
+ import { createCollectionOutput, createStaticOutput } from './markdown-generator'
4
+ import type { MarkdownSyntax } from './mdx'
5
+ import type { MarkdownOutput, ResolvedOptions } from './types'
6
+
7
+ /** What a page is resolved against: the pages that render an entry, and how the site parses markdown. */
8
+ export interface SiteContent {
9
+ pages: CollectionPages
10
+ syntax: MarkdownSyntax
11
+ }
12
+
13
+ export interface PageMarkdown {
14
+ output: MarkdownOutput
15
+ type: 'collection' | 'static'
16
+ /** The entry's source, for a collection page. */
17
+ sourcePath?: string
18
+ /** The entry's title, else the page's `<title>`. */
19
+ title?: string
20
+ /** The rendered page's metadata, when the page was read. */
21
+ metadata?: HtmlToMarkdownResult['metadata']
22
+ }
23
+
24
+ /**
25
+ * Resolve a page to the markdown that tells it: its collection entry when it has one, else its rendered
26
+ * HTML when static pages are wanted. `readPage` converts the rendered page, or gives `undefined` when
27
+ * there is none; it is called at most once and only when the page is needed.
28
+ */
29
+ export async function resolvePageMarkdown(
30
+ pagePath: string,
31
+ options: ResolvedOptions,
32
+ site: SiteContent,
33
+ readPage: () => Promise<HtmlToMarkdownResult | undefined>,
34
+ ): Promise<PageMarkdown | undefined> {
35
+ let page: Promise<HtmlToMarkdownResult | undefined> | undefined
36
+ const renderedPage = () => (page ??= readPage())
37
+
38
+ const entry = await getCollectionContent(pagePath, site.pages, site.syntax)
39
+ if (entry) {
40
+ const entryTitle = entry.frontmatter.title?.value
41
+ // An entry that renders more than its source says is told by its page, which is still the entry's,
42
+ // so it is written whether or not static pages are.
43
+ const rendered = entry.body === undefined || entryTitle === undefined ? await renderedPage() : undefined
44
+ const body = entry.body ?? rendered?.body
45
+ if (body === undefined) return undefined
46
+ return {
47
+ output: createCollectionOutput(entry.frontmatter, body, entry.file),
48
+ type: 'collection',
49
+ sourcePath: entry.file,
50
+ title: entryTitle ?? rendered?.metadata.title,
51
+ metadata: rendered?.metadata,
52
+ }
53
+ }
54
+
55
+ if (!options.includeStaticPages) return undefined
56
+ const rendered = await renderedPage()
57
+ if (!rendered) return undefined
58
+ return { output: createStaticOutput(rendered.metadata, rendered.body), type: 'static', title: rendered.metadata.title, metadata: rendered.metadata }
59
+ }
package/src/tsconfig.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "outDir": "../dist/types"
5
5
  },
6
6
  "references": [
7
- { "path": "../../cms/src" }
7
+ { "path": "../../cms/src" },
8
+ { "path": "../../cms-core/src" }
8
9
  ]
9
10
  }