docsify-init.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. let clink = null;
  2. if (window.DOCSIFY_ROUTER_MODE === "history") {
  3. clink = Docsify.dom.create("link");
  4. clink.rel = "canonical";
  5. Docsify.dom.appendTo(Docsify.dom.head, clink);
  6. }
  7. const documentTitleBase = document.title;
  8. const linksMenu = '<div class="links-menu">' +
  9. '<a href="https://imgproxy.net" target="_blank" title="Website"><img src="/assets/website.svg" /></a>' +
  10. '<a href="https://github.com/imgproxy" target="_blank" title="GitHub"><img src="/assets/github.svg" /></a>' +
  11. '<a href="https://twitter.com/imgproxy_net" target="_blank" title="Twitter"><img src="/assets/twitter.svg" /></a>' +
  12. '<a href="https://discord.gg/5GgpXgtC9u" target="_blank" title="Discord"><img src="/assets/discord.svg" /></a>' +
  13. '<a href="https://github.com/sponsors/imgproxy" target="_blank" title="Sponsor"><img src="/assets/heart.svg" /></a>' +
  14. '</div>';
  15. const docEditBase = 'https://github.com/imgproxy/imgproxy/edit/master/docs/';
  16. const proBadge = Docsify.dom.create("img");
  17. proBadge.setAttribute("src", "/assets/pro.svg");
  18. proBadge.setAttribute("title", "This feature is available in imgproxy Pro");
  19. const proBadgeRegex = /\!\[pro\]\((\S+)\)/g;
  20. const proLink = `<a class="badge" href="https://imgproxy.net/#pro" target="_blank">${proBadge.outerHTML}</a>`;
  21. const oldProBadge = "<i class='badge badge-pro'></i>";
  22. const configRegex = /^\* `([^`]+)`:/gm;
  23. const defaultVersions = [["latest", "latest"]];
  24. const configureDocsify = (additionalVersions, latestVersion, latestTag) => {
  25. const versions = defaultVersions.concat(additionalVersions);
  26. const versionAliases = {};
  27. const versionSelect = ['<div class="sidebar-version-select"><select id="version-selector" name="version">'];
  28. versions.forEach(([version, tag]) => {
  29. const value = version == latestVersion ? "" : version;
  30. versionSelect.push(`<option value="${value}">${version}</value>`);
  31. if (version !== "latest") {
  32. versionAliases[`/${version}/(.*)`] =
  33. `https://raw.githubusercontent.com/imgproxy/imgproxy/${tag}/docs/$1`;
  34. versionAliases[`/${version}/`] =
  35. `https://raw.githubusercontent.com/imgproxy/imgproxy/${tag}/docs/README.md`;
  36. }
  37. });
  38. versionSelect.push('</select></div>');
  39. if (latestTag === "latest") latestTag = "master";
  40. window.$docsify = {
  41. name: '<a id="home-link" class="app-name-link" href="/"><img src="/assets/logo.svg"></a>' +
  42. linksMenu +
  43. versionSelect.join(""),
  44. nameLink: false,
  45. loadSidebar: true,
  46. relativePath: true,
  47. subMaxLevel: 3,
  48. auto2top: true,
  49. routerMode: window.DOCSIFY_ROUTER_MODE || "hash",
  50. noEmoji: true,
  51. alias: Object.assign(versionAliases, {
  52. '/latest/': 'README.md',
  53. '/latest/(.*)': '$1',
  54. '/([0-9]+\.[0-9]+)/(.*)': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v$1.0/docs/$2',
  55. '/([0-9]+\.[0-9]+)/': 'https://raw.githubusercontent.com/imgproxy/imgproxy/v$1.0/docs/README.md',
  56. '/(.*)': `https://raw.githubusercontent.com/imgproxy/imgproxy/${latestTag}/docs/$1`,
  57. '/': `https://raw.githubusercontent.com/imgproxy/imgproxy/${latestTag}/docs/README.md`,
  58. }),
  59. search: {
  60. namespace: 'docs-imgproxy',
  61. depth: 6,
  62. // pathNamespaces: versions.map(v => "/" + v[0]),
  63. pathNamespaces: /^(\/(latest|([0-9]+\.[0-9]+)))?/,
  64. },
  65. namespaces: [
  66. {
  67. id: "version",
  68. values: versions.map(v => v[0]),
  69. optional: true,
  70. selector: "#version-selector",
  71. }
  72. ],
  73. plugins: window.$docsify.plugins.concat([
  74. (hook, vm) => {
  75. window.DocsifyVM = vm;
  76. hook.beforeEach(() => {
  77. if (clink)
  78. clink.href = "https://docs.imgproxy.net" + vm.route.path;
  79. });
  80. hook.doneEach(() => {
  81. const appNameLink = Docsify.dom.find("#home-link");
  82. if (!appNameLink) return;
  83. appNameLink.href = vm.config.currentNamespace;
  84. });
  85. hook.doneEach(() => {
  86. if (document.title != documentTitleBase)
  87. document.title += " | " + documentTitleBase;
  88. });
  89. hook.afterEach(html => {
  90. const docName = vm.route.file.replace(
  91. /https\:\/\/raw.githubusercontent\.com\/(.*)\/docs\//, ''
  92. );
  93. if (!docName) return html;
  94. const editButton = '<a class="github-edit-btn" title="Edit on GitHub" href="' +
  95. docEditBase + docName +
  96. '" target="_blank">' +
  97. 'Edit on <strong>GitHub</strong>' +
  98. '</a>';
  99. return html + editButton;
  100. })
  101. hook.beforeEach((content, next) => {
  102. content = content
  103. .replaceAll(proBadgeRegex, proLink)
  104. .replaceAll(oldProBadge, proLink);
  105. content = content
  106. .replaceAll("📝", '<i class="icon icon-note"></i>')
  107. .replaceAll("⚠️", '<i class="icon icon-warn"></i>')
  108. .replaceAll("✅", '<i class="icon icon-check"></i>')
  109. .replaceAll("❌", '<i class="icon icon-cross"></i>');
  110. if (vm.route.path.endsWith('/configuration'))
  111. content = content.replaceAll(configRegex, '* <code id="$1">$1</code>:');
  112. next(content);
  113. })
  114. hook.doneEach(() => {
  115. const badges = Docsify.dom.findAll(".sidebar .badge-pro");
  116. badges.forEach(b => { b.replaceWith(proBadge.cloneNode()) });
  117. // Docsify cuts off "target" sometimes
  118. const links = Docsify.dom.findAll("a.badge");
  119. links.forEach(l => { l.setAttribute("target", "_blank") });
  120. })
  121. }
  122. ])
  123. }
  124. }
  125. const initDocsify = (versions, latestVersion, latestTag) => {
  126. configureDocsify(versions, latestVersion, latestTag);
  127. window.runDocsify();
  128. };
  129. const VERSIONS_KEY = "imgproxy.versions";
  130. const VERSIONS_ETAG_KEY = "imgproxy.versions.etag";
  131. let latestVersion = "latest";
  132. let latestTag = "latest";
  133. let storedVersions = [];
  134. let storedVersionsJson = localStorage.getItem(VERSIONS_KEY);
  135. let storedVersionsEtag = localStorage.getItem(VERSIONS_ETAG_KEY);
  136. if (storedVersionsJson) {
  137. try {
  138. storedVersions = JSON.parse(storedVersionsJson);
  139. } catch {
  140. storedVersions = [];
  141. }
  142. }
  143. if (storedVersions?.length)
  144. [latestVersion, latestTag] = storedVersions[0];
  145. else {
  146. // Just in case storedVersions is not an array
  147. storedVersions = [];
  148. storedVersionsEtag = null;
  149. }
  150. fetch(
  151. "https://api.github.com/repos/imgproxy/imgproxy/releases",
  152. {
  153. headers: {
  154. "Accept": "application/json",
  155. "If-None-Match": storedVersionsEtag,
  156. },
  157. },
  158. ).then((resp) => {
  159. if (resp.status === 304) {
  160. initDocsify(storedVersions, latestVersion, latestTag);
  161. return;
  162. }
  163. if (resp.status != 200)
  164. throw new Error(`Can't fetch imgproxy versions: ${resp.statusText}`);
  165. resp.json().then((data) => {
  166. const uniq = {};
  167. const fetchedVersions = [];
  168. data.forEach((release) => {
  169. if (release.draft || release.prerelease) return;
  170. var tag = release.tag_name;
  171. var matches = tag?.match(/^v([0-9]+\.[0-9]+)/);
  172. if (!matches?.length) return;
  173. var version = matches[1];
  174. if (uniq[version]) return;
  175. fetchedVersions.push([version, tag]);
  176. uniq[version] = true;
  177. });
  178. if (fetchedVersions.length)
  179. [latestVersion, latestTag] = fetchedVersions[0];
  180. localStorage.setItem(VERSIONS_KEY, JSON.stringify(fetchedVersions));
  181. localStorage.setItem(VERSIONS_ETAG_KEY, resp.headers.get("Etag"));
  182. initDocsify(fetchedVersions, latestVersion, latestTag);
  183. }).catch((e) => {
  184. initDocsify(storedVersions, latestVersion, latestTag);
  185. throw e;
  186. });
  187. }).catch((e) => {
  188. initDocsify(storedVersions, latestVersion, latestTag);
  189. throw e;
  190. });