Skip to content

Commit 1bb8650

Browse files
vnickolovknutwalker
andcommitted
Retrieve the available Docs versions from S3
Co-authored-by: Paul Horn <[email protected]>
1 parent 5adf8c0 commit 1bb8650

File tree

4 files changed

+120
-62
lines changed

4 files changed

+120
-62
lines changed

doc/javascript/mp-nav.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ function highlightToc() {
8484
}
8585

8686
// Highlight the active publication in the docs library header
87-
function highlightLibraryHeader() {
88-
var thisName = window.docMeta.name
87+
async function highlightLibraryHeader() {
88+
var meta = await window.docMeta;
89+
var thisName = meta.name
8990
var thisEntry;
9091
$('header > ul.documentation-library').children('li').children('a').each(
9192
function (key, value) {

doc/javascript/version.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
1-
window.docMeta = (function () {
2-
var version = '1.1';
3-
var name = 'graph-data-science';
4-
var href = window.location.href;
1+
window.docMeta = (async function () {
2+
const version = '1.1';
3+
const name = 'graph-data-science';
4+
5+
const href = window.location.href;
6+
const thisPubBaseUri = href.substring(0, href.indexOf(name) + name.length) + '/' + version;
7+
const unversionedDocBaseUri = href.substring(0, href.indexOf(name) + name.length) + '/';
8+
const commonDocsBaseUri = href.substring(0, href.indexOf(name) - 1);
9+
10+
const pathname = window.location.pathname;
11+
let neo4jPageId;
12+
if (pathname.indexOf(name) > -1) {
13+
const baseUri = unversionedDocBaseUri + pathname.split(name + '/')[1].split('/')[0] + '/';
14+
neo4jPageId = href.replace(baseUri, '');
15+
}
16+
17+
const versionsUrl =
18+
'https://s3-eu-west-1.amazonaws.com/com.neo4j.graphalgorithms.dist/graph-data-science/doc-versions.json';
19+
const availableDocVersions = await jQuery.ajax({
20+
type: 'GET',
21+
url: versionsUrl,
22+
headers: {
23+
'Content-Type': 'application/json',
24+
Accept: 'application/vnd.github.v3+json'
25+
},
26+
crossOrigin: true,
27+
success: function (data) {
28+
return data;
29+
},
30+
error: function (jqXHR, textStatus, errorThrown) {
31+
console.error('Request failed...', jqXHR, textStatus, errorThrown);
32+
}
33+
});
34+
535
return {
636
name: name,
737
version: version,
8-
availableDocVersions: ['1.0', '1.1', '1.2', '1.3'],
9-
thisPubBaseUri: href.substring(0, href.indexOf(name) + name.length) + '/' + version,
10-
unversionedDocBaseUri: href.substring(0, href.indexOf(name) + name.length) + '/',
11-
commonDocsBaseUri: href.substring(0, href.indexOf(name) - 1)
38+
availableDocVersions: availableDocVersions,
39+
thisPubBaseUri: thisPubBaseUri,
40+
unversionedDocBaseUri: unversionedDocBaseUri,
41+
commonDocsBaseUri: commonDocsBaseUri,
42+
neo4jPageId: neo4jPageId
1243
}
1344
})();
14-
15-
(function () {
16-
var baseUri = window.docMeta.unversionedDocBaseUri + window.location.pathname.split(window.docMeta.name + '/')[1].split('/')[0] + '/';
17-
var docPath = window.location.href.replace(baseUri, '');
18-
window.neo4jPageId = docPath;
19-
})();
2045
// vim: set sw=2 ts=2:

doc/javascript/versionswitcher.js

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
jQuery( window ).load( function() {
2-
var location = window.location;
3-
versionSwitcher( jQuery );
4-
} );
1+
jQuery(window).load(function () {
2+
versionSwitcher(jQuery).then(function() {});
3+
});
54

65
/**
76
* Utility to browse different versions of the documentation. Requires the versions.js file loaded, which lists the
87
* available (relevant) versions of a particular publication.
98
*/
10-
function versionSwitcher( $ )
11-
{
12-
var MAX_STABLE_COUNT = 2;
13-
var DOCS_BASE_URL = window.docMeta.commonDocsBaseUri;
14-
var THIS_DOC_BASE_URI = window.docMeta.unversionedDocBaseUri;
15-
16-
var currentVersion = window.docMeta.version;
17-
var currentPage = window.neo4jPageId;
9+
async function versionSwitcher($) {
10+
const docMeta = await window.docMeta;
11+
const THIS_DOC_BASE_URI = docMeta.unversionedDocBaseUri;
12+
const currentPage = docMeta.neo4jPageId;
1813

1914
loadVersions();
2015

@@ -23,56 +18,59 @@ function versionSwitcher( $ )
2318
* Non-existing entries will be unlinked. Current version will be marked as such.
2419
*/
2520
function loadVersions() {
26-
var $navHeader = $( 'header' );
27-
var $additionalVersions = $( '<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1"/>' );
28-
var versionCount = 0;
29-
$.each( window.docMeta.availableDocVersions, function( index, version ) {
30-
if ( version === currentVersion ) {
31-
return;
32-
}
33-
else {
34-
addVersion( version, $additionalVersions );
21+
const $navHeader = $('header');
22+
const $additionalVersions = $('<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1"/>');
23+
let versionCount = 0;
24+
let currentVersion = docMeta.version
25+
const availableVersions = docMeta.availableDocVersions;
26+
console.log('availableVersions', availableVersions)
27+
availableVersions.forEach(function (version) {
28+
if (version !== currentVersion) {
29+
addVersion(version, $additionalVersions);
3530
versionCount++;
3631
}
37-
} );
38-
if ( versionCount == 0){
39-
var $dropdown = $( '<div id="additional-versions"><div class="additional-versions">Version: ' + currentVersion + ' </div></div>' );
32+
});
33+
34+
let $dropdown;
35+
if (versionCount === 0) {
36+
$dropdown = $('<div id="additional-versions"><div class="additional-versions">Version: ' + currentVersion + ' </div></div>');
4037
} else {
41-
var $dropdown = $( '<div id="additional-versions"><div class="dropdown"><a class="dropdown-toggle"id="dropdownMenu1" data-toggle="dropdown">Version: ' + currentVersion + ' <i class="fa fa-caret-down"></i></a></div></div>' );
42-
$dropdown.children().first().append( $additionalVersions );
38+
$dropdown = $('<div id="additional-versions"><div class="dropdown"><a class="dropdown-toggle"id="dropdownMenu1" data-toggle="dropdown">Version: ' + currentVersion + ' <i class="fa fa-caret-down"></i></a></div></div>');
39+
$dropdown.children().first().append($additionalVersions);
4340
}
44-
$navHeader.append( $dropdown );
41+
$navHeader.append($dropdown);
4542
}
4643

47-
function addVersion( version, $container ) {
48-
var $optionWrapper = $( '<li />' );
49-
var $newOption = $( '<a role="menuitem">' + version + '</a>' ).appendTo( $optionWrapper );
44+
function addVersion(version, $container) {
45+
var $optionWrapper = $('<li />');
46+
var $newOption = $('<a role="menuitem">' + version + '</a>').appendTo($optionWrapper);
5047
var url = THIS_DOC_BASE_URI + version + '/' + currentPage;
51-
$container.append( $optionWrapper );
52-
checkUrlExistence( url, function() {
53-
$newOption.attr( 'href', url );
54-
$newOption.attr( 'title', 'See this page in version ' + version + '.' );
55-
}, function() {
56-
$newOption.attr( 'title', 'This page does not exist in version ' + version + '.' );
57-
$optionWrapper.addClass( 'disabled' );
48+
$container.append($optionWrapper);
49+
checkUrlExistence(url, function () {
50+
$newOption.attr('href', url);
51+
$newOption.attr('title', 'See this page in version ' + version + '.');
52+
}, function () {
53+
$newOption.attr('title', 'This page does not exist in version ' + version + '.');
54+
$optionWrapper.addClass('disabled');
5855
}
5956
);
6057
}
6158

6259
/**
6360
* Check if a specific URL exists. The success and failure functions will be automatically called on finish.
6461
*/
65-
function checkUrlExistence( url, success, failure ) {
62+
function checkUrlExistence(url, success, failure) {
6663
var settings = {
67-
'type' : 'HEAD',
68-
'async' : true,
69-
'url' : url
64+
'type': 'HEAD',
65+
'async': true,
66+
'url': url
7067
};
71-
if ( success )
68+
if (success)
7269
settings.success = success;
73-
if ( failure )
70+
if (failure)
7471
settings.error = failure;
75-
$.ajax( settings );
72+
$.ajax(settings);
7673
}
7774
}
75+
7876
// vim: set ts=2 sw=2:

packaging/build.gradle

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ task versionsJson {
4848
}
4949
}
5050

51+
task docsVersionsJson {
52+
ext.outFile = file("$distributionDir/doc-versions.json")
53+
def versionsJsonUrl = new URL("https://s3-eu-west-1.amazonaws.com/com.neo4j.graphalgorithms.dist/graph-data-science/doc-versions.json")
54+
outputs.file(outFile)
55+
doLast {
56+
def versionsJson = new groovy.json.JsonSlurper().parse(versionsJsonUrl)
57+
def gdsVersion = project.version
58+
// True if the project version doesn't contain `-alpha0x`
59+
def isGa = gdsVersion.toString().matches("^(\\d+\\.){2}\\d+\$")
60+
61+
// Derive the Docs version from the GDS Version -> i.e.
62+
// GDS Version: `1.4.0-alpha01` -> `1.4-preview`
63+
// GDS Version: `1.4.1` -> `1.4`
64+
def docsVersion = (gdsVersion =~ /(\d+)\.(\d+)/).findAll()*.first()[0]
65+
if(!isGa) {
66+
docsVersion += '-preview'
67+
}
68+
69+
// Find the index of the element that starts with our new docs version
70+
// Using `startsWith` to catch new version being GA and the old one Preview
71+
def idx = versionsJson.findIndexOf { v -> v.startsWith(docsVersion)}
72+
if(idx >= 0) {
73+
// Replace the old version it if exists
74+
versionsJson[idx] = docsVersion
75+
} else {
76+
// Add the new docs version if it doesn't exist
77+
versionsJson.add(docsVersion)
78+
}
79+
80+
// Save the results to a file
81+
outFile.text = groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(versionsJson))
82+
}
83+
}
84+
5185
task distZip(type: Zip) {
5286
description 'Create a ZIP archive of the main distribution JAR.'
5387
archiveBaseName = shadowJar.archiveBaseName
@@ -56,5 +90,5 @@ task distZip(type: Zip) {
5690
destinationDirectory.set(file(distributionDir))
5791
}
5892

59-
ci.finalizedBy shadowCopy, versionsJson, distZip
60-
cipr.finalizedBy shadowCopy, versionsJson, distZip
93+
ci.finalizedBy shadowCopy, versionsJson, docsVersionsJson, distZip
94+
cipr.finalizedBy shadowCopy, versionsJson, docsVersionsJson, distZip

0 commit comments

Comments
 (0)