Skip to content

Commit ce17d04

Browse files
committed
chore: add doc generation for AI
1 parent a3b1a09 commit ce17d04

8 files changed

Lines changed: 165 additions & 17 deletions

File tree

docs/modules/Config.ts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface Config {
5757
readonly outDir: string
5858
readonly theme: string
5959
readonly enableSearch: boolean
60+
readonly enableAI: boolean
6061
readonly enforceDescriptions: boolean
6162
readonly enforceExamples: boolean
6263
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)
@@ -37,13 +39,26 @@ Added in v1.0.0
3739
- [Module (interface)](#module-interface)
3840
- [NamedDoc (interface)](#nameddoc-interface)
3941
- [Namespace (interface)](#namespace-interface)
42+
- [Printable (type alias)](#printable-type-alias)
4043
- [Property (interface)](#property-interface)
4144
- [TypeAlias (interface)](#typealias-interface)
4245
- [sorting](#sorting)
4346
- [ByPath](#bypath)
4447

4548
---
4649

50+
# accessors
51+
52+
## printablesFromModule
53+
54+
**Signature**
55+
56+
```ts
57+
export declare const printablesFromModule: (module: Module) => ReadonlyArray<Printable>
58+
```
59+
60+
Added in v1.0.0
61+
4762
# constructors
4863
4964
## createClass
@@ -364,6 +379,16 @@ export interface Namespace extends NamedDoc {
364379

365380
Added in v1.0.0
366381

382+
## Printable (type alias)
383+
384+
**Signature**
385+
386+
```ts
387+
export type Printable = Class | Constant | Export | Function | Interface | TypeAlias | Namespace
388+
```
389+
390+
Added in v1.0.0
391+
367392
## Property (interface)
368393
369394
**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

@@ -40,3 +41,17 @@ console.log(Markdown.printModule(m, 0))
4041
```
4142

4243
Added in v1.0.0
44+
45+
## printPrintableForAI
46+
47+
**Signature**
48+
49+
```ts
50+
export declare const printPrintableForAI: (
51+
projectName: string,
52+
module: Domain.Module,
53+
printable: Domain.Printable
54+
) => Effect.Effect<never, never, string>
55+
```
56+
57+
Added in v1.0.0

src/Config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export interface Config {
7676
readonly outDir: string
7777
readonly theme: string
7878
readonly enableSearch: boolean
79+
readonly enableAI: boolean
7980
readonly enforceDescriptions: boolean
8081
readonly enforceExamples: boolean
8182
readonly enforceVersion: boolean
@@ -128,6 +129,7 @@ export const getDefaultConfig = (name: string, homepage: string): Config => ({
128129
srcDir: "src",
129130
outDir: "docs",
130131
theme: "mikearnaldi/just-the-docs",
132+
enableAI: true,
131133
enableSearch: true,
132134
enforceDescriptions: false,
133135
enforceExamples: false,

src/Core.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import { Path } from "@effect/platform-node"
66
import chalk from "chalk"
7-
import { Console, Effect, Layer, Logger, LogLevel, ReadonlyArray, String } from "effect"
7+
import { Console, Effect, Layer, Logger, LogLevel, pipe, ReadonlyArray, String } from "effect"
88
import * as ChildProcess from "./CommandExecutor.js"
99
import * as Config from "./Config.js"
10-
import type * as Domain from "./Domain.js"
10+
import * as Domain from "./Domain.js"
1111
import * as FileSystem from "./FileSystem.js"
1212
import { SimpleLogger } from "./Logger.js"
13-
import { printModule } from "./Markdown.js"
13+
import { printModule, printPrintableForAI } from "./Markdown.js"
1414
import * as Parser from "./Parser.js"
1515
import * as Process from "./Process.js"
1616

@@ -289,7 +289,8 @@ const getMarkdown = (modules: ReadonlyArray<Domain.Module>) =>
289289
const index = yield* _(getMarkdownIndex)
290290
const yml = yield* _(getMarkdownConfigYML)
291291
const moduleFiles = yield* _(getModuleMarkdownFiles(modules))
292-
return [homepage, index, yml, ...moduleFiles]
292+
const aiFiles = yield* _(maybeGetAIMarkdownFiles(modules))
293+
return [homepage, index, yml, ...moduleFiles, ...aiFiles]
293294
})
294295

295296
const getMarkdownHomepage = Effect.gen(function*(_) {
@@ -388,6 +389,14 @@ const getModuleMarkdownOutputPath = (module: Domain.Module) =>
388389
`${module.path.slice(1).join(path.sep)}.md`
389390
)))
390391

392+
const getAIMarkdownOutputPath = (module: Domain.Module, printable: Domain.Printable) =>
393+
Effect.map(Effect.all([Config.Config, Path.Path]), ([config, path]) =>
394+
path.join(
395+
config.outDir,
396+
"ai",
397+
`${module.path.slice(1).join("-").replace(/\.ts$/, "")}-${printable.name}.md`
398+
))
399+
391400
const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
392401
Effect.forEach(modules, (module, order) =>
393402
Effect.gen(function*(_) {
@@ -396,6 +405,34 @@ const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
396405
return FileSystem.createFile(outputPath, content, true)
397406
}))
398407

408+
const getAIMarkdownFiles = (projectName: string, modules: ReadonlyArray<Domain.Module>) =>
409+
Effect.gen(function*(_) {
410+
const aiModules = pipe(
411+
modules,
412+
ReadonlyArray.flatMap((module) =>
413+
pipe(
414+
Domain.printablesFromModule(module),
415+
ReadonlyArray.map((printable) => ({ module, printable }))
416+
)
417+
),
418+
ReadonlyArray.filter(({ printable }) => printable.description._tag === "Some")
419+
)
420+
421+
return yield* _(Effect.forEach(aiModules, ({ module, printable }) =>
422+
Effect.gen(function*(_) {
423+
const outputPath = yield* _(getAIMarkdownOutputPath(module, printable))
424+
const content = yield* _(printPrintableForAI(projectName, module, printable))
425+
return FileSystem.createFile(outputPath, content, true)
426+
})))
427+
})
428+
429+
const maybeGetAIMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
430+
Effect.flatMap(
431+
Config.Config,
432+
(config) =>
433+
config.enableAI ? getAIMarkdownFiles(config.projectName, modules) : Effect.succeed([])
434+
)
435+
399436
const writeMarkdown = (files: ReadonlyArray<FileSystem.File>) =>
400437
Effect.gen(function*(_) {
401438
const config = yield* _(Config.Config)

src/Domain.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ export interface Namespace extends NamedDoc {
138138
readonly namespaces: ReadonlyArray<Namespace>
139139
}
140140

141+
/**
142+
* @category model
143+
* @since 1.0.0
144+
*/
145+
export type Printable =
146+
| Class
147+
| Constant
148+
| Export
149+
| Function
150+
| Interface
151+
| TypeAlias
152+
| Namespace
153+
141154
// -------------------------------------------------------------------------------------
142155
// constructors
143156
// -------------------------------------------------------------------------------------
@@ -273,6 +286,24 @@ export const createExport = (doc: NamedDoc, signature: string): Export => ({
273286
signature
274287
})
275288

289+
// -------------------------------------------------------------------------------------
290+
// accessors
291+
// -------------------------------------------------------------------------------------
292+
293+
/**
294+
* @category accessors
295+
* @since 1.0.0
296+
*/
297+
export const printablesFromModule = (module: Module): ReadonlyArray<Printable> =>
298+
[
299+
module.classes,
300+
module.constants,
301+
module.exports,
302+
module.functions,
303+
module.interfaces,
304+
module.typeAliases
305+
].flat()
306+
276307
/**
277308
* @category constructors
278309
* @since 1.0.0

src/Markdown.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ import { pipe } from "effect/Function"
66
import * as Prettier from "prettier"
77
import type * as Domain from "./Domain.js"
88

9-
type Printable =
10-
| Domain.Class
11-
| Domain.Constant
12-
| Domain.Export
13-
| Domain.Function
14-
| Domain.Interface
15-
| Domain.TypeAlias
16-
| Domain.Namespace
17-
189
const createHeaderPrinter = (level: number) => (content: string): string =>
1910
"#".repeat(level) + " " + content + "\n\n"
2011

@@ -65,6 +56,28 @@ const printExamples = (es: ReadonlyArray<string>): string =>
6556
)
6657
.join("\n\n")
6758

59+
const printImportDescription = (
60+
projectName: string,
61+
module: Domain.Module,
62+
method: string
63+
): string => {
64+
const namespace = module.path.slice(1).join("/").replace(/\.ts$/, "")
65+
66+
return (
67+
MarkdownPrinter.paragraph(
68+
`To import and use \`${method}\` from the "${module.name}" module:`
69+
) +
70+
MarkdownPrinter.paragraph(
71+
MarkdownPrinter.fence(
72+
"ts",
73+
`import * as ${module.name} from "${projectName}/${namespace}"
74+
// Can be accessed like this
75+
${module.name}.${method}`
76+
)
77+
)
78+
)
79+
}
80+
6881
const printStaticMethod = (m: Domain.Method): string =>
6982
MarkdownPrinter.paragraph(
7083
MarkdownPrinter.h3(printTitle(m.name, m.deprecated, "(static method)")),
@@ -223,7 +236,7 @@ export const printNamespace = (ns: Domain.Namespace, indentation: number): strin
223236
)
224237

225238
/** @internal */
226-
export const print = (p: Printable): string => {
239+
export const print = (p: Domain.Printable): string => {
227240
switch (p._tag) {
228241
case "Class":
229242
return printClass(p)
@@ -242,8 +255,8 @@ export const print = (p: Printable): string => {
242255
}
243256
}
244257

245-
const getPrintables = (module: Domain.Module): ReadonlyArray<Printable> =>
246-
ReadonlyArray.flatten<Printable>([
258+
const getPrintables = (module: Domain.Module): ReadonlyArray<Domain.Printable> =>
259+
ReadonlyArray.flatten<Domain.Printable>([
247260
module.classes,
248261
module.constants,
249262
module.exports,
@@ -295,7 +308,7 @@ export const printModule = (
295308
ReadonlyArray.sort(
296309
Order.mapInput(
297310
String.Order,
298-
(printable: Printable) => printable.name
311+
(printable: Domain.Printable) => printable.name
299312
)
300313
),
301314
ReadonlyArray.map(print)
@@ -331,6 +344,29 @@ export const printModule = (
331344
))
332345
})
333346

347+
/**
348+
* @category printers
349+
* @since 1.0.0
350+
*/
351+
export const printPrintableForAI = (
352+
projectName: string,
353+
module: Domain.Module,
354+
printable: Domain.Printable
355+
) =>
356+
prettify(
357+
[
358+
MarkdownPrinter.h1(printable.name),
359+
printDescription(printable.description),
360+
printImportDescription(projectName, module, printable.name),
361+
printExamples(printable.examples),
362+
printable._tag === "Function"
363+
? printSignatures(printable.signatures)
364+
: printable._tag === "Constant"
365+
? printSignature(printable.signature)
366+
: ""
367+
].join("\n")
368+
)
369+
334370
const defaultPrettierOptions: Prettier.Options = {
335371
parser: "markdown",
336372
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: true,
2425
enableSearch: true,
2526
enforceDescriptions: false,
2627
enforceExamples: false,

0 commit comments

Comments
 (0)