110
110
//<![CDATA[
111
111
112
112
const repoNames = {
113
+ "eclipse-platform/.github" : "Platform .github" ,
113
114
"eclipse-platform/eclipse.platform" : "Platform" ,
114
115
"eclipse-platform/eclipse.platform.ui" : "Platform UI" ,
115
116
"eclipse-platform/www.eclipse.org-eclipse" : "Eclipse Website" ,
125
126
const branch = parts == null ? '' : parts . groups . branch ;
126
127
const path = parts == null ? '' : parts . groups . path ;
127
128
129
+ const isLocalHost = window . location . hostname == 'localhost' ;
128
130
const selfHosted = repo == 'www.eclipse.org-eclipse' ;
129
131
const repoName = parts == null ? '' : repoNames [ `${ org } /${ repo } ` ] ;
130
132
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 } ` ) ;
132
135
133
136
if ( parts != null && parts . groups . path . endsWith ( '.md' ) ) {
134
137
tableOfContentsAside = `
@@ -160,8 +163,8 @@ <h2>Table of Contents</h2>
160
163
function generateFileList ( files ) {
161
164
const fileElements = files . map ( file => {
162
165
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 ) :
165
168
/ (?< filename > [ ^ / ] + ) \. m d $ / . exec ( fileURL . pathname ) ;
166
169
if ( fileName == null ) {
167
170
return '' ;
@@ -170,7 +173,7 @@ <h2>Table of Contents</h2>
170
173
const parts = / \/ r e p o s \/ (?< org > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ c o n t e n t s \/ (?< path > .* ) / . exec ( fileURL . pathname ) ;
171
174
const url = new URL ( window . location ) ;
172
175
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 ( '//' , '/' ) ;
174
177
const label = niceName ( fileName . groups . filename ) ;
175
178
return `<div><a href="${ url } ">${ label } <a/></div>\n` ;
176
179
} ) ;
@@ -183,7 +186,7 @@ <h2>Table of Contents</h2>
183
186
184
187
function generateMarkdown ( logicalBaseURL , response ) {
185
188
if ( response instanceof Array ) {
186
- generateFileList ( response )
189
+ generateFileList ( response ) ;
187
190
} else {
188
191
const text = response ;
189
192
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>
266
269
breadcrumb . append ( ...toElements ( `<li><a href="?file=${ org } /${ repo } /${ branch } ${ crumbPath } ">${ segment . length == 0 ? repoName : niceName ( segment . replace ( / \. m d $ / , '' ) ) } </a></li>` ) ) ;
267
270
}
268
271
272
+
269
273
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 ( / < u l > < l i > < a h r e f = " [ ^ " ] + " > P a r e n t D i r e c t o r y < \/ a > < \/ l i > / ) ) {
283
+ const links = [ ...text . matchAll ( / h r e f = " ( [ ^ . / ] [ ^ " ] + ?( \. m d | \/ ) ) " / 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
+ }
283
317
}
284
318
}
285
319
0 commit comments