@gtkx/gir 0.9.2 → 0.9.3
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/package.json +1 -1
- package/src/doc-sanitizer.ts +56 -21
package/package.json
CHANGED
package/src/doc-sanitizer.ts
CHANGED
|
@@ -127,7 +127,12 @@ function parseGirLink(type: string, reference: string): GirLink | undefined {
|
|
|
127
127
|
};
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
interface LinkFormatOptions {
|
|
131
|
+
linkStyle: "namespaced" | "prefixed";
|
|
132
|
+
currentNamespace: string | undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function formatGirLinkForTsDoc(link: GirLink, options: LinkFormatOptions): string {
|
|
131
136
|
let displayText: string;
|
|
132
137
|
if (link.member) {
|
|
133
138
|
displayText = `${link.target}.${link.member}`;
|
|
@@ -135,7 +140,17 @@ function formatGirLinkForTsDoc(link: GirLink): string {
|
|
|
135
140
|
displayText = link.target;
|
|
136
141
|
}
|
|
137
142
|
|
|
138
|
-
|
|
143
|
+
let linkTarget: string;
|
|
144
|
+
if (options.linkStyle === "prefixed") {
|
|
145
|
+
const effectiveNamespace = link.namespace ?? options.currentNamespace;
|
|
146
|
+
if (effectiveNamespace && effectiveNamespace !== "Gtk") {
|
|
147
|
+
linkTarget = `${effectiveNamespace}${displayText}`;
|
|
148
|
+
} else {
|
|
149
|
+
linkTarget = displayText;
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
linkTarget = link.namespace ? `${link.namespace}.${displayText}` : displayText;
|
|
153
|
+
}
|
|
139
154
|
|
|
140
155
|
switch (link.type) {
|
|
141
156
|
case "class":
|
|
@@ -171,37 +186,37 @@ function formatGirLinkForTsDoc(link: GirLink): string {
|
|
|
171
186
|
}
|
|
172
187
|
}
|
|
173
188
|
|
|
174
|
-
function convertGirLinks(text: string): string {
|
|
189
|
+
function convertGirLinks(text: string, options: LinkFormatOptions): string {
|
|
175
190
|
return text.replace(GIR_LINK_PATTERN, (_, type: string, reference: string) => {
|
|
176
191
|
const link = parseGirLink(type, reference);
|
|
177
192
|
if (!link) {
|
|
178
193
|
return `\`${reference}\``;
|
|
179
194
|
}
|
|
180
|
-
return formatGirLinkForTsDoc(link);
|
|
195
|
+
return formatGirLinkForTsDoc(link, options);
|
|
181
196
|
});
|
|
182
197
|
}
|
|
183
198
|
|
|
184
|
-
const
|
|
185
|
-
Gtk: "gtk4",
|
|
186
|
-
Gdk: "gdk4",
|
|
187
|
-
Gsk: "gsk4",
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
199
|
+
const GTK_DOCS_BASE_URLS: Record<string, string> = {
|
|
200
|
+
Gtk: "https://docs.gtk.org/gtk4",
|
|
201
|
+
Gdk: "https://docs.gtk.org/gdk4",
|
|
202
|
+
Gsk: "https://docs.gtk.org/gsk4",
|
|
203
|
+
GLib: "https://docs.gtk.org/glib",
|
|
204
|
+
GObject: "https://docs.gtk.org/gobject",
|
|
205
|
+
Gio: "https://docs.gtk.org/gio",
|
|
206
|
+
Pango: "https://docs.gtk.org/Pango",
|
|
207
|
+
PangoCairo: "https://docs.gtk.org/PangoCairo",
|
|
208
|
+
GdkPixbuf: "https://docs.gtk.org/gdk-pixbuf",
|
|
209
|
+
Cairo: "https://docs.gtk.org/cairo",
|
|
210
|
+
Adw: "https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1-latest",
|
|
196
211
|
};
|
|
197
212
|
|
|
198
213
|
function getDocsBaseUrl(namespace: string | undefined): string {
|
|
199
214
|
if (!namespace) {
|
|
200
215
|
return "https://docs.gtk.org/gtk4";
|
|
201
216
|
}
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
204
|
-
return
|
|
217
|
+
const baseUrl = GTK_DOCS_BASE_URLS[namespace];
|
|
218
|
+
if (baseUrl) {
|
|
219
|
+
return baseUrl;
|
|
205
220
|
}
|
|
206
221
|
return `https://docs.gtk.org/${namespace.toLowerCase()}`;
|
|
207
222
|
}
|
|
@@ -273,7 +288,21 @@ function convertAtAnnotations(text: string): string {
|
|
|
273
288
|
}
|
|
274
289
|
|
|
275
290
|
function escapeXmlStyleTags(text: string): string {
|
|
276
|
-
|
|
291
|
+
const codeBlockPattern = /```[\s\S]*?```/g;
|
|
292
|
+
const codeBlocks: string[] = [];
|
|
293
|
+
|
|
294
|
+
let processed = text.replace(codeBlockPattern, (match) => {
|
|
295
|
+
codeBlocks.push(match);
|
|
296
|
+
return `__CODE_BLOCK_${codeBlocks.length - 1}__`;
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
processed = processed.replace(/<(\/?)(child|object|property|signal|template|style|item|attribute)>/gi, "`<$1$2>`");
|
|
300
|
+
|
|
301
|
+
for (let i = 0; i < codeBlocks.length; i++) {
|
|
302
|
+
processed = processed.replace(`__CODE_BLOCK_${i}__`, codeBlocks[i] ?? "");
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return processed;
|
|
277
306
|
}
|
|
278
307
|
|
|
279
308
|
function cleanupWhitespace(text: string): string {
|
|
@@ -285,6 +314,7 @@ function cleanupWhitespace(text: string): string {
|
|
|
285
314
|
export interface SanitizeDocOptions {
|
|
286
315
|
escapeXmlTags?: boolean;
|
|
287
316
|
namespace?: string;
|
|
317
|
+
linkStyle?: "namespaced" | "prefixed";
|
|
288
318
|
}
|
|
289
319
|
|
|
290
320
|
export function sanitizeDoc(doc: string, options: SanitizeDocOptions = {}): string {
|
|
@@ -295,7 +325,12 @@ export function sanitizeDoc(doc: string, options: SanitizeDocOptions = {}): stri
|
|
|
295
325
|
result = convertMarkdownImageUrls(result, baseUrl);
|
|
296
326
|
result = convertKbdElements(result);
|
|
297
327
|
result = stripHtmlLinks(result);
|
|
298
|
-
|
|
328
|
+
|
|
329
|
+
const linkFormatOptions: LinkFormatOptions = {
|
|
330
|
+
linkStyle: options.linkStyle ?? "namespaced",
|
|
331
|
+
currentNamespace: options.namespace,
|
|
332
|
+
};
|
|
333
|
+
result = convertGirLinks(result, linkFormatOptions);
|
|
299
334
|
result = convertAtAnnotations(result);
|
|
300
335
|
|
|
301
336
|
if (options.escapeXmlTags) {
|