Skip to content

Commit 447b885

Browse files
committed
Trim markdown URLs for eclipse-platform/www.eclipse.org-eclipse/master`
- Use `f=` instead of `file=` where the prefix `eclipse-platform/www.eclipse.org-eclipse/master` is implicit. - E.g., https://eclipse.dev/eclipse/markdown/?f=news/4.36/index.md
1 parent aae3f9d commit 447b885

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

markdown/index.html

+29-7
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,28 @@
119119
"eclipse-ide/.github": "Eclipse IDE",
120120
};
121121

122-
const file = new URLSearchParams(window.location.search).get('file');
122+
function getFileParameter() {
123+
const search = new URLSearchParams(window.location.search);
124+
return search.get('file') ?? (search.get('f') == null ? null : `eclipse-platform/www.eclipse.org-eclipse/master/${search.get('f')}`);
125+
}
126+
127+
function getMarkdownSearch(path) {
128+
const parts = /eclipse-platform\/www\.eclipse\.org-eclipse\/master(.*)/.exec(path);
129+
if (parts == null) {
130+
return `?file=${path}`;
131+
}
132+
return `?f=${parts[1].replace(/^\//, '')}`;
133+
}
134+
135+
function getMarkdownURL(path) {
136+
const parts = /eclipse-platform\/www\.eclipse\.org-eclipse\/master(.*)/.exec(path);
137+
if (parts == null) {
138+
return `${markdownBase}${path}`;
139+
}
140+
return `${selfHostedMarkdownBase}${parts[1].replace(/^\//,'')}`;
141+
}
142+
143+
const file = getFileParameter();
123144
const parts = /(?<org>[^/]+)\/(?<repo>[^/]+)\/(?<branch>[^/]+)\/(?<path>.*)/.exec(file);
124145
const org = parts == null ? '' : parts.groups.org;
125146
const repo = parts == null ? '' : parts.groups.repo;
@@ -130,7 +151,7 @@
130151
const selfHosted = repo == 'www.eclipse.org-eclipse';
131152
const repoName = parts == null ? '' : repoNames[`${org}/${repo}`];
132153

133-
const localSiteNavigator = isLocalHost ? `<a href="${markdownBase}eclipse-platform/www.eclipse.org-eclipse/master/">Eclipse Website Navigator</a>` : '';
154+
const localSiteNavigator = isLocalHost ? `<a href="${getMarkdownURL('eclipse-platform/www.eclipse.org-eclipse/master/')}">Eclipse Website Navigator</a>` : '';
134155
defaultAside = toElements(`${markdownAside}${localSiteNavigator}`);
135156

136157
if (parts != null && parts.groups.path.endsWith('.md')) {
@@ -192,7 +213,7 @@ <h2>Table of Contents</h2>
192213
const parts = /\/repos\/(?<org>[^/]+)\/(?<repo>[^/]+)\/contents\/(?<path>.*)/.exec(fileURL.pathname);
193214
const url = new URL(window.location);
194215
url.hash = '';
195-
url.search = `?file=${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`.replace('//', '/');
216+
url.search = getMarkdownSearch(`${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`.replace('//', '/'));
196217
const label = niceName(fileName.groups.filename);
197218
return `<div><a href="${url}">${label}<a/></div>\n`;
198219
});
@@ -272,13 +293,13 @@ <h2>Table of Contents</h2>
272293
if (logicalHref.hostname == 'api.github.com') {
273294
const parts = /\/repos\/(?<org>[^/]+)\/(?<repo>[^/]+)\/contents\/(?<path>.*)/.exec(logicalHref.pathname);
274295
if (parts != null) {
275-
url.search = `?file=${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`;
296+
url.search = getMarkdownSearch(`${parts.groups.org}/${parts.groups.repo}/${branch}/${parts.groups.path}`);
276297
a.href = url;
277298
}
278299
} else if (logicalHref.hostname == 'github.com') {
279300
const parts = /(?<org>[^/]+)\/(?<repo>[^/]+)\/blob\/(?<branch>[^/]+)\/(?<path>.*)/.exec(logicalHref.pathname);
280301
if (parts != null) {
281-
url.search = `?file=${parts.groups.org}/${parts.groups.repo}/${parts.groups.branch}/${parts.groups.path}`;
302+
url.search = getMarkdownSearch(`=${parts.groups.org}/${parts.groups.repo}/${parts.groups.branch}/${parts.groups.path}`);
282303
a.href = url;
283304
}
284305
}
@@ -298,7 +319,7 @@ <h2>Table of Contents</h2>
298319
const lastPart = filename == 'index' ? '' : `<li>${niceName(filename)}</li>`;
299320
breadcrumb.append(...toElements(`
300321
<li><a href="${scriptBase}news/">News</a></li>
301-
<li><a href="${markdownBase}eclipse-platform/www.eclipse.org-eclipse/master/news/${version}/index.md">${version}</a></li>
322+
<li><a href="${getMarkdownURL(`eclipse-platform/www.eclipse.org-eclipse/master/news/${version}/index.md`)}">${version}</a></li>
302323
${lastPart}
303324
`));
304325
return true;
@@ -322,7 +343,8 @@ <h2>Table of Contents</h2>
322343
let crumbPath = '';
323344
for (const segment of segments) {
324345
crumbPath = (crumbPath == '/' ? '/' : crumbPath + '/') + segment;
325-
breadcrumb.append(...toElements(`<li><a href="?file=${org}/${repo}/${branch}${crumbPath}">${segment.length == 0 ? repoName : niceName(segment.replace(/\.md$/, ''))}</a></li>`));
346+
const href = getMarkdownSearch(`${org}/${repo}/${branch}${crumbPath}`);
347+
breadcrumb.append(...toElements(`<li><a href="${href}">${segment.length == 0 ? repoName : niceName(segment.replace(/\.md$/, ''))}</a></li>`));
326348
}
327349
}
328350

project.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ window.onscroll = function() {
88
const scriptBase = new URL(".", document.currentScript.src).href
99
const apiGitHub = 'https://api.github.com/repos/eclipse-platform/www.eclipse.org-eclipse/contents/';
1010
const markdownBase = `${scriptBase}markdown/?file=`;
11+
const selfHostedMarkdownBase = `${scriptBase}markdown/?f=`;
1112
const newsBase = `${scriptBase}news/news.html?file=`;
12-
const selfHostedMarkdownBase = `${markdownBase}eclipse-platform/www.eclipse.org-eclipse/master/`;
1313

1414
let meta = toElements(`
1515
<meta charset="utf-8">
@@ -90,7 +90,9 @@ let tableOfContentsAside = '';
9090
let selfContent = document.documentElement.outerHTML;
9191

9292
function redirect(href) {
93-
const location = href != null ? `${href}${window.location.search}${window.location.hash}` : new URL(`${markdownBase}eclipse-platform/www.eclipse.org-eclipse/master${window.location.pathname.replace(/^\/eclipse/, '').replace(/\.html$/, '.md')}`);
93+
const location = href != null ?
94+
`${href}${window.location.search}${window.location.hash}` :
95+
new URL(`${selfHostedMarkdownBase}${window.location.pathname.replace(/^\/eclipse\//, '').replace(/\/$/, '/index.html').replace(/\.html$/, '.md')}`);
9496
const body = document.querySelector('body')
9597
replaceChildren(body, "body", ...toElements(`<div style="display: none">If you are not redirected automatically, follow this <a href='${location}'>link</a>.</div>`));
9698
window.location = location;

0 commit comments

Comments
 (0)