404.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function () {
  2. // regexes as strings
  3. // NOTE:
  4. // these patterns should NOT contain capturing groups (parentheses)
  5. // because they might be combined with other regexes
  6. var DOCS_VERSION_PATTERN = "(?:[\\d\\.]+x?|dev|latest)";
  7. var DOCS_LANGUAGE_PATTERN = "\\w\\w(?:-\\w\\w)?";
  8. var DOCS_VERSION_PATH_PATTERN = "docs\\/" + DOCS_LANGUAGE_PATTERN + "\\/" + DOCS_VERSION_PATTERN;
  9. function splitDocsURL(url) {
  10. var splitPattern = new RegExp("(" + DOCS_VERSION_PATH_PATTERN + ")");
  11. return url.split(splitPattern);
  12. }
  13. function transformOldURL(oldURL) {
  14. var splitURL = splitDocsURL(oldURL);
  15. var baseURL = splitURL[0];
  16. var versionString = splitURL[1];
  17. var pageExtension = splitURL.slice(2).join("");
  18. // get rid of the fragment and replace underscores with slashes
  19. // also remove the ".md" extension
  20. pageExtension = pageExtension.split("#")[0];
  21. pageExtension = pageExtension.replace(".md", "");
  22. pageExtension = pageExtension.replace(/_/g, "/");
  23. // undo special cases of paths that really do contain underscores
  24. // NOTE:
  25. // this list is complete; in the pre-migration docs, no other
  26. // directories or files contained underscores
  27. pageExtension = pageExtension.replace("config/ref", "config_ref");
  28. pageExtension = pageExtension.replace("plugin/ref", "plugin_ref");
  29. pageExtension = pageExtension.replace("display/name", "display_name");
  30. pageExtension = pageExtension.replace("platform/plugin/versioning/ref", "platform_plugin_versioning_ref");
  31. return baseURL + versionString + pageExtension;
  32. }
  33. function getDevURL(url) {
  34. var replaceWhat = new RegExp("docs\\/(" + DOCS_LANGUAGE_PATTERN + ")\\/" + DOCS_VERSION_PATTERN);
  35. var replaceWith = "docs/$1/dev";
  36. return url.replace(replaceWhat, replaceWith);
  37. }
  38. function getRootURL(url) {
  39. var splitURL = splitDocsURL(url);
  40. var baseURL = splitURL[0];
  41. var versionString = splitURL[1];
  42. return baseURL + versionString + "/";
  43. }
  44. function isDocsURL(url) {
  45. var testPattern = new RegExp(DOCS_VERSION_PATH_PATTERN);
  46. return testPattern.test(url);
  47. }
  48. function showNewRedirect(url) {
  49. $("#new-redirect-link").attr("href", url);
  50. $("#not-found-redirect-alert").css("display", "block");
  51. }
  52. function showDevRedirect(url) {
  53. $("#dev-redirect-link").attr("href", url);
  54. $("#no-version-redirect-alert").css("display", "block");
  55. }
  56. function showRootRedirect(url) {
  57. $("#root-redirect-link").attr("href", url);
  58. $("#no-page-redirect-alert").css("display", "block");
  59. }
  60. function main() {
  61. var url = window.location.href;
  62. if (!isDocsURL(url)) {
  63. return;
  64. }
  65. // get related URLs
  66. var rootURL = getRootURL(url);
  67. var devURL = getDevURL(url);
  68. var newURL = transformOldURL(url);
  69. // try the root URL
  70. $.ajax({
  71. url: rootURL,
  72. statusCode: {
  73. // if the root exists, check if the new URL exists
  74. 200: function () {
  75. $.ajax({
  76. url: newURL,
  77. statusCode: {
  78. // redirect to the new URL if it exists
  79. 200: function () {
  80. showNewRedirect(newURL)
  81. },
  82. // if the new URL doesn't exist, redirect to root
  83. 404: function() {
  84. showRootRedirect(rootURL)
  85. }
  86. }
  87. });
  88. },
  89. // if root doesn't exist, assume that the version
  90. // is invalid and redirect to dev
  91. 404: function () {
  92. showDevRedirect(devURL)
  93. }
  94. }
  95. });
  96. }
  97. main();
  98. }());