-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path404.html
More file actions
219 lines (187 loc) · 8.47 KB
/
Copy path404.html
File metadata and controls
219 lines (187 loc) · 8.47 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
(function() {
var baseUrl = ['4000', '4200'].includes(window.location.port) ? `http://localhost:${window.location.port}` : 'https://www.juncture-digital.io';
document.head.append(Object.assign(document.createElement('link'), { rel: 'stylesheet', href: `${baseUrl}/css/juncture.css`} ))
document.head.append(Object.assign(document.createElement('link'), { rel: 'stylesheet', href: `${baseUrl}/css/index.css`} ))
window.dynamicBaseUrl = baseUrl;
})();
</script>
<style>
body { opacity: 0; transition: opacity 0.5s ease-in-out; max-width: 1000px; margin: auto; }
</style>
</head>
<body>
<main class="page-content">{{ content }}</main>
<script>
const moved = new Set('audio header image map youtube'.split(' '))
const pathRoot = location.pathname.split('/')[1]
if (moved.has(pathRoot)) location.pathname = `/components/${pathRoot}`
</script>
<script src="https://cdn.jsdelivr.net/npm/js-yaml@4.1.0/dist/js-yaml.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/markdown-it@14.1.0/dist/markdown-it.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/markdown-it-attrs@4.3.1/markdown-it-attrs.browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/markdown-it-footnote@4.0.0/dist/markdown-it-footnote.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/markdown-it-deflist@3.0.0/dist/markdown-it-deflist.min.js"></script>
<script type="module">
const addScript = async () => {
var baseUrl = (window.location.port === '4200') ? 'http://localhost:4200' : 'https://www.juncture-digital.io';
let [owner, repo, ...rest] = location.pathname.split('/').filter(p => p)
rest = rest.filter(p => p !== 'undefined')
let branch
if (rest[0] === 'main' || rest[0] === 'master') branch = rest.shift()
else {
const repoBranches = await branches(owner, repo)
if (repoBranches?.length === 1) branch = repoBranches[0]
else branch = await defaultBranch(owner, repo)
}
let path = rest ? rest.join('/') : ''
console.log(`addScript: owner=${owner}, repo=${repo}, branch=${branch}, rest=${rest}, path=${path}`)
window.jekyll = {github: { owner_name: owner, repository_name: repo, source: { branch: branch } } }
let ghbase = `${owner}/${repo}/${branch}/${path}`
const scriptEl = Object.assign(document.createElement('script'), { id: 'loader', src: `${baseUrl}/js/juncture.js`} );
scriptEl.dataset.ghbase = ghbase;
document.body.append(scriptEl);
}
const branches = async (acct, repo) => {
let resp = await fetch(`https://api.github.com/repos/${acct}/${repo}/branches`, {headers: { Accept: 'application/vnd.github+json' } })
if (resp.ok) {
resp = await resp.json()
return resp.map(b => b.name)
}
}
const defaultBranch = async (acct, repo) => {
let defaultBranch = null
let url = `https://api.github.com/repos/${acct}/${repo}`
let resp = await fetch(url)
if (resp.ok) {
resp = await resp.json()
defaultBranch = resp.default_branch
}
return defaultBranch
}
const getGhFile = async (acct, repo, branch, path) => {
let url = `https://api.github.com/repos/${acct}/${repo}/contents/${path}?ref=${branch}`
let resp = await fetch(url, {cache: 'no-cache'})
if (resp.ok) {
let payload = await resp.json()
let content = decodeURIComponent(escape(atob(payload.content)))
return {status: resp.status, content}
} else if (resp.status === 403 || resp.status === 401) { // access problem, possibly api rate limit exceeded
url = `https://raw.githubusercontent.com/${acct}/${repo}/${branch}/${path}`
resp = await fetch(url)
if (resp.ok) {
let content = await resp.text()
return {status: resp.status, content}
} else {
return {status: resp.status, content: null}
}
} else {
return {status: resp.status, content: null}
}
}
const getGhMarkdown = async (owner, repo, branch, path) => {
let extension = path.slice(-3) === '.md' ? 'md' : ''
// console.log(`getGhMarkdown: owner=${owner}, repo=${repo}, branch=${branch}, path=${path}, extension=${extension}`)
if (extension === 'md') {
let resp = await getGhFile(owner, repo, branch, path)
return resp.content
} else {
let promises = [
getGhFile(owner, repo, branch, path ? `${path}/index.md` : 'index.md'),
getGhFile(owner, repo, branch, path ? `${path}/README.md` : 'README.md')
]
if (path) promises.push(getGhFile(owner, repo, branch, `${path}.md`))
return await Promise.all(promises).then(resp => {
let found = resp.find(r => r?.status === 200)
return found?.content || ''
})
}
}
const getLocalMarkdown = async () => {
let href = location.href[location.href.length-1] === '/' ? location.href.slice(0,-1) : location.href
let extension = location.pathname.slice(-3) === '.md' ? 'md' : ''
if (extension === 'md') {
let resp = await fetch(location.href)
return resp.content
} else {
return await Promise.all([
fetch(`${href}.md`),
fetch(`${href}/README.md`),
fetch(`${href}/index.md`)
]).then(resp => resp.find(r => r?.status === 200)?.text())
}
}
let main = document.querySelector('main')
let [owner, repo, ...rest] = location.pathname.split('/').filter(p => p)
rest = rest.filter(p => p !== 'undefined')
let branch
if (rest[0] === 'main' || rest[0] === 'master') {
branch = rest.shift()
} else {
const repoBranches = await branches(owner, repo)
if (repoBranches?.length === 1) branch = repoBranches[0]
else branch = await defaultBranch(owner, repo)
}
let path = rest ? rest.join('/') : ''
window.jekyll = {github:{owner_name:owner,repository_name:repo,source:{branch:branch}}}
console.log(`404: owner=${owner}, repo=${repo}, branch=${branch}, rest=${rest}, path=${path}`)
history.pushState({}, '', path ? `/${owner}/${repo}/${branch}/${path}` : `/${owner}/${repo}/${branch}`);
let markdown = await getGhMarkdown(owner, repo, branch, path)
if (!markdown) markdown = await getLocalMarkdown()
if (markdown) {
const fmRegex = /^---\s*[\r\n]+([\s\S]*?)[\r\n]+---[\r\n]+/;
// Match the front matter using the regex.
const match = markdown.match(fmRegex);
let frontMatter = {};
if (match) {
// Convert the captured front matter (YAML) into a JavaScript object.
frontMatter = jsyaml.load(match[1]);
// Remove the front matter block from the Markdown text.
markdown = markdown.replace(fmRegex, '').replace(/\{#paragraph[0-9]+\}/g, '');
}
markdown = markdown.replace(/{#paragraph\d+}/g, '')
const md = markdownit({ html: true })
.use(markdownItAttrs, {leftDelimiter: '{:', rightDelimiter: '}'})
.use(markdownitFootnote);
let html = new DOMParser().parseFromString(md.render(markdown), 'text/html')
let style = html.querySelector('style')
let body = html.querySelector('body')
// remove "editor" and "view as" buttons
Array.from(body.querySelectorAll('a > img'))
.find(img => img.src.indexOf('ve-button') > -1 || img.parentElement.href.indexOf('juncture-digital.org') > 0)
?.parentElement?.parentElement?.remove()
Array.from(body.querySelectorAll('h1 + p, h2 + p, h3 + p, h4 + p, h5 + p, h6 + p'))
.filter(p => !p.textContent)
.forEach(p => {
let heading = p.previousElementSibling
if (p.id) heading.id = p.id
p.classList.forEach(c => heading.classList.add(c))
if (p.getAttribute('style')) heading.setAttribute('style', p.getAttribute('style'))
p.remove()
})
main.setAttribute('style', 'padding:0 1em;' + (frontMatter['max-width'] ? ` max-width:${frontMatter['max-width']}; margin:0 auto;` : ''))
main.innerHTML = (style ? `${style.outerHTML}\n` : '') + body.innerHTML
addScript()
/*
let junctureScript = document.createElement('script')
junctureScript.id = 'junctureScript';
junctureScript.src = `${window.dynamicBaseUrl}/js/index.js`
junctureScript.dataset.ghbase = `${owner}/${repo}/${branch}/${path}`
junctureScript.type = 'module'
document.body.appendChild(junctureScript)
*/
} else {
main.innerHTML = `<div class="container">
<h1>404</h1>
<p><strong>Page not found :(</strong></p>
<p>The requested page could not be found.</p>
</div>`
}
</script>
</body>
</html>