Skip to content

Commit 3d3cb65

Browse files
committed
Fix cross-references and example image paths with base path
1 parent ad038fb commit 3d3cb65

5 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/lib/components/api/DocstringRenderer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { onMount, tick } from 'svelte';
3+
import { base } from '$app/paths';
34
import { goto } from '$app/navigation';
45
import { loadKatex, getKatexCssUrl } from '$lib/utils/katexLoader';
56
import { loadCodeMirrorModules, createEditorExtensions, type CodeMirrorModules } from '$lib/utils/codemirror';
@@ -15,7 +16,7 @@
1516
let { html }: Props = $props();
1617
1718
// Process HTML for cross-references
18-
let processedHtml = $derived(processCrossRefs(html));
19+
let processedHtml = $derived(processCrossRefs(html, base));
1920
2021
let container: HTMLDivElement | undefined = $state();
2122
let katexLoaded = $state(false);

src/lib/components/common/MarkdownRenderer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Mirrors DocstringRenderer functionality: KaTeX math, CodeMirror code blocks, cross-refs
55
*/
66
import { onMount, tick } from 'svelte';
7+
import { base } from '$app/paths';
78
import { goto } from '$app/navigation';
89
import { marked } from 'marked';
910
import { loadKatex, getKatexCssUrl } from '$lib/utils/katexLoader';
@@ -62,7 +63,7 @@
6263
// Protect math blocks before marked processes them
6364
const { text: protected_, blocks } = protectMath(markdown);
6465
const rawHtml = marked.parse(protected_, { async: false }) as string;
65-
return { html: processCrossRefs(rawHtml), mathBlocks: blocks };
66+
return { html: processCrossRefs(rawHtml, base), mathBlocks: blocks };
6667
});
6768
6869
let html = $derived(processedMarkdown.html);

src/lib/components/common/RstRenderer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* For full RST rendering, content should be pre-processed to HTML
66
*/
77
import { onMount, tick } from 'svelte';
8+
import { base } from '$app/paths';
89
import { goto } from '$app/navigation';
910
import { loadKatex, getKatexCssUrl } from '$lib/utils/katexLoader';
1011
import { loadCodeMirrorModules, createEditorExtensions, type CodeMirrorModules } from '$lib/utils/codemirror';
@@ -255,7 +256,7 @@
255256
.replace(/``([^`]+)``/g, '<code>$1</code>');
256257
257258
// Process cross-references for class/function names
258-
para = processCrossRefs(para);
259+
para = processCrossRefs(para, base);
259260
260261
blocks.push({ type: 'paragraph', content: para });
261262
}

src/lib/utils/crossref.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function buildCrossRefIndex(): Map<string, CrossRefTarget> {
2323
const index = new Map<string, CrossRefTarget>();
2424

2525
for (const [packageId, pkg] of Object.entries(apiData)) {
26-
const basePath = `/${packageId}/api`;
26+
const basePath = `${packageId}/api`;
2727

2828
for (const [moduleName, module] of Object.entries(pkg.modules)) {
2929
// Add module
@@ -118,17 +118,20 @@ export function lookupRef(name: string): CrossRefTarget | undefined {
118118
* - Title tag references from docutils: <cite>ClassName</cite>
119119
* - Single-quoted class names in text: 'ClassName'
120120
*/
121-
export function processCrossRefs(html: string, currentPackageId?: string): string {
121+
export function processCrossRefs(html: string, basePath: string = '', currentPackageId?: string): string {
122122
const index = getCrossRefIndex();
123123

124+
// Helper to build full path with base
125+
const fullPath = (path: string) => `${basePath}/${path}`;
126+
124127
// 1. Handle RST role syntax: :class:`Name`, :func:`Name`, :meth:`Name`, :mod:`Name`
125128
// These often get converted to various forms by docutils
126129
html = html.replace(
127130
/<code class="xref[^"]*">([^<]+)<\/code>/g,
128131
(match, name) => {
129132
const target = index.get(name.trim());
130133
if (target) {
131-
return `<a href="${target.path}" class="crossref crossref-${target.type}">${name}</a>`;
134+
return `<a href="${fullPath(target.path)}" class="crossref crossref-${target.type}">${name}</a>`;
132135
}
133136
return match;
134137
}
@@ -141,7 +144,7 @@ export function processCrossRefs(html: string, currentPackageId?: string): strin
141144
const trimmed = name.trim();
142145
const target = index.get(trimmed);
143146
if (target) {
144-
return `<a href="${target.path}" class="crossref crossref-${target.type}">${name}</a>`;
147+
return `<a href="${fullPath(target.path)}" class="crossref crossref-${target.type}">${name}</a>`;
145148
}
146149
return match;
147150
}
@@ -155,7 +158,7 @@ export function processCrossRefs(html: string, currentPackageId?: string): strin
155158
// Skip if it looks like it's already part of a link
156159
const target = index.get(name);
157160
if (target && target.type === 'class') {
158-
return `<a href="${target.path}" class="crossref crossref-class"><code>${name}</code></a>`;
161+
return `<a href="${fullPath(target.path)}" class="crossref crossref-class"><code>${name}</code></a>`;
159162
}
160163
return match;
161164
}
@@ -167,7 +170,7 @@ export function processCrossRefs(html: string, currentPackageId?: string): strin
167170
(match, title, text) => {
168171
const target = index.get(title) || index.get(text);
169172
if (target) {
170-
return `<a href="${target.path}" class="crossref crossref-${target.type}">${text}</a>`;
173+
return `<a href="${fullPath(target.path)}" class="crossref crossref-${target.type}">${text}</a>`;
171174
}
172175
return match;
173176
}
@@ -180,7 +183,7 @@ export function processCrossRefs(html: string, currentPackageId?: string): strin
180183
(match, name) => {
181184
const target = index.get(name);
182185
if (target && target.type === 'class') {
183-
return `<a href="${target.path}" class="crossref crossref-class">'${name}'</a>`;
186+
return `<a href="${fullPath(target.path)}" class="crossref crossref-class">'${name}'</a>`;
184187
}
185188
return match;
186189
}

src/routes/pathsim/examples/[slug]/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script lang="ts">
2+
import { base } from '$app/paths';
23
import { Notebook } from '$lib/components/notebook';
34
import Tooltip from '$lib/components/common/Tooltip.svelte';
45
import type { PageData } from './$types';
56
67
let { data }: { data: PageData } = $props();
78
8-
let basePath = $derived(`/notebooks/${data.packageId}`);
9+
let basePath = $derived(`${base}/notebooks/${data.packageId}`);
910
</script>
1011

1112
<svelte:head>

0 commit comments

Comments
 (0)