Skip to content

Commit ecd9aeb

Browse files
committed
build: generate AI docs from latest version
INSTUI-5092
1 parent a1adc22 commit ecd9aeb

4 files changed

Lines changed: 71 additions & 22 deletions

File tree

docs/guides/component-versioning.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Component versioning
33
category: Guides
44
order: 2
5+
relevantForAI: true
56
---
67

78
## Why components are versioned

packages/__docs__/buildScripts/ai-accessible-documentation/generate-ai-accessible-llms-file.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function generateAIAccessibleLlmsFile(
7272

7373
let LlmsMarkdownContent = `# Instructure UI (InstUI) - React Component Library\n\n- version ${version} \n\n`
7474
LlmsMarkdownContent += `- Instructure UI (InstUI) is a comprehensive React component library.\n\n`
75+
LlmsMarkdownContent += `- All component documentation below always reflects the latest InstUI version noted above (${version}). Props, types, and examples describe this version.\n\n`
7576

7677
// Add main Documentation section
7778
LlmsMarkdownContent += `## Documentation\n\n`

packages/__docs__/buildScripts/ai-accessible-documentation/generate-ai-accessible-markdowns.mts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,36 +173,62 @@ function generatePropsTable(
173173
return propsContent
174174
}
175175

176-
function generateComponentUsage(doc: DocumentationData): string {
176+
177+
interface VersionImportInfo {
178+
version: string
179+
versionedPackages: Set<string>
180+
}
181+
182+
// Versioned packages import from the pinned version path (e.g.
183+
// `@instructure/ui-badge/v11_7`); non-versioned ones import from the plain
184+
// package name.
185+
function buildImportPath(
186+
packageName: string,
187+
versionImport?: VersionImportInfo
188+
): string {
189+
if (!versionImport) return packageName
190+
const shortName = packageName.replace(/^@instructure\//, '')
191+
return versionImport.versionedPackages.has(shortName)
192+
? `${packageName}/${versionImport.version}`
193+
: packageName
194+
}
195+
196+
function generateComponentUsage(
197+
doc: DocumentationData,
198+
versionImport?: VersionImportInfo
199+
): string {
177200
if (!isComponent(doc)) return ''
178201

179202
const { id, displayName, packageName } = doc
180203
const importName = displayName || id
181204

182205
if (!packageName) return ''
183206

207+
const importPath = buildImportPath(packageName, versionImport)
208+
184209
let usageContent = `### Usage\n\n`
185210

186211
usageContent += `Install the package:\n\n\`\`\`shell\nnpm install ${packageName}\n\`\`\`\n\n`
187212

188213
usageContent += `Import the component:\n\n\`\`\`javascript
189214
/*** ES Modules (with tree shaking) ***/
190-
import { ${importName} } from '${packageName}'
215+
import { ${importName} } from '${importPath}'
191216
\`\`\`\n\n`
192217

193218
return usageContent
194219
}
195220

196221
function generateComponentMarkdown(
197222
jsonData: ComponentData,
198-
childComponents: ComponentData[] = []
223+
childComponents: ComponentData[] = [],
224+
versionImport?: VersionImportInfo
199225
): string {
200226
const { displayName, description } = jsonData
201227

202228
let markdownContent = `# ${displayName}\n\n`
203229
markdownContent += `${description}\n\n`
204230
markdownContent += generatePropsTable(jsonData, childComponents)
205-
markdownContent += generateComponentUsage(jsonData)
231+
markdownContent += generateComponentUsage(jsonData, versionImport)
206232

207233
return markdownContent
208234
}
@@ -217,7 +243,9 @@ function generateGuideMarkdown(jsonData: GuideData): string {
217243

218244
async function generateAIAccessibleMarkdowns(
219245
docsFolder: string,
220-
outputDir: string
246+
outputDir: string,
247+
sourcesDataFilePath = './__build__/markdown-and-sources-data.json',
248+
versionImport?: VersionImportInfo
221249
): Promise<void> {
222250
if (!existsSync(outputDir)) {
223251
mkdirSync(outputDir, { recursive: true })
@@ -262,7 +290,8 @@ async function generateAIAccessibleMarkdowns(
262290
if (!component.parent) {
263291
const markdownContent = generateComponentMarkdown(
264292
component,
265-
childComponents
293+
childComponents,
294+
versionImport
266295
)
267296
const fileName = `${
268297
component.id ||
@@ -273,9 +302,9 @@ async function generateAIAccessibleMarkdowns(
273302
}
274303
}
275304

276-
const buildDir = './__build__/'
277-
// create an llms-like index file for docs in the zip
278-
generateAIAccessibleLlmsFile(buildDir + 'markdown-and-sources-data.json', {
305+
// create an llms-like index file for docs in the zip, from the same
306+
// (latest) version's sources data used to generate the markdown files
307+
generateAIAccessibleLlmsFile(sourcesDataFilePath, {
279308
outputFilePath: path.join(outputDir, 'index.md'),
280309
baseUrl: './',
281310
summariesFilePath:
@@ -292,3 +321,4 @@ async function generateAIAccessibleMarkdowns(
292321
}
293322

294323
export { generateAIAccessibleMarkdowns }
324+
export type { VersionImportInfo }

packages/__docs__/buildScripts/build-docs.mts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,39 @@ async function buildDocs() {
226226
JSON.stringify(docsVersionsManifest)
227227
)
228228

229-
// Generate AI accessible documentation from default version
230-
const defaultVersionDocsDir = buildDir + 'docs/' + defaultVersion + '/'
231-
generateAIAccessibleMarkdowns(defaultVersionDocsDir, buildDir + 'markdowns/')
229+
// Generate AI accessible documentation from the latest library version.
230+
const latestVersion =
231+
versionMap.libraryVersions[versionMap.libraryVersions.length - 1] ||
232+
defaultVersion
233+
const latestVersionDocsDir = buildDir + 'docs/' + latestVersion + '/'
234+
const latestVersionSourcesData =
235+
latestVersionDocsDir + 'markdown-and-sources-data.json'
236+
237+
// The latest version plus its versioned packages (the version map's
238+
// mapping keys), used to emit versioned import examples in the markdown.
239+
const versionImport = {
240+
version: latestVersion,
241+
versionedPackages: new Set(
242+
Object.keys(versionMap.mapping[latestVersion] || {})
243+
)
244+
}
232245

233-
generateAIAccessibleLlmsFile(
234-
buildDir + 'markdown-and-sources-data.json',
235-
{
236-
outputFilePath: path.join(buildDir, 'llms.txt'),
237-
baseUrl: 'https://instructure.design/markdowns/',
238-
summariesFilePath: path.join(
239-
__dirname,
240-
'../buildScripts/ai-accessible-documentation/summaries-for-llms-file.json'
241-
)
242-
}
246+
await generateAIAccessibleMarkdowns(
247+
latestVersionDocsDir,
248+
buildDir + 'markdowns/',
249+
latestVersionSourcesData,
250+
versionImport
243251
)
244252

253+
generateAIAccessibleLlmsFile(latestVersionSourcesData, {
254+
outputFilePath: path.join(buildDir, 'llms.txt'),
255+
baseUrl: 'https://instructure.design/markdowns/',
256+
summariesFilePath: path.join(
257+
__dirname,
258+
'../buildScripts/ai-accessible-documentation/summaries-for-llms-file.json'
259+
)
260+
})
261+
245262
fs.copyFileSync(
246263
projectRoot + '/packages/ui-icons/src/generated/legacy/legacy-icons-data.json',
247264
buildDir + 'legacy-icons-data.json'

0 commit comments

Comments
 (0)