docsify-init.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. if (window.DOCSIFY_ROUTER_MODE === "history") {
  2. var clink = document.createElement("link")
  3. clink.rel = "canonical"
  4. document.getElementsByTagName("head")[0].appendChild(clink)
  5. }
  6. var documentTitleBase = document.title;
  7. var linksMenu = '<div class="links-menu">' +
  8. '<a href="https://imgproxy.net" target="_blank" title="Website"><img src="/assets/website.svg" /></a>' +
  9. '<a href="https://github/imgproxy" target="_blank" title="GitHub"><img src="/assets/github.svg" /></a>' +
  10. '<a href="https://twitter.com/imgproxy_net" target="_blank" title="Twitter"><img src="/assets/twitter.svg" /></a>' +
  11. '<a href="https://discord.gg/5GgpXgtC9u" target="_blank" title="Discord"><img src="/assets/discord.svg" /></a>' +
  12. '</div>';
  13. var docEditBase = 'https://github.com/imgproxy/imgproxy/edit/master/docs/';
  14. var proBadge = document.createElement("img")
  15. proBadge.setAttribute("src", "/assets/pro.svg")
  16. proBadge.setAttribute("title", "This feature is available in imgproxy Pro")
  17. var proBadgeRegex = /\!\[pro\]\((\S+)\)/g;
  18. var proLink = '<a class="badge" href="https://imgproxy.net/#pro" target="_blank">' +
  19. proBadge.outerHTML + '</a>';
  20. var oldProBadge = "<i class='badge badge-pro'></i>";
  21. var versions = ["latest"].concat(window.IMGPROXY_VERSIONS);
  22. var latestVersion = window.IMGPROXY_VERSIONS[0];
  23. var versionSelect = '<select id="version-selector" name="version" class="sidebar-version-select">';
  24. versions.forEach(function (version) {
  25. var value = version == latestVersion ? "" : version;
  26. versionSelect = versionSelect + '<option value="' + value + '">' + version + '</value>';
  27. });
  28. versionSelect = versionSelect + '</select>';
  29. window.$docsify = {
  30. name: '<a id="home-link" class="app-name-link" href="/"><img src="/assets/logo.svg"></a>' +
  31. linksMenu +
  32. versionSelect,
  33. nameLink: false,
  34. loadSidebar: true,
  35. relativePath: true,
  36. subMaxLevel: 3,
  37. auto2top: true,
  38. routerMode: window.DOCSIFY_ROUTER_MODE || "hash",
  39. noEmoji: true,
  40. alias: {
  41. '/latest/': 'README.md',
  42. '/latest/(.*)': '$1',
  43. '/([0-9]+\.[0-9]+)/(.*)': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v$1.0/docs/$2',
  44. '/([0-9]+\.[0-9]+)/': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v$1.0/docs/README.md',
  45. '/(.*)': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v' + latestVersion + '.0/docs/$1',
  46. '/': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v' + latestVersion + '.0/docs/README.md',
  47. },
  48. search: {
  49. namespace: 'docs-imgproxy',
  50. depth: 6,
  51. pathNamespaces: versions.map(function (v) { return "/" + v })
  52. },
  53. namespaces: [
  54. {
  55. id: "version",
  56. values: versions,
  57. optional: true,
  58. selector: "#version-selector"
  59. }
  60. ],
  61. plugins: [
  62. function (hook, vm) {
  63. window.DocsifyVM = vm
  64. hook.beforeEach(function () {
  65. if (clink) {
  66. clink.href = "https://docs.imgproxy.net" + vm.route.path
  67. }
  68. });
  69. hook.doneEach(function () {
  70. var appNameLink = Docsify.dom.find("#home-link");
  71. if (!appNameLink) return;
  72. appNameLink.href = vm.config.currentNamespace;
  73. });
  74. hook.doneEach(function() {
  75. if (document.title != documentTitleBase)
  76. document.title += " | " + documentTitleBase;
  77. });
  78. hook.afterEach(function (html) {
  79. var docName = vm.route.file.replace(
  80. /https\:\/\/raw.githubusercontent\.com\/(.*)\/docs\//, ''
  81. )
  82. if (!docName) {
  83. return html;
  84. }
  85. var editButton = '<a class="github-edit-btn" title="Edit on GitHub" href="' +
  86. docEditBase + docName +
  87. '" target="_blank">' +
  88. 'Edit on <strong>GitHub</strong>' +
  89. '</a>';
  90. return html + editButton
  91. })
  92. hook.beforeEach(function (content, next) {
  93. content = content.replaceAll(proBadgeRegex, proLink)
  94. content = content.replaceAll(oldProBadge, proLink)
  95. next(content)
  96. })
  97. hook.doneEach(function () {
  98. var badges = Docsify.dom.findAll(".sidebar .badge-pro")
  99. badges.forEach(function (b) { b.replaceWith(proBadge.cloneNode()) })
  100. // Docsify cuts off "target" sometimes
  101. var links = Docsify.dom.findAll("a.badge")
  102. links.forEach(function(l){ l.setAttribute("target", "_blank") })
  103. })
  104. }
  105. ]
  106. }