@larkiny/astro-github-loader 0.14.0 → 0.14.2
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/github.content.js +4 -2
- package/dist/github.link-transform.js +13 -0
- package/dist/github.types.d.ts +2 -2
- package/package.json +1 -1
- package/src/github.content.ts +5 -5
- package/src/github.link-transform.spec.ts +26 -0
- package/src/github.link-transform.ts +18 -0
- package/src/github.types.ts +2 -2
package/dist/github.content.js
CHANGED
|
@@ -304,9 +304,11 @@ export async function toCollectionEntry({ context, octokit, options, signal, for
|
|
|
304
304
|
let processedFiles = allFiles;
|
|
305
305
|
if (options.linkTransform) {
|
|
306
306
|
logger.verbose(`Applying link transformation to ${allFiles.length} files`);
|
|
307
|
+
// Resolve stripPrefixes with Starlight default
|
|
308
|
+
const stripPrefixes = options.linkTransform.stripPrefixes ?? ['src/content/docs'];
|
|
307
309
|
// Generate automatic link mappings from pathMappings
|
|
308
310
|
const autoGeneratedMappings = options.includes
|
|
309
|
-
? generateAutoLinkMappings(options.includes,
|
|
311
|
+
? generateAutoLinkMappings(options.includes, stripPrefixes)
|
|
310
312
|
: [];
|
|
311
313
|
// Combine auto-generated mappings with user-defined mappings
|
|
312
314
|
const allLinkMappings = [
|
|
@@ -315,7 +317,7 @@ export async function toCollectionEntry({ context, octokit, options, signal, for
|
|
|
315
317
|
];
|
|
316
318
|
logger.debug(`Generated ${autoGeneratedMappings.length} automatic link mappings from pathMappings`);
|
|
317
319
|
processedFiles = globalLinkTransform(allFiles, {
|
|
318
|
-
stripPrefixes
|
|
320
|
+
stripPrefixes,
|
|
319
321
|
customHandlers: options.linkTransform.customHandlers,
|
|
320
322
|
linkMappings: allLinkMappings,
|
|
321
323
|
logger,
|
|
@@ -181,6 +181,19 @@ function transformLink(linkText, linkUrl, context) {
|
|
|
181
181
|
return `[${linkText}](${linkUrl})`;
|
|
182
182
|
}
|
|
183
183
|
const { path: linkPath, anchor } = extractAnchor(linkUrl);
|
|
184
|
+
// Try global linkMappings on the RAW link path before normalization.
|
|
185
|
+
// Bare-path links (e.g., "docs/markdown/autoapi/foo/") are repo-root-relative
|
|
186
|
+
// but normalizePath() treats them as file-relative, mangling the path.
|
|
187
|
+
// Applying global mappings first lets patterns match the link as written.
|
|
188
|
+
if (context.global.linkMappings) {
|
|
189
|
+
const globalMappings = context.global.linkMappings.filter((m) => m.global);
|
|
190
|
+
if (globalMappings.length > 0) {
|
|
191
|
+
const rawMapped = applyLinkMappings(linkPath + anchor, globalMappings, context);
|
|
192
|
+
if (rawMapped !== linkPath + anchor) {
|
|
193
|
+
return `[${linkText}](${rawMapped})`;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
184
197
|
// Normalize the link path relative to current file FIRST
|
|
185
198
|
const normalizedPath = normalizePath(linkPath, context.currentFile.sourcePath, context.global.logger);
|
|
186
199
|
// Apply global path mappings to the normalized path
|
package/dist/github.types.d.ts
CHANGED
|
@@ -38,8 +38,8 @@ export interface LinkMapping {
|
|
|
38
38
|
* Configuration for import link transformation
|
|
39
39
|
*/
|
|
40
40
|
export interface ImportLinkTransformOptions {
|
|
41
|
-
/** Base paths to strip from final URLs
|
|
42
|
-
stripPrefixes
|
|
41
|
+
/** Base paths to strip from final URLs. Defaults to ["src/content/docs"] (Starlight convention). */
|
|
42
|
+
stripPrefixes?: string[];
|
|
43
43
|
/** Custom handlers for special link types */
|
|
44
44
|
customHandlers?: LinkHandler[];
|
|
45
45
|
/** Link mappings to transform URLs in markdown links */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@larkiny/astro-github-loader",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.14.
|
|
4
|
+
"version": "0.14.2",
|
|
5
5
|
"description": "Load content from GitHub repositories into Astro content collections with asset management and content transformations",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"astro",
|
package/src/github.content.ts
CHANGED
|
@@ -434,12 +434,12 @@ export async function toCollectionEntry({
|
|
|
434
434
|
if (options.linkTransform) {
|
|
435
435
|
logger.verbose(`Applying link transformation to ${allFiles.length} files`);
|
|
436
436
|
|
|
437
|
+
// Resolve stripPrefixes with Starlight default
|
|
438
|
+
const stripPrefixes = options.linkTransform.stripPrefixes ?? ['src/content/docs'];
|
|
439
|
+
|
|
437
440
|
// Generate automatic link mappings from pathMappings
|
|
438
441
|
const autoGeneratedMappings = options.includes
|
|
439
|
-
? generateAutoLinkMappings(
|
|
440
|
-
options.includes,
|
|
441
|
-
options.linkTransform.stripPrefixes,
|
|
442
|
-
)
|
|
442
|
+
? generateAutoLinkMappings(options.includes, stripPrefixes)
|
|
443
443
|
: [];
|
|
444
444
|
|
|
445
445
|
// Combine auto-generated mappings with user-defined mappings
|
|
@@ -453,7 +453,7 @@ export async function toCollectionEntry({
|
|
|
453
453
|
);
|
|
454
454
|
|
|
455
455
|
processedFiles = globalLinkTransform(allFiles, {
|
|
456
|
-
stripPrefixes
|
|
456
|
+
stripPrefixes,
|
|
457
457
|
customHandlers: options.linkTransform.customHandlers,
|
|
458
458
|
linkMappings: allLinkMappings,
|
|
459
459
|
logger,
|
|
@@ -120,6 +120,32 @@ describe("globalLinkTransform", () => {
|
|
|
120
120
|
expect(result[0].content).toBe("[See other](/other/)");
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
+
it("should apply global linkMappings to bare-path links before normalization", () => {
|
|
124
|
+
const files: ImportedFile[] = [
|
|
125
|
+
createImportedFile(
|
|
126
|
+
"docs/markdown/capabilities/guide.md",
|
|
127
|
+
"src/content/docs/guide.md",
|
|
128
|
+
"[`AccountManager`](docs/markdown/autoapi/algokit_utils/accounts/account_manager/#algokit_utils.accounts.account_manager.AccountManager)",
|
|
129
|
+
),
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
const result = globalLinkTransform(files, {
|
|
133
|
+
stripPrefixes: ["src/content/docs"],
|
|
134
|
+
linkMappings: [
|
|
135
|
+
{
|
|
136
|
+
pattern: /^docs\/markdown\/autoapi\/algokit_utils\/(.+)/,
|
|
137
|
+
replacement: "/docs/algokit-utils/python/latest/api/$1",
|
|
138
|
+
global: true,
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
logger,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
expect(result[0].content).toBe(
|
|
145
|
+
"[`AccountManager`](/docs/algokit-utils/python/latest/api/accounts/account_manager/#algokit_utils.accounts.account_manager.AccountManager)",
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
123
149
|
it("should preserve anchors in transformed links", () => {
|
|
124
150
|
const files: ImportedFile[] = [
|
|
125
151
|
createImportedFile(
|
|
@@ -314,6 +314,24 @@ function transformLink(
|
|
|
314
314
|
|
|
315
315
|
const { path: linkPath, anchor } = extractAnchor(linkUrl);
|
|
316
316
|
|
|
317
|
+
// Try global linkMappings on the RAW link path before normalization.
|
|
318
|
+
// Bare-path links (e.g., "docs/markdown/autoapi/foo/") are repo-root-relative
|
|
319
|
+
// but normalizePath() treats them as file-relative, mangling the path.
|
|
320
|
+
// Applying global mappings first lets patterns match the link as written.
|
|
321
|
+
if (context.global.linkMappings) {
|
|
322
|
+
const globalMappings = context.global.linkMappings.filter((m) => m.global);
|
|
323
|
+
if (globalMappings.length > 0) {
|
|
324
|
+
const rawMapped = applyLinkMappings(
|
|
325
|
+
linkPath + anchor,
|
|
326
|
+
globalMappings,
|
|
327
|
+
context,
|
|
328
|
+
);
|
|
329
|
+
if (rawMapped !== linkPath + anchor) {
|
|
330
|
+
return `[${linkText}](${rawMapped})`;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
317
335
|
// Normalize the link path relative to current file FIRST
|
|
318
336
|
const normalizedPath = normalizePath(
|
|
319
337
|
linkPath,
|
package/src/github.types.ts
CHANGED
|
@@ -52,8 +52,8 @@ export interface LinkMapping {
|
|
|
52
52
|
* Configuration for import link transformation
|
|
53
53
|
*/
|
|
54
54
|
export interface ImportLinkTransformOptions {
|
|
55
|
-
/** Base paths to strip from final URLs
|
|
56
|
-
stripPrefixes
|
|
55
|
+
/** Base paths to strip from final URLs. Defaults to ["src/content/docs"] (Starlight convention). */
|
|
56
|
+
stripPrefixes?: string[];
|
|
57
57
|
/** Custom handlers for special link types */
|
|
58
58
|
customHandlers?: LinkHandler[];
|
|
59
59
|
/** Link mappings to transform URLs in markdown links */
|