docsify-init.js 7.4 KB

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