@redocly/realm-plugin-asciidoc 0.8.0-next.16 → 0.8.0-next.18
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/CHANGELOG.md +19 -0
- package/lib/.tsbuildinfo +1 -1
- package/package.json +6 -2
- package/src/asciidoc-parser.ts +0 -1025
- package/src/get-ai-search-documents.ts +0 -194
- package/src/plugin.ts +0 -675
- package/src/search-facets.ts +0 -46
- package/src/search-resolver.ts +0 -98
- package/src/template.tsx +0 -687
- package/src/types.ts +0 -178
- package/tests/asciidoc-parser.test.ts +0 -87
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -17
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import type { SearchFacet } from '@redocly/theme';
|
|
2
|
-
import type { PageRouteDetails } from '@redocly/realm/dist/server/types';
|
|
3
|
-
import type { AsciidocSection, AsciidocFrontmatter } from './types.js';
|
|
4
|
-
|
|
5
|
-
import { extractDocumentSearchFacets } from './search-facets.js';
|
|
6
|
-
|
|
7
|
-
type AiDocumentsStore = {
|
|
8
|
-
getLLMsTxts: () => Promise<
|
|
9
|
-
{
|
|
10
|
-
content: string;
|
|
11
|
-
title: string;
|
|
12
|
-
description?: string;
|
|
13
|
-
slug: string;
|
|
14
|
-
fsPath: string;
|
|
15
|
-
includeInLLMsTxt: boolean;
|
|
16
|
-
}[]
|
|
17
|
-
>;
|
|
18
|
-
getSearchDocuments: () => Promise<
|
|
19
|
-
{
|
|
20
|
-
url: string;
|
|
21
|
-
fsPath: string;
|
|
22
|
-
content: string;
|
|
23
|
-
title: string;
|
|
24
|
-
locale: string;
|
|
25
|
-
product?: string;
|
|
26
|
-
facets?: Record<string, string>;
|
|
27
|
-
}[]
|
|
28
|
-
>;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const AI_SEARCH_CHUNK_SIZE = 50;
|
|
32
|
-
const AI_SEARCH_DOCUMENT_CHUNK_SIZE = 250;
|
|
33
|
-
|
|
34
|
-
export function getAiDocumentsStore(
|
|
35
|
-
frontmatter: AsciidocFrontmatter,
|
|
36
|
-
sections: AsciidocSection[],
|
|
37
|
-
getSearchFacets: () => Map<string, SearchFacet>,
|
|
38
|
-
): (
|
|
39
|
-
route: PageRouteDetails,
|
|
40
|
-
staticData: unknown,
|
|
41
|
-
context: unknown,
|
|
42
|
-
actions: unknown,
|
|
43
|
-
) => Promise<AiDocumentsStore | undefined> {
|
|
44
|
-
return async (
|
|
45
|
-
route: PageRouteDetails,
|
|
46
|
-
_staticData: unknown,
|
|
47
|
-
_context: unknown,
|
|
48
|
-
_actions: unknown,
|
|
49
|
-
): Promise<AiDocumentsStore | undefined> => {
|
|
50
|
-
if (frontmatter.excludeFromSearch) return;
|
|
51
|
-
|
|
52
|
-
const pageTitle = frontmatter.seo?.title || frontmatter.title || route.slug;
|
|
53
|
-
const description = frontmatter.seo?.description || frontmatter.description;
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
async getLLMsTxts() {
|
|
57
|
-
const body = sectionsToMarkdown(sections);
|
|
58
|
-
const content = `# ${pageTitle}\n\n${body}`;
|
|
59
|
-
|
|
60
|
-
return [
|
|
61
|
-
{
|
|
62
|
-
title: pageTitle,
|
|
63
|
-
description,
|
|
64
|
-
slug: route.slug,
|
|
65
|
-
fsPath: route.fsPath,
|
|
66
|
-
content,
|
|
67
|
-
includeInLLMsTxt: true,
|
|
68
|
-
},
|
|
69
|
-
];
|
|
70
|
-
},
|
|
71
|
-
async getSearchDocuments() {
|
|
72
|
-
const metadata = route.metadata || {};
|
|
73
|
-
const facets = extractDocumentSearchFacets(metadata, getSearchFacets);
|
|
74
|
-
const chunks = createContentChunks(sections, pageTitle, route.slug);
|
|
75
|
-
const mergedChunks = mergeChunks(chunks);
|
|
76
|
-
|
|
77
|
-
return mergedChunks
|
|
78
|
-
.filter((chunk) => chunk.content)
|
|
79
|
-
.map((chunk) => ({
|
|
80
|
-
title: chunk.title,
|
|
81
|
-
url: chunk.url,
|
|
82
|
-
content: chunk.toc
|
|
83
|
-
? `${chunk.toc}\n${chunk.content}`
|
|
84
|
-
: `# ${pageTitle}\n${chunk.content}`,
|
|
85
|
-
fsPath: route.fsPath,
|
|
86
|
-
locale: detectLocaleFromFsPath(route.fsPath),
|
|
87
|
-
product: route.product?.name,
|
|
88
|
-
facets,
|
|
89
|
-
}));
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type ContentChunk = {
|
|
96
|
-
title: string;
|
|
97
|
-
content: string;
|
|
98
|
-
url: string;
|
|
99
|
-
level: number;
|
|
100
|
-
toc: string;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
function sectionsToMarkdown(sections: AsciidocSection[], headingLevel = 1): string {
|
|
104
|
-
const parts: string[] = [];
|
|
105
|
-
|
|
106
|
-
for (const section of sections) {
|
|
107
|
-
const prefix = '#'.repeat(headingLevel + 1);
|
|
108
|
-
parts.push(`${prefix} ${section.title}`);
|
|
109
|
-
|
|
110
|
-
if (section.textContent) {
|
|
111
|
-
parts.push(section.textContent);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (section.children.length > 0) {
|
|
115
|
-
parts.push(sectionsToMarkdown(section.children, headingLevel + 1));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return parts.join('\n\n');
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function createContentChunks(
|
|
123
|
-
sections: AsciidocSection[],
|
|
124
|
-
pageTitle: string,
|
|
125
|
-
pageUrl: string,
|
|
126
|
-
): ContentChunk[] {
|
|
127
|
-
const chunks: ContentChunk[] = [];
|
|
128
|
-
const headingStack: { title: string; level: number }[] = [];
|
|
129
|
-
|
|
130
|
-
function processSection(section: AsciidocSection) {
|
|
131
|
-
// Update heading stack
|
|
132
|
-
headingStack.splice(section.level - 1);
|
|
133
|
-
headingStack.push({ title: section.title, level: section.level });
|
|
134
|
-
|
|
135
|
-
const sectionUrl = `${pageUrl}#${section.id}`;
|
|
136
|
-
const toc = headingStack.map((h) => `${'#'.repeat(h.level + 1)} ${h.title}`).join('\n');
|
|
137
|
-
const chunkTitle =
|
|
138
|
-
section.level > 1 ? `${pageTitle} → ${section.title}` : section.title || pageTitle;
|
|
139
|
-
|
|
140
|
-
chunks.push({
|
|
141
|
-
title: chunkTitle,
|
|
142
|
-
content: section.textContent,
|
|
143
|
-
url: sectionUrl,
|
|
144
|
-
level: section.level,
|
|
145
|
-
toc,
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
for (const child of section.children) {
|
|
149
|
-
processSection(child);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
for (const section of sections) {
|
|
154
|
-
processSection(section);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return chunks;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function estimateChunkSize(content: string): number {
|
|
161
|
-
return content.length / 4;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function mergeChunks(chunks: ContentChunk[]): ContentChunk[] {
|
|
165
|
-
if (chunks.length === 0) return [];
|
|
166
|
-
const mergedChunks: ContentChunk[] = [];
|
|
167
|
-
let currentChunk = chunks[0];
|
|
168
|
-
|
|
169
|
-
for (let i = 1; i < chunks.length; i++) {
|
|
170
|
-
const chunk = chunks[i];
|
|
171
|
-
const currentChunkSize = estimateChunkSize(currentChunk.content);
|
|
172
|
-
const chunkSize = estimateChunkSize(chunk.content);
|
|
173
|
-
const isLastChunk = i === chunks.length - 1;
|
|
174
|
-
|
|
175
|
-
if (
|
|
176
|
-
chunkSize < AI_SEARCH_CHUNK_SIZE &&
|
|
177
|
-
(currentChunkSize < AI_SEARCH_DOCUMENT_CHUNK_SIZE || isLastChunk) &&
|
|
178
|
-
chunk.level > currentChunk.level
|
|
179
|
-
) {
|
|
180
|
-
currentChunk.content += '\n' + chunk.content;
|
|
181
|
-
} else {
|
|
182
|
-
mergedChunks.push(currentChunk);
|
|
183
|
-
currentChunk = chunk;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
mergedChunks.push(currentChunk);
|
|
188
|
-
return mergedChunks;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function detectLocaleFromFsPath(relativePath: string): string {
|
|
192
|
-
const localeCandidate = relativePath.split('/')[0];
|
|
193
|
-
return /^[a-z]{2}-[A-Z]{2}$/.test(localeCandidate) ? localeCandidate : '';
|
|
194
|
-
}
|