@statewalker/site-api-reference 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -16,6 +16,12 @@ export interface ApiPage {
16
16
  export interface ApiPagesOptions {
17
17
  /** Slug of the package's README page; symbol pages go below it. */
18
18
  base: string;
19
+ /**
20
+ * Where a relative link to one of the package's files goes (`[the guide](docs/guide.md)` in the
21
+ * README): `file` relative to the package folder, `page` the slug of the page the link is on.
22
+ * The link's `#anchor` is added after. Default: the link as written.
23
+ */
24
+ fileLink?: (file: string, page: string) => string;
19
25
  }
20
26
  /**
21
27
  * The pages of one package's reference: its README first, then one page per exported class,
package/dist/index.mjs CHANGED
@@ -72,8 +72,9 @@ function apiPages(project, opts) {
72
72
  }
73
73
  };
74
74
  place(project, opts.base);
75
- const r = new Renderer(slugOf);
75
+ const r = new Renderer(slugOf, project.files?.entries, opts.fileLink);
76
76
  const pkgName = project.packageName ?? project.name;
77
+ r.page = opts.base;
77
78
  const pages = [{
78
79
  slug: opts.base,
79
80
  title: pkgName,
@@ -81,20 +82,26 @@ function apiPages(project, opts) {
81
82
  description: firstParagraph(project.readme),
82
83
  html: [project.readme ? `<div class="api-readme">${r.markdown(project.readme)}</div>` : "", r.index(project, entries)].join("\n")
83
84
  }];
84
- for (const m of modules) pages.push({
85
- slug: m.slug,
86
- title: `${pkgName}/${m.decl.name}`,
87
- kind: "module",
88
- description: firstParagraph(m.decl.comment?.summary),
89
- html: [r.comment(m.decl.comment), r.index(m.decl, entries)].join("\n")
90
- });
91
- for (const e of entries) pages.push({
92
- slug: e.slug,
93
- title: e.decl.name,
94
- kind: e.kind,
95
- description: firstParagraph(e.decl.comment?.summary ?? e.decl.signatures?.[0]?.comment?.summary),
96
- html: r.declaration(e.decl, e.kind)
97
- });
85
+ for (const m of modules) {
86
+ r.page = m.slug;
87
+ pages.push({
88
+ slug: m.slug,
89
+ title: `${pkgName}/${m.decl.name}`,
90
+ kind: "module",
91
+ description: firstParagraph(m.decl.comment?.summary),
92
+ html: [r.comment(m.decl.comment), r.index(m.decl, entries)].join("\n")
93
+ });
94
+ }
95
+ for (const e of entries) {
96
+ r.page = e.slug;
97
+ pages.push({
98
+ slug: e.slug,
99
+ title: e.decl.name,
100
+ kind: e.kind,
101
+ description: firstParagraph(e.decl.comment?.summary ?? e.decl.signatures?.[0]?.comment?.summary),
102
+ html: r.declaration(e.decl, e.kind)
103
+ });
104
+ }
98
105
  return pages;
99
106
  }
100
107
  /** Children in TypeDoc's group order (Classes, Interfaces, Type Aliases, …), then the rest. */
@@ -123,8 +130,14 @@ function firstParagraph(parts) {
123
130
  }
124
131
  var Renderer = class {
125
132
  slugOf;
126
- constructor(slugOf) {
133
+ files;
134
+ fileLink;
135
+ /** The slug of the page being rendered. */
136
+ page = "";
137
+ constructor(slugOf, files, fileLink) {
127
138
  this.slugOf = slugOf;
139
+ this.files = files;
140
+ this.fileLink = fileLink;
128
141
  }
129
142
  link(name, target) {
130
143
  const slug = typeof target === "number" ? this.slugOf.get(target) : void 0;
@@ -133,7 +146,15 @@ var Renderer = class {
133
146
  /** Comment parts as Markdown (links resolved to page URLs), rendered to HTML. */
134
147
  markdown(parts) {
135
148
  if (!parts?.length) return "";
136
- const text = parts.map((p) => {
149
+ const text = parts.map((p, i) => {
150
+ if (p.kind === "relative-link") {
151
+ const file = p.target !== void 0 ? this.files?.[String(p.target)] : void 0;
152
+ const before = parts[i - 1];
153
+ const destination = before?.kind === "text" && /(\]\(|\]:\s*)$/.test(before.text);
154
+ if (!this.fileLink || file === void 0 || !destination) return p.text;
155
+ const anchor = p.targetAnchor ? `#${p.targetAnchor}` : "";
156
+ return `<${this.fileLink(file, this.page)}${anchor}>`;
157
+ }
137
158
  if (p.kind !== "inline-tag") return p.text;
138
159
  const slug = typeof p.target === "number" ? this.slugOf.get(p.target) : void 0;
139
160
  const label = `\`${p.text.replace(/`/g, "")}\``;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/site-api-reference",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "API reference pages for project sites: TypeDoc JSON of a package in, page models out.",
package/src/index.ts CHANGED
@@ -32,6 +32,12 @@ export interface ApiPage {
32
32
  export interface ApiPagesOptions {
33
33
  /** Slug of the package's README page; symbol pages go below it. */
34
34
  base: string;
35
+ /**
36
+ * Where a relative link to one of the package's files goes (`[the guide](docs/guide.md)` in the
37
+ * README): `file` relative to the package folder, `page` the slug of the page the link is on.
38
+ * The link's `#anchor` is added after. Default: the link as written.
39
+ */
40
+ fileLink?: (file: string, page: string) => string;
35
41
  }
36
42
 
37
43
  // TypeDoc's ReflectionKind values (stable across 0.2x).
@@ -110,8 +116,9 @@ export function apiPages(project: JSONOutput.ProjectReflection, opts: ApiPagesOp
110
116
  };
111
117
  place(project, opts.base);
112
118
 
113
- const r = new Renderer(slugOf);
119
+ const r = new Renderer(slugOf, project.files?.entries, opts.fileLink);
114
120
  const pkgName = project.packageName ?? project.name;
121
+ r.page = opts.base;
115
122
  const pages: ApiPage[] = [
116
123
  {
117
124
  slug: opts.base,
@@ -125,6 +132,7 @@ export function apiPages(project: JSONOutput.ProjectReflection, opts: ApiPagesOp
125
132
  },
126
133
  ];
127
134
  for (const m of modules) {
135
+ r.page = m.slug;
128
136
  pages.push({
129
137
  slug: m.slug,
130
138
  title: `${pkgName}/${m.decl.name}`,
@@ -134,6 +142,7 @@ export function apiPages(project: JSONOutput.ProjectReflection, opts: ApiPagesOp
134
142
  });
135
143
  }
136
144
  for (const e of entries) {
145
+ r.page = e.slug;
137
146
  pages.push({
138
147
  slug: e.slug,
139
148
  title: e.decl.name,
@@ -186,8 +195,18 @@ function firstParagraph(parts: Part[] | undefined): string | undefined {
186
195
 
187
196
  class Renderer {
188
197
  private slugOf: Map<number, string>;
189
- constructor(slugOf: Map<number, string>) {
198
+ private files: Record<string, string> | undefined;
199
+ private fileLink: ApiPagesOptions["fileLink"];
200
+ /** The slug of the page being rendered. */
201
+ page = "";
202
+ constructor(
203
+ slugOf: Map<number, string>,
204
+ files: Record<string, string> | undefined,
205
+ fileLink: ApiPagesOptions["fileLink"],
206
+ ) {
190
207
  this.slugOf = slugOf;
208
+ this.files = files;
209
+ this.fileLink = fileLink;
191
210
  }
192
211
 
193
212
  link(name: string, target: unknown): string {
@@ -199,7 +218,18 @@ class Renderer {
199
218
  markdown(parts: Part[] | undefined): string {
200
219
  if (!parts?.length) return "";
201
220
  const text = parts
202
- .map((p) => {
221
+ .map((p, i) => {
222
+ if (p.kind === "relative-link") {
223
+ // TypeDoc's file registry names the target relative to the package folder.
224
+ const file = p.target !== undefined ? this.files?.[String(p.target)] : undefined;
225
+ // TypeDoc also marks paths in HTML attributes (`<img src>`), which are no Markdown
226
+ // link destination: only a destination (`](...)`, `]: ...`) is rewritten.
227
+ const before = parts[i - 1];
228
+ const destination = before?.kind === "text" && /(\]\(|\]:\s*)$/.test(before.text);
229
+ if (!this.fileLink || file === undefined || !destination) return p.text;
230
+ const anchor = p.targetAnchor ? `#${p.targetAnchor}` : "";
231
+ return `<${this.fileLink(file, this.page)}${anchor}>`;
232
+ }
203
233
  if (p.kind !== "inline-tag") return p.text;
204
234
  const slug = typeof p.target === "number" ? this.slugOf.get(p.target) : undefined;
205
235
  const label = `\`${p.text.replace(/`/g, "")}\``;