-
Notifications
You must be signed in to change notification settings - Fork 508
/
LiveSampleURL.ejs
35 lines (31 loc) · 1018 Bytes
/
LiveSampleURL.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%
// Returns the URL used to access as a live sample the code
// at a given ID.
//
// Parameters:
//
// $0 The ID of the sample's section or containing block.
// $1 The URL from which to pull the sample; if not provided,
// the current page is assumed.
let id = $0;
let remoteUrl = $1;
let pagePath = env.url;
let baseUrl = env.live_samples.base_url;
let samplePath = `${pagePath}/runner.html`.replace(/\/\/+/g, "/");
if (remoteUrl && remoteUrl.length > 0) {
pagePath = remoteUrl;
baseUrl = env.live_samples.legacy_url;
samplePath = `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent(id))}.html`.replace(/\/\/+/g, "/");
}
wiki.ensureExistence(pagePath);
const searchParams = new URLSearchParams([["id", id], ["url", remoteUrl]].filter(([_,v]) => Boolean(v)));
let url;
if (baseUrl.includes('://')) {
url = new URL(baseUrl);
url.pathname = samplePath;
url.search = searchParams.toString();
} else {
url = `${samplePath}?${searchParams.toString()}`;
}
%>
<%= url %>