smalldocs.api_ts
TypeDoc gives each exported item with its JSDoc comment, its types and its defaults. It does not keep the comment after a parameter, so smalldocs reads that from the source file, at the line TypeDoc gives:
export function add(a: number, // the first number
b: number = 1, // the second number
): number { // the sumThe settings are api in _smalldocs.yml, or smalldocs.api in _quarto.yml:
api:
language: typescript
entry: ../src/index.ts # the entry points, one path or a list, or a folder; from the site's folder
tsconfig: ../tsconfig.json # optional
out: api
source: https://github.com/me/mypkg/blob/mainclass Field
Field(name: str, type: str, optional: bool, doc: str, default: str = '', shared: bool = False, listed: bool = False)One field of an interface, a type, a class or a component’s props.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | The field’s name |
type |
str | required | Its type, as TypeScript writes it |
optional |
bool | required | Whether it is written name? |
doc |
str | required | The comment after it, or its JSDoc comment; empty when there is none |
default |
str | '' |
Its default, from a component’s destructured props; empty when there is none |
shared |
bool | False |
Whether its line has a comment that it shares with other fields, so that the comment is no one’s |
listed |
bool | False |
Whether its type has an entry of its own, where check reports the field |
entries_of
entries_of(s: Settings) -> list[Path]Find the entry points that entry names.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
| returns | list[Path] | The entry points, from the site’s folder |
project_of
project_of(s: Settings, entries: list[Path]) -> PathFind the TypeScript project’s folder, where TypeDoc runs and where the paths in its JSON start.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
entries |
list[Path] | required | The entry points |
| returns | Path | The folder TypeDoc runs in: the tsconfig’s, or the nearest with a package.json or a tsconfig.json |
command
command(project: Path) -> list[str]Choose the project’s TypeDoc when node_modules/.bin/typedoc exists, else TypeDoc 0.28 through npx.
| Parameter | Type | Default | Description |
|---|---|---|---|
project |
Path | required | The TypeScript project’s folder |
| returns | list[str] | The command that runs TypeDoc: the project’s own, or npx’s |
run_typedoc
run_typedoc(project: Path, entries: list[Path], tsconfig: Path | None = None) -> dictRun TypeDoc on the entry points, into a temporary file outside the project, and read its JSON.
| Parameter | Type | Default | Description |
|---|---|---|---|
project |
Path | required | The TypeScript project’s folder |
entries |
list[Path] | required | The entry points: files, or folders |
tsconfig |
Path | None | None |
The tsconfig to use; None lets TypeDoc find the project’s |
| returns | dict | TypeDoc’s JSON, with each file named from the project’s folder |
typedoc_json
typedoc_json(s: Settings) -> tuple[dict, Path]Run TypeDoc as the settings say.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
| returns | tuple[dict, Path] | TypeDoc’s JSON, and the project’s folder that its file names start from |
class Sources
Sources(project: Path)The project’s source files, read once, and the comments on their lines.
| Parameter | Type | Default | Description |
|---|---|---|---|
project |
Path | required | The folder that TypeDoc’s file names start from |
Sources.lines
Sources.lines(name: str) -> list[Line]Read one file as lines of code and comments; no lines when it cannot be read.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | A file name from TypeDoc’s JSON |
| returns | list[Line] | Each line as its code and its // comment |
Sources.comment
Sources.comment(node: dict) -> strRead the comment after a field or a member, on the line where it starts, when nothing else follows it on that line.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
dict | required | An item of TypeDoc’s JSON that has sources |
| returns | str | The // comment on its line, when it is the last item there; empty otherwise |
class Line
Line(code: str, text: str, comment: str, raw: str)One line of a source file, split into its code and its comment.
| Parameter | Type | Default | Description |
|---|---|---|---|
code |
str | required | The code, with comments and the insides of strings blanked, so that brackets in them do not count |
text |
str | required | The same code with its strings, at the same columns, to copy a type or a default from |
comment |
str | required | The text of its // comment; empty when there is none |
raw |
str | required | The line as it is |
regex_end
regex_end(raw: str, start: int) -> intFind where a regular expression literal ends, past its escapes and its [...] classes.
| Parameter | Type | Default | Description |
|---|---|---|---|
raw |
str | required | A line |
start |
int | required | The column of a / that may open a regular expression |
| returns | int | The column after the expression and its flags; -1 when the line has no closing / |
split_comments
split_comments(source: str) -> list[Line]Split each line of a file into code and its // comment, past strings, regular expressions and /* */ comments.
| Parameter | Type | Default | Description |
|---|---|---|---|
source |
str | required | A TypeScript file |
| returns | list[Line] | Its lines, each split into code and its // comment |
alone
alone(code: str, column: int) -> boolTell whether nothing follows an item on its line but its ; or ,: not another field, and not the end of what holds it.
| Parameter | Type | Default | Description |
|---|---|---|---|
code |
str | required | A line’s code, as split_comments gives it |
column |
int | required | The column where an item starts |
| returns | bool | Whether the item is the last thing on the line, so that the comment after it is its own |
class Signature
Signature(params: list[str], returns: str, defaults: dict[str, str], destructured: bool = False)What the source of one signature says: the comments after its parameters and its return, and its destructured defaults.
| Parameter | Type | Default | Description |
|---|---|---|---|
params |
list[str] | required | The comment after each parameter, in order; empty for one without |
returns |
str | required | The comment on the line of the return type or of the {; empty when there is none |
defaults |
dict[str, str] | required | The defaults of a destructured first parameter, { a, b = 1 }, by name |
destructured |
bool | False |
Whether the first parameter is destructured, as { a, b } |
read_signature
read_signature(lines: list[Line], line: int, column: int = 0) -> SignatureRead a signature from its source: a comment on a parameter’s line is that parameter’s, one on the line of the return or the { is the return’s.
| Parameter | Type | Default | Description |
|---|---|---|---|
lines |
list[Line] | required | The file, as split_comments gives it |
line |
int | required | The line the signature starts on, from 1 |
column |
int | 0 |
The column its name starts at |
| returns | Signature | The comments, by parameter, and the return’s |
destructured
destructured(pattern: str, kept: str) -> dict[str, str]Read the defaults of a destructured parameter, splitting at the commas outside brackets and strings.
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern |
str | required | The inside of { a, b = 1, c: d = 2 }, without the braces, with its strings blanked |
kept |
str | required | The same, with its strings, at the same columns |
| returns | dict[str, str] | The default of each name that has one |
body_start
body_start(code: str, name: str) -> int | NoneFind the body of interface Name ... { or type Name = {, past the braces inside type parameters.
| Parameter | Type | Default | Description |
|---|---|---|---|
code |
str | required | A line’s code |
name |
str | required | The name of an interface or a type |
| returns | int | None | The column after the { that opens its body; None when the line declares no such object type |
end_field
end_field(f: Field | None) -> NoneTidy a field’s type once it ends.
| Parameter | Type | Default | Description |
|---|---|---|---|
f |
Field | None | required | A field whose type has ended, or None |
declared_fields
declared_fields(lines: list[Line], name: str) -> list[Field] | NoneRead the fields of an interface or an object type that TypeDoc leaves out, because the file does not export it.
| Parameter | Type | Default | Description |
|---|---|---|---|
lines |
list[Line] | required | The file, as split_comments gives it |
name |
str | required | The name of an interface or a type that is not exported |
| returns | list[Field] | None | Its fields, or None when the file declares no such object type |
jsdoc_at
jsdoc_at(raws: list[str], start: int) -> strRead the text of a /** */ comment that opens at a line.
| Parameter | Type | Default | Description |
|---|---|---|---|
raws |
list[str] | required | The lines of a file |
start |
int | required | The line, from 0, where a /** comment opens |
| returns | str | The comment’s text, its lines joined |
header_comment
header_comment(text: str) -> strRead a file’s opening comment as the module’s, when TypeDoc drops it: no @module marks it, and no declaration follows it at once.
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
str | required | A TypeScript file |
| returns | str | The /** */ comment at its top, when no declaration takes it; empty otherwise |
is_jsx
is_jsx(t: dict | None) -> boolTell whether a function returns what React draws.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | None | required | A return type |
| returns | bool | Whether it is what a React component returns: JSX.Element, ReactNode and the like, alone or with null |
wrap
wrap(t: dict) -> strWrite a type that goes before [], or inside a union, in parentheses when it needs them.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | required | A type |
| returns | str | It as text, in parentheses when it is a union, an intersection or a function |
type_text
type_text(t: dict | None) -> strWrite a type the way TypeScript does.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | None | required | A type from TypeDoc’s JSON |
| returns | str | It as TypeScript writes it |
member_type
member_type(c: dict) -> dict | NoneFind the type of a member, whatever its kind.
| Parameter | Type | Default | Description |
|---|---|---|---|
c |
dict | required | A property, an accessor or a method of TypeDoc’s JSON |
| returns | dict | None | Its type: a property’s own, an accessor’s getter’s, or a method’s as a function |
param_name
param_name(p: dict) -> strName a parameter as a reader would.
| Parameter | Type | Default | Description |
|---|---|---|---|
p |
dict | required | A parameter of TypeDoc’s JSON |
| returns | str | Its name; props for a destructured one, which TypeDoc calls __namedParameters |
params_text
params_text(sig: dict) -> strWrite the parameters of a signature: each with its type and default.
| Parameter | Type | Default | Description |
|---|---|---|---|
sig |
dict | required | A signature of TypeDoc’s JSON |
| returns | str | Its parameters as TypeScript writes them, without the parentheses |
type_params
type_params(node: dict) -> strWrite the type parameters of an item.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
dict | required | A signature, a class, an interface or a type of TypeDoc’s JSON |
| returns | str | Its type parameters, <T extends X = Y>; empty when it has none |
structure
structure(text: str) -> strKeep a comment’s paragraphs and lists: an indented - item stays a list item, and indented prose is no code block.
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
str | required | A comment, whose lines a * or the code’s indentation may have pushed right |
| returns | str | The same Markdown: prose at the left, lists as lists with their nesting, fenced code as it is |
comment_text
comment_text(parts: list[dict] | None) -> strWrite the parts of a TypeDoc comment as Markdown.
| Parameter | Type | Default | Description |
|---|---|---|---|
parts |
list[dict] | None | required | The parts of a comment: text, code and inline tags |
| returns | str | The comment as Markdown; a {@link Name} becomes `Name`, which links when linkify is on |
doc
doc(node: dict | None) -> strWrite the doc comment of an item.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
dict | None | required | An item or a signature of TypeDoc’s JSON |
| returns | str | Its JSDoc comment as Markdown: the summary, then @remarks, @example and @deprecated |
returns_doc
returns_doc(sig: dict) -> strRead a signature’s @returns.
| Parameter | Type | Default | Description |
|---|---|---|---|
sig |
dict | required | A signature of TypeDoc’s JSON |
| returns | str | The text of its @returns tag; empty when there is none |
is_void
is_void(t: dict | None) -> boolTell whether a return type says nothing comes back.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | None | required | A return type |
| returns | bool | Whether it is void, which needs no row and no comment |
class Module
Module(name: str, node: dict, ids: dict[int, dict], src: Sources)One page of the reference: an entry point or a file, and what it exports.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | The module’s name, such as components/Button |
node |
dict | required | Its part of TypeDoc’s JSON |
ids |
dict[int, dict] | required | Every item of the JSON by its id, to follow a type to its fields |
src |
Sources | required | The project’s source files |
Module.summary
Module.summary() -> strRead the module’s comment.
| Parameter | Type | Default | Description |
|---|---|---|---|
| returns | str | The module’s comment; its opening comment when TypeDoc has none |
Module.items
Module.items() -> list[tuple[dict, str, int]]List what the module exports, in source order: each item, then its methods and its namespace’s items.
| Parameter | Type | Default | Description |
|---|---|---|---|
| returns | list[tuple[dict, str, int]] | Each item, its name on the page, and its heading level |
Module.walk
Module.walk(children: list[dict], prefix: str, level: int) -> list[tuple[dict, str, int]]List items and what they hold, as items does.
| Parameter | Type | Default | Description |
|---|---|---|---|
children |
list[dict] | required | Items of TypeDoc’s JSON |
prefix |
str | required | The name of the class or namespace they are in, with a dot; empty at the top |
level |
int | required | Their heading level |
| returns | list[tuple[dict, str, int]] | Each item, its name on the page, and its heading level |
Module.default_name
Module.default_name(c: dict) -> strName a default export by the name in its source.
| Parameter | Type | Default | Description |
|---|---|---|---|
c |
dict | required | An item that the file exports as default |
| returns | str | The name the source gives it, as in export default function Button; default when it has none |
Module.fields
Module.fields(t: dict | None) -> list[Field] | NoneFind the fields of a type, following a reference; a type that is not exported is read from its file.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | None | required | A type: an object type, or a reference to an interface or a type |
| returns | list[Field] | None | Its fields, or None when it is no object type |
Module.own_fields
Module.own_fields(node: dict, listed: bool = False) -> list[Field]List the fields of an interface, a type or a class.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
dict | required | An interface, a type or a class of TypeDoc’s JSON, with children |
listed |
bool | False |
Whether the node has an entry of its own, where check reports its fields |
| returns | list[Field] | Its properties and accessors, each with the comment after it or its JSDoc |
Module.props
Module.props(name: str, sig: dict) -> list[Field] | NoneList a React component’s props: a component is a function with a capital name that returns JSX and takes no parameter or one props object.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | The item’s name on the page |
sig |
dict | required | Its first signature |
| returns | list[Field] | None | The fields of its props, when it is a React component; otherwise None |
Module.named
Module.named(sig: dict) -> dictUndo TypeDoc’s naming of a destructured parameter after a @param that is JSDoc’s for one of its fields, as @param a for { a }.
| Parameter | Type | Default | Description |
|---|---|---|---|
sig |
dict | required | A signature of TypeDoc’s JSON |
| returns | dict | The same, its destructured parameter named props again when TypeDoc named it after the @param of one of its fields |
Module.read
Module.read(sig: dict) -> SignatureRead a signature’s comments from its source.
| Parameter | Type | Default | Description |
|---|---|---|---|
sig |
dict | required | A signature of TypeDoc’s JSON |
| returns | Signature | What its source says |
Module.param_rows
Module.param_rows(name: str, sig: dict, constructor: bool = False) -> tuple[list[list[str]], list[str]]Describe a signature by its parameters and its return; a component by its props.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | The item’s name on the page |
sig |
dict | required | A signature of TypeDoc’s JSON |
constructor |
bool | False |
Whether it is a class’s constructor, which has no return row |
| returns | tuple[list[list[str]], list[str]] | The rows of its parameters table, and its gaps for check |
Module.entry
Module.entry(c: dict, name: str, level: int) -> tuple[str, list[str]]Write one item: its signature, its doc comment and its table.
| Parameter | Type | Default | Description |
|---|---|---|---|
c |
dict | required | One item of TypeDoc’s JSON |
name |
str | required | Its name on the page, such as Rect.area |
level |
int | required | Its heading level |
| returns | tuple[str, list[str]] | The Markdown of its entry, without the heading, and its gaps for check |
Module.parts
Module.parts(code: str, *rest: str) -> strJoin the parts of an entry.
| Parameter | Type | Default | Description |
|---|---|---|---|
code |
str | required | The signature, or empty for none |
rest |
str | The doc comment and the tables, each empty when there is none | |
| returns | str | The parts as Markdown, the signature in a ts block |
gap
gap(f: Field) -> strSay why a field has no comment: it has none, or the comment on its line follows more than the field, so it belongs to no field.
| Parameter | Type | Default | Description |
|---|---|---|---|
f |
Field | required | A field with no comment |
| returns | str | What check says of it |
component
component(name: str, sig: dict) -> boolTell a React component by React’s rules: its props may be of any object type, even one that smalldocs cannot read, but not a string or a number.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str | required | A function’s name on the page |
sig |
dict | required | Its signature |
| returns | bool | Whether it is a React component: a capital name, JSX back, and no parameter or one |
primitive
primitive(t: dict) -> boolTell a type that no props object can have.
| Parameter | Type | Default | Description |
|---|---|---|---|
t |
dict | required | A type |
| returns | bool | Whether it is a string, a number, a boolean, a bigint, a symbol, a literal, or a union of them, which no props object is |
fields_table
fields_table(fields: list[Field]) -> strDescribe an object type by its fields.
| Parameter | Type | Default | Description |
|---|---|---|---|
fields |
list[Field] | required | The fields of an interface, a type or a class |
| returns | str | A Markdown table of them: type and description; empty when there are none |
source_link
source_link(s: Settings, src: Sources, c: dict) -> strLink an item to the line that defines it.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
src |
Sources | required | The project’s source files |
c |
dict | required | An item of TypeDoc’s JSON |
| returns | str | A Markdown link to the line that defines it, or empty when there is no source |
modules
modules(data: dict, src: Sources) -> list[Module]List the modules of TypeDoc’s JSON: the project itself for one entry point, its modules for several.
| Parameter | Type | Default | Description |
|---|---|---|---|
data |
dict | required | TypeDoc’s JSON |
src |
Sources | required | The project’s source files |
| returns | list[Module] | One module per entry point, named from the folder they share |
page_paths
page_paths(mods: list[Module], taken: set[str] = frozenset()) -> list[str]Place each module’s page as its file is placed; a module named as a folder, from its index.ts, is that folder’s index, or sits beside a hand-written one.
| Parameter | Type | Default | Description |
|---|---|---|---|
mods |
list[Module] | required | The modules |
taken |
set[str] | frozenset() |
Pages that someone wrote, which a build never writes over, such as components/index.qmd |
| returns | list[str] | The path of each one’s page in out, without .qmd, as its folders: components/Badge |
written_pages
written_pages(s: Settings, mods: list[Module], taken: set[str] = frozenset()) -> tuple[dict[str, str], dict[str, str], set[str]]Write every page in memory: one per module, in its folder, and an index.qmd for each folder with none, that lists its pages.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
mods |
list[Module] | required | The modules |
taken |
set[str] | frozenset() |
Pages that someone wrote, from hand_written |
| returns | tuple[dict[str, str], dict[str, str], set[str]] | Each page’s text by its path in out; the index of names; the full names in it |
page
page(s: Settings, mod: Module, pages: str = '') -> strWrite one module’s page: front matter, the module’s comment, its folder’s pages, then an entry for each item, with an id and a source link.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
mod |
Module | required | One module |
pages |
str | '' |
The table of its folder’s pages, when it is the folder’s index |
| returns | str | The text of its .qmd page |
load
load(s: Settings) -> list[Module]Run TypeDoc and read its modules.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
| returns | list[Module] | The modules that TypeDoc finds |
build
build(s: Settings) -> list[Path]Write one reference page per entry point or file, and _symbols.json, the index of names that links use.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
| returns | list[Path] | The pages written |
check
check(s: Settings, shown: bool = False) -> intPrint how much of each module is documented, then every gap, then the total.
| Parameter | Type | Default | Description |
|---|---|---|---|
s |
Settings | required | The settings |
shown |
bool | False |
Hold only what the pages show; TypeDoc gives only what is exported, so this is always so |
| returns | int | The exit code: 0 when nothing is missing, 1 otherwise |