Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/astro-github-loader/src/github.content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ export async function toCollectionEntry({
if (options.linkTransform) {
logger.verbose(`Applying link transformation to ${allFiles.length} files`);

// Resolve stripPrefixes with Starlight default
const stripPrefixes = options.linkTransform.stripPrefixes ?? ['src/content/docs'];

// Generate automatic link mappings from pathMappings
const autoGeneratedMappings = options.includes
? generateAutoLinkMappings(
options.includes,
options.linkTransform.stripPrefixes,
)
? generateAutoLinkMappings(options.includes, stripPrefixes)
: [];

// Combine auto-generated mappings with user-defined mappings
Expand All @@ -453,7 +453,7 @@ export async function toCollectionEntry({
);

processedFiles = globalLinkTransform(allFiles, {
stripPrefixes: options.linkTransform.stripPrefixes,
stripPrefixes,
customHandlers: options.linkTransform.customHandlers,
linkMappings: allLinkMappings,
logger,
Expand Down
26 changes: 26 additions & 0 deletions packages/astro-github-loader/src/github.link-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ describe("globalLinkTransform", () => {
expect(result[0].content).toBe("[See other](/other/)");
});

it("should apply global linkMappings to bare-path links before normalization", () => {
const files: ImportedFile[] = [
createImportedFile(
"docs/markdown/capabilities/guide.md",
"src/content/docs/guide.md",
"[`AccountManager`](docs/markdown/autoapi/algokit_utils/accounts/account_manager/#algokit_utils.accounts.account_manager.AccountManager)",
),
];

const result = globalLinkTransform(files, {
stripPrefixes: ["src/content/docs"],
linkMappings: [
{
pattern: /^docs\/markdown\/autoapi\/algokit_utils\/(.+)/,
replacement: "/docs/algokit-utils/python/latest/api/$1",
global: true,
},
],
logger,
});

expect(result[0].content).toBe(
"[`AccountManager`](/docs/algokit-utils/python/latest/api/accounts/account_manager/#algokit_utils.accounts.account_manager.AccountManager)",
);
});

it("should preserve anchors in transformed links", () => {
const files: ImportedFile[] = [
createImportedFile(
Expand Down
18 changes: 18 additions & 0 deletions packages/astro-github-loader/src/github.link-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ function transformLink(

const { path: linkPath, anchor } = extractAnchor(linkUrl);

// Try global linkMappings on the RAW link path before normalization.
// Bare-path links (e.g., "docs/markdown/autoapi/foo/") are repo-root-relative
// but normalizePath() treats them as file-relative, mangling the path.
// Applying global mappings first lets patterns match the link as written.
if (context.global.linkMappings) {
const globalMappings = context.global.linkMappings.filter((m) => m.global);
if (globalMappings.length > 0) {
const rawMapped = applyLinkMappings(
linkPath + anchor,
globalMappings,
context,
);
if (rawMapped !== linkPath + anchor) {
return `[${linkText}](${rawMapped})`;
}
}
}

// Normalize the link path relative to current file FIRST
const normalizedPath = normalizePath(
linkPath,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-github-loader/src/github.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export interface LinkMapping {
* Configuration for import link transformation
*/
export interface ImportLinkTransformOptions {
/** Base paths to strip from final URLs (e.g., ["src/content/docs"]) */
stripPrefixes: string[];
/** Base paths to strip from final URLs. Defaults to ["src/content/docs"] (Starlight convention). */
stripPrefixes?: string[];
/** Custom handlers for special link types */
customHandlers?: LinkHandler[];
/** Link mappings to transform URLs in markdown links */
Expand Down