2024-02-01 14:16:17 +00:00
|
|
|
import { APIGroup, InputSection } from '@/types/content-types'
|
2024-06-13 13:09:27 +00:00
|
|
|
import { TldrawApiModel } from '@/utils/TldrawApiModel'
|
2024-01-15 12:33:15 +00:00
|
|
|
import { nicelog } from '@/utils/nicelog'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import { CONTENT_DIR, getSlug } from '../utils'
|
|
|
|
import { getApiMarkdown } from './getApiMarkdown'
|
|
|
|
|
|
|
|
export async function createApiMarkdown() {
|
|
|
|
const apiInputSection: InputSection = {
|
2024-01-30 14:19:25 +00:00
|
|
|
id: 'reference' as string,
|
2024-01-15 12:33:15 +00:00
|
|
|
title: 'API Reference',
|
|
|
|
description: "Reference for the tldraw package's APIs (generated).",
|
|
|
|
categories: [],
|
2024-01-30 14:19:25 +00:00
|
|
|
sidebar_behavior: 'reference',
|
2024-02-29 16:28:45 +00:00
|
|
|
hero: null,
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const addedCategories = new Set<string>()
|
|
|
|
|
|
|
|
const INPUT_DIR = path.join(process.cwd(), 'api')
|
2024-01-30 14:19:25 +00:00
|
|
|
const OUTPUT_DIR = path.join(CONTENT_DIR, 'reference')
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(OUTPUT_DIR)) {
|
|
|
|
fs.rmSync(OUTPUT_DIR, { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.mkdirSync(OUTPUT_DIR)
|
|
|
|
|
2024-06-13 13:09:27 +00:00
|
|
|
const model = new TldrawApiModel()
|
2024-01-15 12:33:15 +00:00
|
|
|
const packageModels = []
|
|
|
|
|
|
|
|
// get all files in the INPUT_DIR
|
|
|
|
const files = fs.readdirSync(INPUT_DIR)
|
|
|
|
for (const file of files) {
|
|
|
|
// get the file path
|
|
|
|
const filePath = path.join(INPUT_DIR, file)
|
|
|
|
|
|
|
|
// parse the file
|
|
|
|
const apiModel = model.loadPackage(filePath)
|
|
|
|
|
|
|
|
// add the parsed file to the packageModels array
|
|
|
|
packageModels.push(apiModel)
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:09:27 +00:00
|
|
|
await model.preprocessReactComponents()
|
|
|
|
|
2024-03-12 13:36:24 +00:00
|
|
|
for (const packageModel of packageModels) {
|
|
|
|
const categoryName = packageModel.name.replace(`@tldraw/`, '')
|
|
|
|
|
|
|
|
if (!addedCategories.has(categoryName)) {
|
|
|
|
apiInputSection.categories!.push({
|
|
|
|
id: categoryName,
|
|
|
|
title: packageModel.name,
|
|
|
|
description: '',
|
|
|
|
groups: Object.values(APIGroup).map((title) => ({
|
|
|
|
id: title,
|
|
|
|
path: null,
|
|
|
|
})),
|
|
|
|
hero: null,
|
|
|
|
})
|
|
|
|
addedCategories.add(categoryName)
|
|
|
|
}
|
|
|
|
|
|
|
|
const entrypoint = packageModel.entryPoints[0]
|
|
|
|
|
|
|
|
for (let j = 0; j < entrypoint.members.length; j++) {
|
|
|
|
const item = entrypoint.members[j]
|
|
|
|
|
|
|
|
const outputFileName = `${getSlug(item)}.mdx`
|
2024-06-13 13:09:27 +00:00
|
|
|
|
|
|
|
const result = await getApiMarkdown(model, categoryName, item, j)
|
2024-03-12 13:36:24 +00:00
|
|
|
nicelog(`✎ ${outputFileName}`)
|
|
|
|
fs.writeFileSync(path.join(OUTPUT_DIR, outputFileName), result.markdown)
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
// Add the API section to the sections.json file
|
|
|
|
|
|
|
|
const sectionsJsonPath = path.join(CONTENT_DIR, 'sections.json')
|
|
|
|
const sectionsJson = JSON.parse(fs.readFileSync(sectionsJsonPath, 'utf8')) as InputSection[]
|
|
|
|
sectionsJson.splice(
|
2024-01-30 14:19:25 +00:00
|
|
|
sectionsJson.findIndex((s) => s.id === 'reference'),
|
2024-01-15 12:33:15 +00:00
|
|
|
1
|
|
|
|
)
|
|
|
|
sectionsJson.push(apiInputSection)
|
2024-03-02 16:42:07 +00:00
|
|
|
fs.writeFileSync(sectionsJsonPath, JSON.stringify(sectionsJson, null, '\t') + '\n')
|
2024-06-17 14:47:22 +00:00
|
|
|
|
|
|
|
model.throwEncounteredErrors()
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|