Skip to content

Commit f7b6ab3

Browse files
committed
Improve markup rendering to serve locally hosted content
1 parent 3b3a84f commit f7b6ab3

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

markdown/index.html

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
//<![CDATA[
111111

112112
const repoNames = {
113+
"eclipse-platform/.github": "Platform .github",
113114
"eclipse-platform/eclipse.platform": "Platform",
114115
"eclipse-platform/eclipse.platform.ui": "Platform UI",
115116
"eclipse-platform/www.eclipse.org-eclipse": "Eclipse Website",
@@ -125,10 +126,12 @@
125126
const branch = parts == null ? '' : parts.groups.branch;
126127
const path = parts == null ? '' : parts.groups.path;
127128

129+
const isLocalHost = window.location.hostname == 'localhost';
128130
const selfHosted = repo == 'www.eclipse.org-eclipse';
129131
const repoName = parts == null ? '' : repoNames[`${org}/${repo}`];
130132

131-
defaultAside = toElements(`${markdownAside}`);
133+
const localSiteNavigator = isLocalHost ? `<a href="${scriptBase}markdown/index.html?file=eclipse-platform/www.eclipse.org-eclipse/master/">Eclipse Website Navigator</a>` : '';
134+
defaultAside = toElements(`${markdownAside}${localSiteNavigator}`);
132135

133136
if (parts != null && parts.groups.path.endsWith('.md')) {
134137
tableOfContentsAside = `
@@ -160,8 +163,8 @@ <h2>Table of Contents</h2>
160163
function generateFileList(files) {
161164
const fileElements = files.map(file => {
162165
const fileURL = new URL(file.url);
163-
const fileName = fileURL.pathname.endsWith('/docs') || fileURL.pathname.endsWith('/profile') ?
164-
/(?<filename>[^/]+)$/.exec(fileURL.pathname) :
166+
const fileName = fileURL.pathname.endsWith('/docs') || fileURL.pathname.endsWith('/profile') || fileURL.pathname.endsWith('/') || isLocalHost && file['type'] == 'dir' ?
167+
/(?<filename>[^./][^/]+)\/?$/.exec(fileURL.pathname) :
165168
/(?<filename>[^/]+)\.md$/.exec(fileURL.pathname);
166169
if (fileName == null) {
167170
return '';
@@ -170,7 +173,7 @@ <h2>Table of Contents</h2>
170173
const parts = /\/repos\/(?<org>[^/]+)\/(?<repo>[^/]+)\/contents\/(?<path>.*)/.exec(fileURL.pathname);
171174
const url = new URL(window.location);
172175
url.hash = '';
173-
url.search = `?file=${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`;
176+
url.search = `?file=${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`.replace('//', '/');
174177
const label = niceName(fileName.groups.filename);
175178
return `<div><a href="${url}">${label}<a/></div>\n`;
176179
});
@@ -183,7 +186,7 @@ <h2>Table of Contents</h2>
183186

184187
function generateMarkdown(logicalBaseURL, response) {
185188
if (response instanceof Array) {
186-
generateFileList(response)
189+
generateFileList(response);
187190
} else {
188191
const text = response;
189192
const editLink = `<a id="edit-markdown-link" href=""><span class="orange">\u270E Improve this page</span></a>\n`;
@@ -266,20 +269,51 @@ <h2>Table of Contents</h2>
266269
breadcrumb.append(...toElements(`<li><a href="?file=${org}/${repo}/${branch}${crumbPath}">${segment.length == 0 ? repoName : niceName(segment.replace(/\.md$/, ''))}</a></li>`));
267270
}
268271

272+
269273
const logicalBaseURL = new URL(`https://api.github.com/repos/${org}/${repo}/contents/${path}`);
270-
fetch(selfHosted ? `${scriptBase}${path}` : `${logicalBaseURL}?ref=${branch}`).then(response => {
271-
console.log(response);
272-
return response.text();
273-
}).then(text => {
274-
if (text.startsWith('<')) {
275-
console.log('Unsupported');
276-
} else if (text.startsWith('{') || text.startsWith('[')) {
277-
const json = JSON.parse(text);
278-
generateMarkdown(logicalBaseURL, json instanceof Array ? json : blobToText(json.content));
279-
} else {
280-
generateMarkdown(logicalBaseURL, text);
281-
}
282-
});
274+
const apiURL = `${logicalBaseURL}?ref=${branch}`;
275+
const defaultURL = selfHosted ? `${scriptBase}${path}` : apiURL;
276+
277+
function defaultHandler(url) {
278+
fetch(url).then(response => {
279+
return response.text();
280+
}).then(text => {
281+
if (text.startsWith('<')) {
282+
if (text.startsWith('<img') || text.match(/<ul><li><a href="[^"]+"> Parent Directory<\/a><\/li>/)) {
283+
const links = [...text.matchAll(/href="([^./][^"]+?(\.md|\/))"/g).map(match => {
284+
return {url: `https://api.github.com/repos/${org}/${repo}/contents/${path}/${match[1]}?ref=${branch}`};
285+
})];
286+
generateFileList(links);
287+
} else if (url != apiURL) {
288+
targetElement.innerHTML = `Cannot produce directory listing ${url} redirecting to ${apiURL}.`;
289+
defaultHandler(apiURL);
290+
} else {
291+
targetElement.innerHTML = `Cannot produce directory listing ${url}.`;
292+
}
293+
} else if (text.startsWith('{') || text.startsWith('[')) {
294+
const json = JSON.parse(text);
295+
generateMarkdown(logicalBaseURL, json instanceof Array ? json : blobToText(json.content));
296+
} else {
297+
generateMarkdown(logicalBaseURL, text);
298+
}
299+
});
300+
}
301+
302+
if (!selfHosted && isLocalHost) {
303+
const localURL = new URL(window.location);
304+
localURL.hash = '';
305+
localURL.search = '';
306+
localURL.pathname = `${org}/${repo}/${branch}/${path}`;
307+
fetch(localURL, {method: 'HEAD', cache: "no-store"}).then(response => {
308+
if (response.status == 200 && response.headers.get('Server') == 'org.eclipse.oomph.internal.util.HTTPServer') {
309+
defaultHandler(localURL);
310+
} else {
311+
defaultHandler(defaultURL);
312+
}
313+
});
314+
} else {
315+
defaultHandler(defaultURL);
316+
}
283317
}
284318
}
285319

0 commit comments

Comments
 (0)