@larkiny/astro-github-loader 0.14.1 → 0.14.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.
@@ -181,6 +181,26 @@ 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
+ // Only for bare paths — relative (./, ../) and absolute (/) links must flow
189
+ // through normalizePath() first to avoid over-matching by generic global
190
+ // mappings like .md-stripping from generateStarlightLinkMappings().
191
+ const isBareBarePath = !linkPath.startsWith("./") &&
192
+ !linkPath.startsWith("../") &&
193
+ !linkPath.startsWith("/") &&
194
+ !linkPath.includes("://");
195
+ if (isBareBarePath && context.global.linkMappings) {
196
+ const globalMappings = context.global.linkMappings.filter((m) => m.global);
197
+ if (globalMappings.length > 0) {
198
+ const rawMapped = applyLinkMappings(linkPath + anchor, globalMappings, context);
199
+ if (rawMapped !== linkPath + anchor) {
200
+ return `[${linkText}](${rawMapped})`;
201
+ }
202
+ }
203
+ }
184
204
  // Normalize the link path relative to current file FIRST
185
205
  const normalizedPath = normalizePath(linkPath, context.currentFile.sourcePath, context.global.logger);
186
206
  // Apply global path mappings to the normalized path
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.1",
4
+ "version": "0.14.3",
5
5
  "description": "Load content from GitHub repositories into Astro content collections with asset management and content transformations",
6
6
  "keywords": [
7
7
  "astro",
@@ -120,6 +120,97 @@ 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
+
149
+ it("should not early-return relative ./ links when .md-stripping global mappings exist", () => {
150
+ const files: ImportedFile[] = [
151
+ createImportedFile(
152
+ "docs/guide.md",
153
+ "src/content/docs/guide.md",
154
+ "[Subscriber](./subscriber.md)",
155
+ ),
156
+ createImportedFile(
157
+ "docs/subscriber.md",
158
+ "src/content/docs/subscriber.md",
159
+ "# Subscriber",
160
+ ),
161
+ ];
162
+
163
+ const result = globalLinkTransform(files, {
164
+ stripPrefixes: ["src/content/docs"],
165
+ linkMappings: [
166
+ {
167
+ pattern: /\.md(#|$)/,
168
+ replacement: "$1",
169
+ global: true,
170
+ },
171
+ {
172
+ pattern: /\/index(\.md)?$/,
173
+ replacement: "/",
174
+ global: true,
175
+ },
176
+ ],
177
+ logger,
178
+ });
179
+
180
+ // Should resolve via sourceToTargetMap, not early-return from .md stripping
181
+ expect(result[0].content).toBe("[Subscriber](/subscriber/)");
182
+ });
183
+
184
+ it("should not early-return relative ../ links when global mappings exist", () => {
185
+ const files: ImportedFile[] = [
186
+ createImportedFile(
187
+ "docs/guides/intro.md",
188
+ "src/content/docs/guides/intro.md",
189
+ "[Overview](../overview.md)",
190
+ ),
191
+ createImportedFile(
192
+ "docs/overview.md",
193
+ "src/content/docs/overview.md",
194
+ "# Overview",
195
+ ),
196
+ ];
197
+
198
+ const result = globalLinkTransform(files, {
199
+ stripPrefixes: ["src/content/docs"],
200
+ linkMappings: [
201
+ {
202
+ pattern: /\.md(#|$)/,
203
+ replacement: "$1",
204
+ global: true,
205
+ },
206
+ ],
207
+ logger,
208
+ });
209
+
210
+ // Should resolve via normalization + sourceToTargetMap
211
+ expect(result[0].content).toBe("[Overview](/overview/)");
212
+ });
213
+
123
214
  it("should preserve anchors in transformed links", () => {
124
215
  const files: ImportedFile[] = [
125
216
  createImportedFile(
@@ -314,6 +314,32 @@ 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
+ // Only for bare paths — relative (./, ../) and absolute (/) links must flow
322
+ // through normalizePath() first to avoid over-matching by generic global
323
+ // mappings like .md-stripping from generateStarlightLinkMappings().
324
+ const isBareBarePath =
325
+ !linkPath.startsWith("./") &&
326
+ !linkPath.startsWith("../") &&
327
+ !linkPath.startsWith("/") &&
328
+ !linkPath.includes("://");
329
+ if (isBareBarePath && context.global.linkMappings) {
330
+ const globalMappings = context.global.linkMappings.filter((m) => m.global);
331
+ if (globalMappings.length > 0) {
332
+ const rawMapped = applyLinkMappings(
333
+ linkPath + anchor,
334
+ globalMappings,
335
+ context,
336
+ );
337
+ if (rawMapped !== linkPath + anchor) {
338
+ return `[${linkText}](${rawMapped})`;
339
+ }
340
+ }
341
+ }
342
+
317
343
  // Normalize the link path relative to current file FIRST
318
344
  const normalizedPath = normalizePath(
319
345
  linkPath,