Skip to content

Commit 39bac75

Browse files
committed
Absolutize links and images in served page documents
A downloaded page must behave like a downloaded .md whose links are absolute: clicking a workflow reference opens Sim in the browser at that workflow. The standalone renderer (plain download, fullscreen viewer, shares) now compiles sim: links and workspace image refs against getBaseUrl(); in-app surfaces keep relative paths and SPA navigation.
1 parent 15fe31f commit 39bac75

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎apps/sim/lib/workspace-files/page-compile.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ const SIM_RESOURCE_ROUTES: Record<string, (workspaceId: string, id: string) => s
137137
* `data-sim-link` marks them so the preview sandbox can bridge clicks to the
138138
* app router instead of cancelling them.
139139
*/
140-
export function resolveSimResourceLinks(html: string, workspaceId: string): string {
140+
export function resolveSimResourceLinks(html: string, workspaceId: string, baseUrl = ''): string {
141141
return html.replace(
142142
/href="sim:(workflow|table|knowledge|file)\/([^"]+)"/g,
143143
(match, type: string, id: string) => {
144144
const route = SIM_RESOURCE_ROUTES[type]
145-
return route ? `href="${route(workspaceId, id)}" data-sim-link=""` : match
145+
return route ? `href="${baseUrl}${route(workspaceId, id)}" data-sim-link=""` : match
146146
}
147147
)
148148
}
@@ -317,19 +317,25 @@ function compileBody(source: string, lenient = false): string {
317317
*/
318318
export function compileSimPage(
319319
source: string,
320-
options?: { workspaceId?: string; lenient?: boolean }
320+
options?: { workspaceId?: string; lenient?: boolean; baseUrl?: string }
321321
): string {
322322
// Workspace images (`![alt](sim:file/<id>)`) resolve to the authed byte
323323
// route regardless of surface; deep links additionally need the workspace.
324+
// A baseUrl absolutizes both — a DOWNLOADED page opened outside the app
325+
// must reach Sim the way an absolute link in a downloaded .md does,
326+
// instead of resolving dead against file:// or a foreign host.
327+
const origin = options?.baseUrl?.replace(/\/$/, '') ?? ''
324328
const compiled = compileSimPageDocument(source, options?.lenient === true)
325-
.replace(/src="sim:file\/([^"]+)"/g, 'src="/api/files/view/$1"')
329+
.replace(/src="sim:file\/([^"]+)"/g, `src="${origin}/api/files/view/$1"`)
326330
// External links leave the page in a new tab on every surface; in the
327331
// sandboxed preview the bootstrap bridges the click to the host instead.
328332
.replace(
329333
/<a href="(https?:\/\/[^"]+)"/g,
330334
'<a href="$1" target="_blank" rel="noopener noreferrer"'
331335
)
332-
return options?.workspaceId ? resolveSimResourceLinks(compiled, options.workspaceId) : compiled
336+
return options?.workspaceId
337+
? resolveSimResourceLinks(compiled, options.workspaceId, origin)
338+
: compiled
333339
}
334340

335341
function compileSimPageDocument(source: string, lenient = false): string {

‎apps/sim/lib/workspace-files/page-document.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getBaseUrl } from '@/lib/core/utils/urls'
12
import {
23
SIM_ARTIFACT_SHELL,
34
SIM_ARTIFACT_STYLESHEET,
@@ -14,7 +15,10 @@ import { SIM_PAGE_FONT_URL, simPageFontFace } from '@/lib/workspace-files/page-f
1415
* the document also stands alone in a plain browser tab.
1516
*/
1617
export function renderSimPageDocument(source: string, options?: { workspaceId?: string }): string {
17-
const compiled = compileSimPage(source, options)
18+
// Absolute app URLs: the standalone document is also what a user
19+
// downloads, and a downloaded page's links and images must reach Sim
20+
// the way an absolute link in a downloaded .md does.
21+
const compiled = compileSimPage(source, { ...options, baseUrl: getBaseUrl() })
1822
return compiled.replace(
1923
'</head>',
2024
`<style>${simPageFontFace(SIM_PAGE_FONT_URL)}${SIM_ARTIFACT_STYLESHEET}</style>${SIM_ARTIFACT_SHELL}</head>`

0 commit comments

Comments
 (0)