|
| 1 | +/** |
| 2 | + * @fileoverview This file executes on load and is meant to check if the help |
| 3 | + * page is the latest version and inject warnings into the page if not. |
| 4 | + * TODO: Need to use GitHub API to retrieve latest version published |
| 5 | + */ |
| 6 | + |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +(function() { |
| 10 | + // RegEx in semverRegex() function © Sindre Sorhus, MIT licensed |
| 11 | + // From https://github.com/sindresorhus/semver-regex |
| 12 | + var semverRegex = function () { |
| 13 | + return /\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b/ig; |
| 14 | + }; |
| 15 | + |
| 16 | + var semverNumbersOnly = function(verStr) { |
| 17 | + // Simple rules here, remove prepended 'v' and appended '-identifier' |
| 18 | + var verArray = verStr.split('.'); |
| 19 | + if (verArray[0].startsWith('v')) { |
| 20 | + verArray[0] = verArray[0].substring(1); |
| 21 | + } |
| 22 | + verArray[2] = verArray[2].split('-')[0]; |
| 23 | + return verArray.join('.'); |
| 24 | + }; |
| 25 | + |
| 26 | + var getUrlVersion = function() { |
| 27 | + var urlPath = window.location.pathname.split('/'); |
| 28 | + for (var i = 0; i < urlPath.length; i++) { |
| 29 | + if (semverRegex().test(urlPath[i])) { |
| 30 | + return semverNumbersOnly(semverRegex().exec(urlPath[i])[0]); |
| 31 | + } |
| 32 | + } |
| 33 | + return null; |
| 34 | + }; |
| 35 | + |
| 36 | + var getLatestReleaseVersion = function() { |
| 37 | + // TODO: This is a hard-coded version string, need to use GH API instead |
| 38 | + var version = "0.9.13"; |
| 39 | + return semverRegex().test(version) ? semverRegex().exec(version)[0] : null; |
| 40 | + }; |
| 41 | + |
| 42 | + var isVersionLowerThan = function(baseVersion, compareVersion) { |
| 43 | + // Version strings checked before, we trust they are formatted correctly |
| 44 | + var verBase = baseVersion.split('.'); |
| 45 | + var verCompare = compareVersion.split('.'); |
| 46 | + return (verBase[0] < verCompare[0]) || (verBase[1] < verCompare[1]) || |
| 47 | + (verBase[2] < verCompare[2]); |
| 48 | + }; |
| 49 | + |
| 50 | + window.addEventListener('load', function load(event) { |
| 51 | + window.removeEventListener('load', load, false); |
| 52 | + var helpInjectDiv = document.getElementById('mu-help-header-inject'); |
| 53 | + if (helpInjectDiv) { |
| 54 | + var urlVersion = getUrlVersion(); |
| 55 | + var latestVersion = getLatestReleaseVersion(); |
| 56 | + if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) { |
| 57 | + helpInjectDiv.innerHTML = [ |
| 58 | + '<div class="alert alert-danger" role="alert">', |
| 59 | + ' <h2><strong>Update Mu!</strong></h2>', |
| 60 | + ' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version</a>.</p>', |
| 61 | + '</div>', |
| 62 | + '<div role="alert" class="alert alert-warning">', |
| 63 | + ' <h2><strong>This is not the latest Documentation!</strong></h2>', |
| 64 | + ' <p>This documentation corresponds to the Mu version you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>', |
| 65 | + ' <p><a href="/help">View documentation for newest versions of Mu.</a></p>', |
| 66 | + '</div>'].join('\n'); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | +})(); |
0 commit comments