Skip to content

Commit 6e883b2

Browse files
Support arbitrary path prefix (#358)
* Add devcontainer * Fix engines to be compatible with App Engine * Hard code /library path everywhere * Fix some more path prefixes * Remove one library prefix * Add library prefix to search autocomplete select * Add library prefix to internal links * Migrate to NodeJS 12 * Upgrade to Node.js 16 * Don't prefix absolute URL for edit link * Don't prefix absoluter URL for edit footer button * Only prefix internal URLs * Fix url converter * Add deploy workflow * Add build step and create .env file * Base64 encode library service account * Implement PATH_PREFIX env var * Support path prefix in client-side js * Fix section links * Update customizations library name * Don't try to format url fragments * Don't try to format url fragments client side * Format * Remove custom GitHub workflow * Cleanup * Update package.json --------- Co-authored-by: Shane Moore <[email protected]>
1 parent 085566d commit 6e883b2

31 files changed

+135
-110
lines changed

app.yaml

-2
This file was deleted.

layouts/categories/default.ejs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head') %>
3+
<%- include('partials/head', {formatUrl, pathPrefix}) %>
44
<body>
55
<h1 class="visually-hidden">Default template</h1>
6-
<%- include('partials/header') %>
7-
<%- include('partials/nav') %>
6+
<%- include('partials/header', {formatUrl}) %>
7+
<%- include('partials/nav', {formatUrl}) %>
88
<div class="g-body">
99
<div class="g-left-panel">
1010
<% if (locals.sections && locals.sections.length) { %>
1111
<!-- is this TOC -->
12-
<%- include('partials/sectionList') %>
12+
<%- include('partials/sectionList', {formatUrl}) %>
1313
<% } %>
1414
<!-- Hide sibling docs on normal pages -->
1515
<!-- <% if (locals.siblings && siblings.length) { %>
@@ -23,11 +23,11 @@
2323
<% } %>
2424

2525
<% if (locals.children && children.length) { %>
26-
<%- include('partials/childrenList', {children, kicker: template('folder.childrenList.kicker', title)}) %>
26+
<%- include('partials/childrenList', {children, kicker: template('folder.childrenList.kicker', title), formatUrl}) %>
2727
<% } %>
2828
</div>
2929
<%- include('partials/imgModal') %>
30-
<%- include('partials/footer', { pageType: 'document', topLevelFolder: url.split('/')[1] }) %>
30+
<%- include('partials/footer', { pageType: 'document', topLevelFolder: url.split('/')[1], formatUrl }) %>
3131
</div>
3232
</body>
3333
</html>

layouts/errors/403.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('error.403.title')}) %>
3+
<%- include('partials/head', {title: template('error.403.title'), formatUrl, pathPrefix}) %>
44
<body>
5-
<%- include('partials/header', {title: template('error.403.title'), parentLinks: []}) %>
5+
<%- include('partials/header', {title: template('error.403.title'), parentLinks: [], formatUrl}) %>
66
<div id="main-search-page">
77
<div class="main-item">
88
<div class="tagline">
@@ -11,7 +11,7 @@
1111
</div>
1212
</div>
1313

14-
<%- include('partials/footer', {style: 'homepage'}) %>
14+
<%- include('partials/footer', {style: 'homepage', formatUrl}) %>
1515
</div>
1616

1717
</body>

layouts/errors/404.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('error.404.title')}) %>
3+
<%- include('partials/head', {title: template('error.404.title'), formatUrl, pathPrefix}) %>
44
<body>
5-
<%- include('partials/header', {title: template('error.404.title'), parentLinks: []}) %>
5+
<%- include('partials/header', {title: template('error.404.title'), parentLinks: [], formatUrl}) %>
66
<div id="main-search-page">
77
<div class="main-item">
88
<div class="tagline">
99
<h1><%- template('error.404.heading') %></h1>
1010
<p><%- template('error.404.message') %></p>
1111
</div>
12-
<%- include('partials/search', {style: 'homepage'}) %>
12+
<%- include('partials/search', {style: 'homepage', formatUrl}) %>
1313
</div>
1414

15-
<%- include('partials/footer', {style: 'homepage'}) %>
15+
<%- include('partials/footer', {style: 'homepage', formatUrl}) %>
1616
</div>
1717

1818
</body>

layouts/errors/500.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('error.500.title')}) %>
3+
<%- include('partials/head', {title: template('error.500.title'), formatUrl, pathPrefix}) %>
44
<body>
5-
<%- include('partials/header', {title: template('error.500.title'), parentLinks: []}) %>
5+
<%- include('partials/header', {title: template('error.500.title'), parentLinks: [], formatUrl}) %>
66
<div id="main-search-page">
77
<div class="main-item">
88
<div class="tagline">
99
<h1><%- template('error.500.heading') %></h1>
1010
<p><%- template('error.500.message') %></p>
1111
</div>
12-
<%- include('partials/search', {style: 'homepage'}) %>
12+
<%- include('partials/search', {style: 'homepage', formatUrl}) %>
1313
</div>
1414

15-
<%- include('partials/footer', {style: 'homepage'}) %>
15+
<%- include('partials/footer', {style: 'homepage', formatUrl}) %>
1616
</div>
1717

1818
</body>

layouts/pages/categories.ejs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('landing.viewAll')}) %>
3+
<%- include('partials/head', {title: template('landing.viewAll'), formatUrl, pathPrefix}) %>
44
<body>
5-
<%- include('partials/header', {parentLinks: [], title: template('landing.viewAll')}) %>
5+
<%- include('partials/header', {parentLinks: [], title: template('landing.viewAll'), formatUrl}) %>
66

77
<div class="g-upper-body">
8-
<%- include('partials/nav') %>
8+
<%- include('partials/nav', {formatUrl}) %>
99
</div>
1010

1111
<div id="category-page" class="g-body">
1212
<div class="g-main-content">
1313
<% all.forEach((category) => { %>
14-
<%- include('partials/childrenList', {kicker: category.prettyName, children: category.children, link: category.path}) %>
14+
<%- include('partials/childrenList', {kicker: category.prettyName, children: category.children, link: category.path, formatUrl}) %>
1515
<% }) %>
1616
</div>
1717
<script> seeMoreButton() </script>
18-
<%- include('partials/footer', { pageType: 'category_index' }) %>
18+
<%- include('partials/footer', { pageType: 'category_index', formatUrl }) %>
1919
</div>
2020
</body>
2121
</html>

layouts/pages/index.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('branding.prettyName')}) %>
3+
<%- include('partials/head', {title: template('branding.prettyName'), formatUrl, pathPrefix}) %>
44
<body>
55

66
<header id="masthead" class="masthead" role="banner">
@@ -11,11 +11,11 @@
1111

1212
<div id="main-search-page">
1313
<div class="main-item">
14-
<%- include('partials/branding', {context: 'home'}) %>
14+
<%- include('partials/branding', {context: 'home', formatUrl}) %>
1515
<div class="tagline">
1616
<p><%- template('landing.tagline') %> <%- template('landing.quickLink') %></p>
1717
</div>
18-
<%- include('partials/search', {style: 'homepage', focus: 'autofocus', msgOnFocus: template('search.placeholder')}) %>
18+
<%- include('partials/search', {style: 'homepage', focus: 'autofocus', msgOnFocus: template('search.placeholder'), formatUrl}) %>
1919

2020
<div class="featured-cat">
2121
<% if (modules.length) {
@@ -29,7 +29,7 @@
2929
</div>
3030

3131
</div>
32-
<%- include('partials/footer', {style: 'homepage', pageType: 'homepage'}) %>
32+
<%- include('partials/footer', {style: 'homepage', pageType: 'homepage', formatUrl}) %>
3333
</div>
3434
</body>
3535
</html>

layouts/pages/search.ejs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<!DOCTYPE html>
22
<html>
3-
<%- include('partials/head', {title: template('search.results.title', locals.q)}) %>
3+
<%- include('partials/head', {title: template('search.results.title', locals.q), formatUrl, pathPrefix}) %>
44
<body>
55
<%- include('partials/header', {parentLinks: [], title: template('search.results.title', locals.q)}) %>
66

7-
<%- include('partials/nav') %>
7+
<%- include('partials/nav', {formatUrl}) %>
88

99
<div class="g-body">
1010
<div class="g-main-content" id="g-search-page">
1111
<% if (locals.results && results.length) { %>
1212
<h3 class="search-kicker"><%- template('search.results.kicker', locals.q) %></h3>
1313
<% results.forEach((res) => { %>
1414
<div class="search-result">
15-
<h3><a href="<%= res.path %>"><%= res.prettyName %></a></h3>
15+
<h3><a href="<%= formatUrl(res.path) %>"><%= res.prettyName %></a></h3>
1616
<p>
1717
<% if (res.folder) { %>
18-
<%- template('search.results.label.folder', res.folder.path, res.folder.prettyName || 'Home')%>&nbsp;
18+
<%- template('search.results.label.folder', formatUrl(res.folder.path), res.folder.prettyName || 'Home')%>&nbsp;
1919
<% } %>
2020
<% if (res.lastModifyingUser && res.lastModifyingUser.displayName) { %>
2121
<%- template('search.results.label.person', res.lastModifyingUser.displayName) %>
@@ -30,7 +30,7 @@
3030
<% } %>
3131
</div>
3232

33-
<%- include('partials/footer', { pageType: 'search' }) %>
33+
<%- include('partials/footer', { pageType: 'search', formatUrl }) %>
3434
</div>
3535
</body>
3636
</html>

layouts/partials/branding.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div class="branding">
22
<h2 class="branding-heading">
33
<% const shouldLink = locals.context !== 'home' %>
4-
<% if (shouldLink) { %><a id="branding-heading-link" href="/"><% } %>
4+
<% if (shouldLink) { %><a id="branding-heading-link" href="<%= formatUrl('/') %>"><% } %>
55
<span class="visually-hidden"><%- template('branding.org') %></span>
66
<img src=<%- template('branding.icon') %> class="logo" alt="<%- template('branding.org') %>">
77
<% if (shouldLink) { %></a><% } %>
88
</h2>
99
<h3 class="branding-label">
10-
<% if (shouldLink) { %><a class="content-link" href="/"><% } %>
10+
<% if (shouldLink) { %><a class="content-link" href="<%= formatUrl('/') %>"><% } %>
1111
<%- template('branding.name') %>
1212
<% if (shouldLink) { %></a><% } %>
1313
</h3>

layouts/partials/breadcrumb.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<ul class="breadcrumb">
22
<!-- <li class="breadcrumb-item">
3-
<a class="content-link" href="/">Home </a> ⇾
3+
<a class="content-link" href="<%= formatUrl("/") %>">Home </a> ⇾
44
</li> -->
55
<% parentLinks.forEach((link) => { %>
66
<li class="breadcrumb-item">
7-
<a class="content-link" href="<%- link.url %>" ><%= link.name %> </a>
7+
<a class="content-link" href="<%= formatUrl(link.url) %>" ><%= link.name %> </a>
88
</li>
99
<% }) %>
1010
<li class="breadcrumb-item">

layouts/partials/childrenList.ejs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="children-container">
22
<% if (locals.kicker) { %>
33
<h3>
4-
<% if (locals.link) { %><a href="<%= link %>"><% } %>
4+
<% if (locals.link) { %><a href="<%= formatUrl(locals.link) %>"><% } %>
55
<%= kicker %>
66
<% if (locals.link) { %></a><% } %>
77
</h3>
@@ -24,9 +24,10 @@
2424
}
2525
const iconName = iconMap[type] || 'external-link'
2626
const className = `fa-${iconName}`
27+
const href = formatUrl(link.url)
2728
%>
2829
<i class="fa foldericon <%= className %>" aria-hidden="true"></i>
29-
<a class="content-link" href="<%- link.url %>" ><%= link.name %></a>
30+
<a class="content-link" href="<%= href %>" ><%= link.name %></a>
3031
</li>
3132
<% }) %>
3233
</ul>

layouts/partials/folderList.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<% if (parents.includes(folder.id)) { %>
1111
<%= folder.prettyName %>
1212
<% } else { %>
13-
<a href="?id=<%= locals.id %>&dest=<%= folder.id %>"><%= folder.prettyName %></a>
13+
<a href="<%= formatUrl('/') %>?id=<%= locals.id %>&dest=<%= folder.id %>"><%= folder.prettyName %></a>
1414
<% } %>
15-
<%- include('./folderList', {folders: folder.children, id, parents}) %>
15+
<%- include('./folderList', {folders: folder.children, id, parents, formatUrl}) %>
1616
</li>
1717
<% }) %>
1818

layouts/partials/footer.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="<%= style %>">
33
<% if (locals.pager) { %>
44
<% pager.forEach((page, i) => { %>
5-
<a class="button btn-footer" href="<%- page.link %>"><%= i + 1 %></a>
5+
<a class="button btn-footer" href="<%- formatUrl(page.link) %>"><%= i + 1 %></a>
66
<% }) %>
77
<% } %>
88
<% if (locals.editLink) { %>
@@ -37,13 +37,13 @@
3737
// get the userinfo then fire a pageview (can't cache in the page)
3838
$.ajax({
3939
method: 'GET',
40-
url: '/whoami.json'
40+
url: '<%= formatUrl("/whoami.json") %>'
4141
}).always(function (data) {
4242
var userId = (data || {}).analyticsUserId;
4343
if (userId) {
4444
ga('set', 'userId', userId)
4545
46-
if(window.location.pathname === '/') {
46+
if(window.location.pathname === '<%= formatUrl("/") %>') {
4747
$(document).ready(function() {
4848
personalizeHomepage(userId)
4949
})
@@ -71,7 +71,7 @@
7171
if (!lastFilenameFetch || now - lastFilenameFetch.lastFetched > 600000) { // 10 min
7272
$.ajax({
7373
method: 'GET',
74-
url: '/filename-listing.json',
74+
url: '<%= formatUrl("/filename-listing.json") %>',
7575
json: true
7676
}).then(function(data, textStatus, xhr) {
7777
if (xhr.status !== 200) return; // if ajax fails, continue with old listing

layouts/partials/head.ejs

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
<% if (locals.inlineCSS) { %>
1616
<style type="text/css"><%- inlineCSS %></style>
1717
<% } else { %>
18-
<link href="/assets/css/style.css" rel="stylesheet">
18+
<link href="<%= formatUrl('/assets/css/style.css') %>" rel="stylesheet">
1919
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
20-
<script src="/assets/scripts/main.js" charset="utf-8"></script>
20+
<script charset="utf-8">
21+
function formatUrl(url) {
22+
if (url.match(/^https?:\/\//)) return url
23+
if (url.startsWith('/')) return "<%= pathPrefix %>" + url.slice(1)
24+
return exports.pathPrefix + url
25+
}
26+
</script>
27+
<script src="<%= formatUrl('/assets/scripts/main.js') %>" charset="utf-8"></script>
2128
<% } %>
2229
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
2330
<link rel="shortcut icon" href="<%- template('branding.favicon') %>" type="image/x-icon" />

layouts/partials/header.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<header id="masthead" class="masthead" role="banner">
22
<div class="container">
3-
<%- include('branding') %>
4-
<%- include('breadcrumb') %>
5-
<%- include('userTools') %>
3+
<%- include('branding', {formatUrl}) %>
4+
<%- include('breadcrumb', {formatUrl}) %>
5+
<%- include('userTools', {formatUrl}) %>
66
</div>
77
</header>

layouts/partials/landingModule.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const itemsLimited = items.slice(0, 6)
88
<ul class="<%= tag %>-cat-list">
99
<% itemsLimited.forEach((item) => {
1010
if (style === 'button') { %>
11-
<li data-<%= tag %>-id="<%= item.id %>"><a class="button btn-cat" href="<%= item.path %>"><%= item.prettyName %></a></li>
11+
<li data-<%= tag %>-id="<%= item.id %>"><a class="button btn-cat" href="<%= formatUrl(item.path) %>"><%= item.prettyName %></a></li>
1212
<% } else {
1313
const type = item.resourceType
1414
const iconMap = {
@@ -25,12 +25,12 @@ const itemsLimited = items.slice(0, 6)
2525
const className = `fa-${iconName}` %>
2626
<li>
2727
<i class="fa foldericon <%= className %>" aria-hidden="true"></i>
28-
<a href="<%= item.path %>"><%= item.prettyName %></a>
28+
<a href="<%= formatUrl(item.path) %>"><%= item.prettyName %></a>
2929
</li>
3030
<% }
3131
})
3232
if (tag === 'featured') { %>
33-
<li><a class="button btn-homepage" href="/categories"><%- template('landing.viewAll') %></a></li>
33+
<li><a class="button btn-homepage" href="<%= formatUrl('/categories') %>"><%- template('landing.viewAll') %></a></li>
3434
<% } %>
3535
</ul>
36-
</div>
36+
</div>

layouts/partials/nav.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="g-upper-body">
2-
<%- include('search') %>
2+
<%- include('search', {formatUrl}) %>
33
<% if (locals.title) { %>
44
<% if (locals.duplicates) { %>
5-
<%- include('partials/warning', {message: template('warning.duplicate', locals.duplicates, locals.parentId) }) %>
5+
<%- include('partials/warning', {message: template('warning.duplicate', locals.duplicates, locals.parentId), formatUrl}) %>
66
<% } %>
77
<h1 class="headline"><%= title %></h1>
88
<div class="kicker">

layouts/partials/search.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="search-topper">
2-
<form method="GET" action="/search" class="search-container">
2+
<form method="GET" action="<%= formatUrl('/search') %>" class="search-container">
33
<% const placeholder = template('search.placeholder')%>
44
<% const focus = locals.focus || '' %>
55
<% const msgOnFocus = placeholder || '' %>
@@ -9,7 +9,7 @@
99

1010
<% const style = locals.style || 'plaintext' %>
1111
<div class="additional-menus <%= style %>">
12-
<a href="/categories"><button type="button" class="button btn-<%= style %>"><%- template('landing.viewAll') %></button></a>
12+
<a href="<%= formatUrl('/categories') %>"><button type="button" class="button btn-<%= style %>"><%- template('landing.viewAll') %></button></a>
1313
<%# <a href="#"><button type="button" class="btn-plaintext">Onboarding Guide</button></a>%>
1414
</div>
1515

layouts/partials/siblingList.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<% if (link.isCurrent) { %>
1111
<strong><%= link.name %></strong>
1212
<% } else { %>
13-
<a class="content-link" href="<%- link.url %>" ><%= link.name %></a>
13+
<a class="content-link" href="<%- formatUrl(link.url) %>" ><%= link.name %></a>
1414
<% } %>
1515
</li>
1616
<% }) %>

0 commit comments

Comments
 (0)