@gtkx/gir 0.9.3 → 0.9.4
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 +46 -5
package/package.json
CHANGED
package/src/doc-sanitizer.ts
CHANGED
|
@@ -311,6 +311,40 @@ function cleanupWhitespace(text: string): string {
|
|
|
311
311
|
return result.trim();
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
+
interface CodeBlockProtection {
|
|
315
|
+
text: string;
|
|
316
|
+
restore: (formatted: string, linePrefix: string) => string;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function protectCodeBlocks(text: string): CodeBlockProtection {
|
|
320
|
+
const codeBlockPattern = /```[\s\S]*?```/g;
|
|
321
|
+
const codeBlocks: string[] = [];
|
|
322
|
+
|
|
323
|
+
const processed = text.replace(codeBlockPattern, (match) => {
|
|
324
|
+
codeBlocks.push(match);
|
|
325
|
+
return `__PROTECTED_CODE_BLOCK_${codeBlocks.length - 1}__`;
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
const restore = (formatted: string, linePrefix: string): string => {
|
|
329
|
+
let result = formatted;
|
|
330
|
+
for (let i = 0; i < codeBlocks.length; i++) {
|
|
331
|
+
const placeholder = `__PROTECTED_CODE_BLOCK_${i}__`;
|
|
332
|
+
const codeBlock = codeBlocks[i];
|
|
333
|
+
if (codeBlock === undefined) continue;
|
|
334
|
+
|
|
335
|
+
const codeLines = codeBlock.split("\n");
|
|
336
|
+
const prefixedCodeLines = codeLines.map((line, index) => {
|
|
337
|
+
if (index === 0) return line;
|
|
338
|
+
return `${linePrefix}${line}`;
|
|
339
|
+
});
|
|
340
|
+
result = result.replace(placeholder, prefixedCodeLines.join("\n"));
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
return { text: processed, restore };
|
|
346
|
+
}
|
|
347
|
+
|
|
314
348
|
export interface SanitizeDocOptions {
|
|
315
349
|
escapeXmlTags?: boolean;
|
|
316
350
|
namespace?: string;
|
|
@@ -352,15 +386,19 @@ export function formatDoc(doc: string | undefined, indent: string = "", options:
|
|
|
352
386
|
return "";
|
|
353
387
|
}
|
|
354
388
|
|
|
355
|
-
const
|
|
389
|
+
const { text: protectedText, restore } = protectCodeBlocks(sanitized);
|
|
390
|
+
|
|
391
|
+
const lines = protectedText.split("\n").map((line) => line.trim());
|
|
356
392
|
const firstLine = lines[0] ?? "";
|
|
357
393
|
|
|
358
394
|
if (lines.length === 1 && firstLine.length < 80) {
|
|
359
|
-
|
|
395
|
+
const result = `${indent}/** ${firstLine} */\n`;
|
|
396
|
+
return restore(result, `${indent} * `);
|
|
360
397
|
}
|
|
361
398
|
|
|
362
399
|
const formattedLines = lines.map((line) => `${indent} * ${line}`);
|
|
363
|
-
|
|
400
|
+
const result = `${indent}/**\n${formattedLines.join("\n")}\n${indent} */\n`;
|
|
401
|
+
return restore(result, `${indent} * `);
|
|
364
402
|
}
|
|
365
403
|
|
|
366
404
|
interface DocParameter {
|
|
@@ -382,9 +420,11 @@ export function formatMethodDoc(
|
|
|
382
420
|
}
|
|
383
421
|
|
|
384
422
|
const lines: string[] = [];
|
|
423
|
+
let protection: CodeBlockProtection | undefined;
|
|
385
424
|
|
|
386
425
|
if (sanitizedDoc) {
|
|
387
|
-
|
|
426
|
+
protection = protectCodeBlocks(sanitizedDoc);
|
|
427
|
+
for (const line of protection.text.split("\n")) {
|
|
388
428
|
lines.push(` * ${line.trim()}`);
|
|
389
429
|
}
|
|
390
430
|
}
|
|
@@ -401,5 +441,6 @@ export function formatMethodDoc(
|
|
|
401
441
|
return "";
|
|
402
442
|
}
|
|
403
443
|
|
|
404
|
-
|
|
444
|
+
const result = `${indent}/**\n${indent}${lines.join(`\n${indent}`)}\n${indent} */\n`;
|
|
445
|
+
return protection ? protection.restore(result, `${indent} * `) : result;
|
|
405
446
|
}
|