(function() {
  // clipboard
  var clipInit = false;
  $("pre code").each(function() {
    var code = $(this), text = code.text();

    if (text.length > 5) {
      if (!clipInit) {
        var text,
          clip = new Clipboard(".copy-to-clipboard", {
            text: function(trigger) {
              text = $(trigger).prev("code").text();
              return text.replace(/^\$\s/gm, "");
            }
          });

        var inPre;
        clip.on("success", function(e) {
          e.clearSelection();
          inPre = $(e.trigger).parent().prop("tagName") == "PRE";
          $(e.trigger)
            .attr("aria-label", "Copied to clipboard!")
            .addClass("tooltipped tooltipped-" + (inPre ? "w" : "s"));
        });

        clip.on("error", function(e) {
          inPre = $(e.trigger).parent().prop("tagName") == "PRE";
          $(e.trigger)
            .attr("aria-label", fallbackMessage(e.action))
            .addClass("tooltipped tooltipped-" + (inPre ? "w" : "s"));
          $(document).one("copy", function() {
            $(e.trigger)
              .attr("aria-label", "Copied to clipboard!")
              .addClass("tooltipped tooltipped-" + (inPre ? "w" : "s"));
          });
        });

        clipInit = true;
      }

      code.after('<span class="copy-to-clipboard">Copy</span>');
      code.next(".copy-to-clipboard").on("mouseleave", function() {
        $(this)
          .attr("aria-label", null)
          .removeClass("tooltipped tooltipped-s tooltipped-w");
      });
    }
  });

  // Sidebar
  var h2s = document.querySelectorAll("h2");
  var h3s = document.querySelectorAll("h3");
  var isAfter = function(e1, e2) {
    return e1.compareDocumentPosition(e2) & Node.DOCUMENT_POSITION_FOLLOWING;
  };
  var activeLink = document.querySelector(".doc-toc.active");

  // TODO: h2sWithH3s
  var h2sWithH3s = [];
  var j = 0;
  for (var i = 0; i < h2s.length; i++) {
    var h2 = h2s[i];
    var nextH2 = h2s[i + 1];
    var ourH3s = [];
    while (
      h3s[j] && isAfter(h2, h3s[j]) && (!nextH2 || !isAfter(nextH2, h3s[j]))
    ) {
      ourH3s.push({ header: h3s[j] });
      j++;
    }

    h2sWithH3s.push({
      header: h2,
      subHeaders: ourH3s
    });
  }

  if (h2sWithH3s.length > 0) {
    createSubtopic(activeLink, h2sWithH3s);
  }

  function createSubtopic(container, headers) {
    var subMenu = document.createElement("ul");
    subMenu.className = "sub-topic";
    container.appendChild(subMenu);

    Array.prototype.forEach.call(headers, function(h) {
      var li = createSubtopicItem(h.header);
      subMenu.appendChild(li);
    });
  }

  function createSubtopicItem(h) {
    var li = document.createElement("li");
    li.innerHTML = '<a href="#' +
      h.id +
      '" data-scroll class="' +
      h.tagName +
      '"><span>' +
      (h.title || h.textContent) +
      "</span></a>";
    return li;
  }

  // code collapse
  var pres = document.getElementsByTagName("pre");
  Array.prototype.forEach.call(pres, function(pre) {
    if (pre.clientHeight > 500) {
      pre.className += " collapsed";

      var showMore = document.createElement("div");
      showMore.className = "showmore";
      showMore.innerHTML = "Show full code";
      showMore.addEventListener("click", function() {
        pre.className = "";
        showMore.parentNode.removeChild(showMore);
      });

      pre.appendChild(showMore);
    }
  });


  // version selector
  var currentVersion = location.pathname.split("/")[1];
  document
    .getElementsByClassName("version-selector")[0]
    .addEventListener("change", function(e) {
      var targetVersion = e.target.value;

      if (currentVersion !== targetVersion) {
        // Getting everything after targetVersion and concatenating it with the hash part.
        var targetPath = "/" +
          targetVersion +
          "/" +
          location.pathname.split("/").slice(2).join("/") +
          location.hash;
        location.assign(targetPath);
      }
    });

  // Add target = _blank to all external links.
  var links = document.links;

  for (var i = 0, linksLength = links.length; i < linksLength; i++) {
     if (links[i].hostname != window.location.hostname) {
         links[i].target = '_blank';
     }
  }
})();

@@ -3403,17 +3403,16 @@
 %0A  %7D);%0A%0A
-%0A
   // ver
@@ -3977,24 +3977,473 @@
  %7D%0A    %7D);%0A%0A
+  var versionSelector = document.getElementsByClassName(%22version-selector%22)%5B0%5D,%0A    options = versionSelector.options;%0A%0A  for (var i = 0; i %3C options.length; i++) %7B%0A    if (options%5Bi%5D.value.indexOf(%22latest%22) != -1) %7B%0A      options%5Bi%5D.value = options%5Bi%5D.value.replace(/%5Cs%5C(latest%5C)/, %22%22);%0A    %7D%0A  %7D%0A%0A  for (var i = 0; i %3C options.length; i++) %7B%0A    if (options%5Bi%5D.value === currentVersion) %7B%0A      options%5Bi%5D.selected = true;%0A      break;%0A    %7D%0A  %7D%0A%0A
   // Add tar
@@ -4579,25 +4579,24 @@
  i++) %7B%0A    
- 
 if (links%5Bi%5D
@@ -4642,19 +4642,16 @@
 %7B%0A      
-   
 links%5Bi%5D
@@ -4664,19 +4664,18 @@
 t = 
-'
+%22
 _blank
-'
+%22
 ;%0A
- 
     

Add back code to select correct version in dgraph.js