Skip to content

Commit 233c6f0

Browse files
committed
chore: add doc generation for AI
1 parent 3ce558e commit 233c6f0

File tree

8 files changed

+151
-34
lines changed

8 files changed

+151
-34
lines changed

docs/modules/Config.ts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface Config {
4949
readonly outDir: string
5050
readonly theme: string
5151
readonly enableSearch: boolean
52+
readonly enableAI: boolean
5253
readonly enforceDescriptions: boolean
5354
readonly enforceExamples: boolean
5455
readonly enforceVersion: boolean

docs/modules/Domain.ts.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Added in v1.0.0
1212

1313
<h2 class="text-delta">Table of contents</h2>
1414

15+
- [accessors](#accessors)
16+
- [printablesFromModule](#printablesfrommodule)
1517
- [constructors](#constructors)
1618
- [createClass](#createclass)
1719
- [createConstant](#createconstant)
@@ -35,11 +37,24 @@ Added in v1.0.0
3537
- [Interface (interface)](#interface-interface)
3638
- [Method (interface)](#method-interface)
3739
- [Module (interface)](#module-interface)
40+
- [Printable (type alias)](#printable-type-alias)
3841
- [Property (interface)](#property-interface)
3942
- [TypeAlias (interface)](#typealias-interface)
4043

4144
---
4245

46+
# accessors
47+
48+
## printablesFromModule
49+
50+
**Signature**
51+
52+
```ts
53+
export declare const printablesFromModule: (module: Module) => ReadonlyArray<Printable>
54+
```
55+
56+
Added in v1.0.0
57+
4358
# constructors
4459
4560
## createClass
@@ -303,6 +318,16 @@ export interface Module extends Documentable {
303318

304319
Added in v1.0.0
305320

321+
## Printable (type alias)
322+
323+
**Signature**
324+
325+
```ts
326+
export type Printable = Class | Constant | Export | Function | Interface | TypeAlias
327+
```
328+
329+
Added in v1.0.0
330+
306331
## Property (interface)
307332
308333
**Signature**

docs/modules/Markdown.ts.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Added in v1.0.0
1414

1515
- [printers](#printers)
1616
- [printModule](#printmodule)
17+
- [printPrintableForAI](#printprintableforai)
1718

1819
---
1920

@@ -28,3 +29,17 @@ export declare const printModule: (module: Domain.Module, order: number) => stri
2829
```
2930
3031
Added in v1.0.0
32+
33+
## printPrintableForAI
34+
35+
**Signature**
36+
37+
```ts
38+
export declare const printPrintableForAI: (
39+
projectName: string,
40+
module: Domain.Module,
41+
printable: Domain.Printable
42+
) => string
43+
```
44+
45+
Added in v1.0.0

src/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface Config {
2828
readonly outDir: string
2929
readonly theme: string
3030
readonly enableSearch: boolean
31+
readonly enableAI: boolean
3132
readonly enforceDescriptions: boolean
3233
readonly enforceExamples: boolean
3334
readonly enforceVersion: boolean
@@ -62,6 +63,7 @@ const ConfigSchema = Schema.struct({
6263
srcDir: Schema.string,
6364
outDir: Schema.string,
6465
theme: Schema.string,
66+
enableAI: Schema.boolean,
6567
enableSearch: Schema.boolean,
6668
enforceDescriptions: Schema.boolean,
6769
enforceExamples: Schema.boolean,
@@ -99,6 +101,7 @@ const getDefaultConfig = (projectName: string, projectHomepage: string): Config
99101
srcDir: "src",
100102
outDir: "docs",
101103
theme: "pmarsceill/just-the-docs",
104+
enableAI: false,
102105
enableSearch: true,
103106
enforceDescriptions: false,
104107
enforceExamples: false,

src/Core.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import chalk from "chalk"
1414
import * as NodePath from "path"
1515
import * as ChildProcess from "./ChildProcess"
1616
import * as Config from "./Config"
17-
import type * as Domain from "./Domain"
17+
import * as Domain from "./Domain"
1818
import * as FileSystem from "./FileSystem"
1919
import { SimpleLogger } from "./Logger"
20-
import { printModule } from "./Markdown"
20+
import { printModule, printPrintableForAI } from "./Markdown"
2121
import * as Parser from "./Parser"
2222
import * as Process from "./Process"
2323

@@ -35,10 +35,8 @@ const readFiles = pipe(
3535
Effect.tap((paths) => Effect.logInfo(chalk.bold(`${paths.length} module(s) found`))),
3636
Effect.flatMap(
3737
Effect.forEachPar((path) =>
38-
Effect.map(
39-
fileSystem.readFile(path),
40-
(content) => FileSystem.makeFile(path, content, false)
41-
)
38+
Effect.map(fileSystem.readFile(path), (content) =>
39+
FileSystem.makeFile(path, content, false))
4240
)
4341
)
4442
)
@@ -320,12 +318,9 @@ const getMarkdown = (modules: ReadonlyArray<Domain.Module>) =>
320318
Effect.bind("home", () => getHome),
321319
Effect.bind("index", () => getModulesIndex),
322320
Effect.bind("yml", () => getConfigYML),
323-
Effect.flatMap(({ home, index, yml }) =>
324-
pipe(
325-
getModuleMarkdownFiles(modules),
326-
Effect.map((files) => [home, index, yml].concat(files))
327-
)
328-
)
321+
Effect.bind("modules", () => getModuleMarkdownFiles(modules)),
322+
Effect.bind("ai", () => maybeGetAIMarkdownFiles(modules)),
323+
Effect.map(({ ai, home, index, modules, yml }) => [home, index, yml].concat(modules).concat(ai))
329324
)
330325

331326
const getHome = pipe(
@@ -441,6 +436,14 @@ const getMarkdownOutputPath = (module: Domain.Module) =>
441436
`${module.path.slice(1).join(NodePath.sep)}.md`
442437
))
443438

439+
const getAIMarkdownOutputPath = (module: Domain.Module, printable: Domain.Printable) =>
440+
Effect.map(Config.Config, (config) =>
441+
join(
442+
config.outDir,
443+
"ai",
444+
`${module.path.slice(1).join("-").replace(/\.ts$/, "")}-${printable.name}.md`
445+
))
446+
444447
const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
445448
Effect.forEachWithIndex(modules, (module, order) =>
446449
pipe(
@@ -450,6 +453,37 @@ const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
450453
Effect.map(({ content, outputPath }) => FileSystem.makeFile(outputPath, content, true))
451454
))
452455

456+
const getAIMarkdownFiles = (projectName: string, modules: ReadonlyArray<Domain.Module>) =>
457+
pipe(
458+
modules,
459+
ReadonlyArray.flatMap((module) =>
460+
pipe(
461+
Domain.printablesFromModule(module),
462+
ReadonlyArray.map((printable) => [module, printable] as const)
463+
)
464+
),
465+
ReadonlyArray.filter(([, printable]) =>
466+
printable.description._tag === "Some" &&
467+
(printable.examples.length > 0 || printable.description.value.includes("```") ||
468+
printable.description.value.length >= 115)
469+
),
470+
Effect.forEach(([module, printable]) =>
471+
pipe(
472+
Effect.Do(),
473+
Effect.bind("outputPath", () => getAIMarkdownOutputPath(module, printable)),
474+
Effect.let("content", () => printPrintableForAI(projectName, module, printable)),
475+
Effect.map(({ content, outputPath }) => FileSystem.makeFile(outputPath, content, true))
476+
)
477+
)
478+
)
479+
480+
const maybeGetAIMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
481+
Effect.flatMap(
482+
Config.Config,
483+
(config) =>
484+
config.enableAI ? getAIMarkdownFiles(config.projectName, modules) : Effect.succeed([])
485+
)
486+
453487
// -------------------------------------------------------------------------------------
454488
// writeMarkdown
455489
// -------------------------------------------------------------------------------------

src/Domain.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import type * as Option from "@effect/data/Option"
6+
import * as ReadonlyArray from "@effect/data/ReadonlyArray"
67
import * as String from "@effect/data/String"
78
import * as order from "@effect/data/typeclass/Order"
89

@@ -112,6 +113,18 @@ export interface Export extends Documentable {
112113
*/
113114
export type Example = string
114115

116+
/**
117+
* @category model
118+
* @since 1.0.0
119+
*/
120+
export type Printable =
121+
| Class
122+
| Constant
123+
| Export
124+
| Function
125+
| Interface
126+
| TypeAlias
127+
115128
// -------------------------------------------------------------------------------------
116129
// constructors
117130
// -------------------------------------------------------------------------------------
@@ -268,6 +281,24 @@ export const createExport = (
268281
signature
269282
})
270283

284+
// -------------------------------------------------------------------------------------
285+
// accessors
286+
// -------------------------------------------------------------------------------------
287+
288+
/**
289+
* @category accessors
290+
* @since 1.0.0
291+
*/
292+
export const printablesFromModule = (module: Module): ReadonlyArray<Printable> =>
293+
ReadonlyArray.getMonoid<Printable>().combineAll([
294+
module.classes,
295+
module.constants,
296+
module.exports,
297+
module.functions,
298+
module.interfaces,
299+
module.typeAliases
300+
])
301+
271302
/**
272303
* @category instances
273304
* @since 1.0.0

src/Markdown.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@ import * as ReadonlyRecord from "@effect/data/ReadonlyRecord"
88
import * as String from "@effect/data/String"
99
import * as Order from "@effect/data/typeclass/Order"
1010
import * as Prettier from "prettier"
11-
import type * as Domain from "./Domain"
11+
import * as Domain from "./Domain"
1212

1313
// eslint-disable-next-line @typescript-eslint/no-var-requires
1414
const toc = require("markdown-toc")
1515

16-
type Printable =
17-
| Domain.Class
18-
| Domain.Constant
19-
| Domain.Export
20-
| Domain.Function
21-
| Domain.Interface
22-
| Domain.TypeAlias
23-
2416
const bold = (s: string) => `**${s}**`
2517

2618
const fence = (language: string, content: string) =>
@@ -191,7 +183,7 @@ const fromTypeAlias = (ta: Domain.TypeAlias): string =>
191183
)
192184

193185
/** @internal */
194-
export const fromPrintable = (p: Printable): string => {
186+
export const fromPrintable = (p: Domain.Printable): string => {
195187
switch (p._tag) {
196188
case "Class":
197189
return fromClass(p)
@@ -208,16 +200,6 @@ export const fromPrintable = (p: Printable): string => {
208200
}
209201
}
210202

211-
const getPrintables = (module: Domain.Module): ReadonlyArray<Printable> =>
212-
ReadonlyArray.getMonoid<Printable>().combineAll([
213-
module.classes,
214-
module.constants,
215-
module.exports,
216-
module.functions,
217-
module.interfaces,
218-
module.typeAliases
219-
])
220-
221203
/**
222204
* @category printers
223205
* @since 1.0.0
@@ -230,7 +212,7 @@ export const printModule = (module: Domain.Module, order: number): string => {
230212
const description = paragraph(getModuleDescription(module))
231213

232214
const content = pipe(
233-
getPrintables(module),
215+
Domain.printablesFromModule(module),
234216
ReadonlyArray.groupBy(({ category }) =>
235217
pipe(
236218
category,
@@ -249,7 +231,7 @@ export const printModule = (module: Domain.Module, order: number): string => {
249231
ReadonlyArray.sort(
250232
Order.contramap(
251233
String.Order,
252-
(printable: Printable) => printable.name
234+
(printable: Domain.Printable) => printable.name
253235
)
254236
),
255237
ReadonlyArray.map(fromPrintable)
@@ -275,6 +257,31 @@ export const printModule = (module: Domain.Module, order: number): string => {
275257
)
276258
}
277259

260+
/**
261+
* @category printers
262+
* @since 1.0.0
263+
*/
264+
export const printPrintableForAI = (
265+
projectName: string,
266+
module: Domain.Module,
267+
printable: Domain.Printable
268+
): string => {
269+
const namespace = module.path.slice(1).join("/").replace(/\.ts$/, "")
270+
return prettify(
271+
[
272+
h1(printable.name),
273+
getDescription(printable.description),
274+
paragraph(
275+
`Part of the \`${namespace}\` module from the \`${projectName}\` package. Also known as \`${namespace}.${printable.name}\`.`
276+
),
277+
printable.examples.map((code) =>
278+
[h3("Example"), paragraph(fence("typescript", code))].join("\n")
279+
)
280+
.join("\n\n")
281+
].join("\n")
282+
)
283+
}
284+
278285
const defaultPrettierOptions: Prettier.Options = {
279286
parser: "markdown",
280287
semi: false,

test/Parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const defaultConfig: Config.Config = {
2121
srcDir: "src",
2222
outDir: "docs",
2323
theme: "pmarsceill/just-the-docs",
24+
enableAI: false,
2425
enableSearch: true,
2526
enforceDescriptions: false,
2627
enforceExamples: false,

0 commit comments

Comments
 (0)