Skip to content

Commit 28f5b9c

Browse files
Merge pull request #176 from earthstar-project/self-contained-previews
Self contained previews
2 parents 8823f5c + 83a3077 commit 28f5b9c

File tree

6 files changed

+1881
-17
lines changed

6 files changed

+1881
-17
lines changed

macromania_temporary_monorepo/macromania_defref/mod.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getNameAndDebug } from "../macromania_names_id/mod.tsx";
22
import {
33
addName,
4+
Config,
5+
ConfigWebserverRoot,
46
dependencyJs,
57
hrefToName,
68
IdA,
@@ -1049,7 +1051,7 @@ function mathLink(
10491051
}
10501052

10511053
let exp = <MHref url={url} children={children} />;
1052-
1054+
10531055
// The following line causes a parsing error when the `data` contains an equals sign (`=`), and there seems to be no way to escape it.
10541056
// exp = data === undefined ? exp : <MData data={data}>{exp}</MData>;
10551057

@@ -1086,11 +1088,15 @@ function addDataAttributes(
10861088
previewPath.push(finalComponent);
10871089

10881090
data["tooltip-anchor"] = (
1089-
<impure
1090-
fun={(ctx) => {
1091-
return hrefTo(ctx, absoluteOutFsPath(previewPath));
1092-
}}
1093-
/>
1091+
// <Config
1092+
// options={[<ConfigWebserverRoot linkType="absolute" alwaysUseDomain />]}
1093+
// >
1094+
<impure
1095+
fun={(ctx) => {
1096+
return hrefTo(ctx, absoluteOutFsPath(previewPath));
1097+
}}
1098+
/>
1099+
// </Config>
10941100
);
10951101
}
10961102

macromania_temporary_monorepo/macromania_previews/mod.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function PreviewScope(
143143
function post(ctx: Context) {
144144
setState(ctx, previousState);
145145

146-
// Any css and js dependencies that got registered in this preview scope should also be registered in the parent scope (which will transitievly extend to all ancestors).
146+
// Any css and js dependencies that got registered in this preview scope should also be registered in the parent scope (which will transitively extend to all ancestors).
147147
for (const cssDep of myState.cssDeps) {
148148
if (previousState && previousState.cssDeps.indexOf(cssDep) === -1) {
149149
previousState.cssDeps.push(cssDep);
@@ -246,13 +246,17 @@ export function PreviewScope(
246246
}
247247

248248
return (
249-
<lifecycle pre={pre} post={post}>
250-
<Config options={[<ConfigWebserverRoot linkType="absolute" />]}>
249+
<Config
250+
options={[
251+
<ConfigWebserverRoot linkType="absolute" alwaysUseDomain />,
252+
]}
253+
>
254+
<lifecycle pre={pre} post={post}>
251255
<map fun={createPreviews}>
252256
<exps x={children} />
253257
</map>
254-
</Config>
255-
</lifecycle>
258+
</lifecycle>
259+
</Config>
256260
);
257261
}
258262

macromania_temporary_monorepo/macromania_webserverroot/mod.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ export type WebserverRootConfig = {
2626
* absolute links, or to always choose the shorter link (default).
2727
*/
2828
linkType: "relative" | "absolute" | "shorter";
29+
/**
30+
* Whether to include the domain even when creating a link within the same page. Defaults to false.
31+
*/
32+
alwaysUseDomain?: boolean;
2933
};
3034

3135
const [getConfig, ConfigWebserverRoot] = createConfigOptions<
3236
WebserverRootConfig,
3337
WebserverRootConfig
3438
>(
3539
"ConfigWebserverRoot",
36-
() => ({ linkType: "shorter" }),
40+
() => ({ linkType: "shorter", alwaysUseDomain: false }),
3741
(old, update) => update,
3842
);
3943
export { ConfigWebserverRoot };
@@ -210,33 +214,42 @@ export function hrefTo(ctx: Context, target: OutFsPath): string {
210214
const targetAbsolutePath = resolveRelativePath(ctx, target).components;
211215

212216
if (
217+
!config.alwaysUseDomain &&
213218
currentServerUrl === nullableInfoToUrl(targetInfo) &&
214219
JSON.stringify(currentAbsolutePath) ===
215220
JSON.stringify(targetAbsolutePath)
216221
) {
217222
return ``;
218223
}
219224

220-
if (linkType === "absolute") {
221-
return absoluteHref(targetInfo, targetAbsolutePath, currentServerUrl);
225+
if (linkType === "absolute" || !!config.alwaysUseDomain) {
226+
return absoluteHref(
227+
targetInfo,
228+
targetAbsolutePath,
229+
currentServerUrl,
230+
!!config.alwaysUseDomain,
231+
);
222232
} else if (linkType === "relative") {
223233
return relativeHref(
224234
targetInfo,
225235
targetAbsolutePath,
226236
currentServerUrl,
227237
currentAbsolutePath,
238+
!!config.alwaysUseDomain,
228239
);
229240
} else {
230241
const absolute = absoluteHref(
231242
targetInfo,
232243
targetAbsolutePath,
233244
currentServerUrl,
245+
!!config.alwaysUseDomain,
234246
);
235247
const relative = relativeHref(
236248
targetInfo,
237249
targetAbsolutePath,
238250
currentServerUrl,
239251
currentAbsolutePath,
252+
!!config.alwaysUseDomain,
240253
);
241254

242255
return absolute.length < relative.length ? absolute : relative;
@@ -247,6 +260,7 @@ function absoluteHref(
247260
targetInfo: ServerInfo | null,
248261
targetAbsolutePath: string[],
249262
currentServerUrl: string | null,
263+
alwaysUseDomain: boolean,
250264
): string {
251265
const targetUrl = nullableInfoToUrl(targetInfo);
252266

@@ -255,7 +269,7 @@ function absoluteHref(
255269
: targetAbsolutePath;
256270

257271
if (
258-
targetUrl === currentServerUrl || targetUrl === null
272+
(targetUrl === currentServerUrl && !alwaysUseDomain) || targetUrl === null
259273
) {
260274
return `${join("/", ...sliced)}`;
261275
} else {
@@ -280,11 +294,17 @@ function relativeHref(
280294
targetAbsolutePath: string[],
281295
currentServerUrl: string | null,
282296
currentAbsolutePath: string[],
297+
alwaysUseDomain: boolean,
283298
): string {
284299
const targetUrl = nullableInfoToUrl(targetInfo);
285300

286301
if (targetUrl !== currentServerUrl) {
287-
return absoluteHref(targetInfo, targetAbsolutePath, currentServerUrl);
302+
return absoluteHref(
303+
targetInfo,
304+
targetAbsolutePath,
305+
currentServerUrl,
306+
alwaysUseDomain,
307+
);
288308
}
289309

290310
return `${

src/main.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { data_model } from "./pages/specs/data_model.tsx";
77
import { willow25 } from "./pages/specs/willow25.tsx";
88
import { e2e } from "./pages/specs/e2e.tsx";
99
import { rbsr } from "./pages/specs/rbsr.tsx";
10+
import { uris } from "./pages/specs/uris.tsx";
1011
import { confidential_sync } from "./pages/specs/confidential_sync.tsx";
1112
import { drop_format } from "./pages/specs/drop_format.tsx";
1213
import { grouping_entries } from "./pages/specs/grouping_entries.tsx";
@@ -113,7 +114,7 @@ const exp = (
113114

114115
<Dir name="build">
115116
<ServerOptimisations>
116-
<ServerRoot url="">
117+
<ServerRoot url="https://willowprotocol.org/">
117118
<Dir name="assets">
118119
{/* See https://github.com/worm-blossom/macromania-assets */}
119120
<Assets
@@ -210,6 +211,7 @@ const exp = (
210211
{e2e}
211212
{meadowcap}
212213
{encodings}
214+
{uris}
213215
{drop_format}
214216
{confidential_sync}
215217
{rbsr}

0 commit comments

Comments
 (0)