2024-02-01 14:16:17 +00:00
|
|
|
import { APIGroup, InputSection } from '@/types/content-types'
|
2024-01-15 12:33:15 +00:00
|
|
|
import { nicelog } from '@/utils/nicelog'
|
|
|
|
import { ApiModel } from '@microsoft/api-extractor-model'
|
|
|
|
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)
|
|
|
|
|
|
|
|
const model = new ApiModel()
|
|
|
|
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-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 result = await getApiMarkdown(categoryName, item, j)
|
|
|
|
const outputFileName = `${getSlug(item)}.mdx`
|
|
|
|
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-01-15 12:33:15 +00:00
|
|
|
}
|