router.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. app.Router = class Router extends Events {
  2. static routes = [
  3. ["*", "before"],
  4. ["/", "root"],
  5. ["/settings", "settings"],
  6. ["/offline", "offline"],
  7. ["/about", "about"],
  8. ["/news", "news"],
  9. ["/help", "help"],
  10. ["/:doc-:type/", "type"],
  11. ["/:doc/", "doc"],
  12. ["/:doc/:path(*)", "entry"],
  13. ["*", "notFound"],
  14. ];
  15. constructor() {
  16. super();
  17. for (var [path, method] of this.constructor.routes) {
  18. page(path, this[method].bind(this));
  19. }
  20. this.setInitialPath();
  21. }
  22. start() {
  23. page.start();
  24. }
  25. show(path) {
  26. page.show(path);
  27. }
  28. triggerRoute(name) {
  29. this.trigger(name, this.context);
  30. this.trigger("after", name, this.context);
  31. }
  32. before(context, next) {
  33. let res;
  34. const previousContext = this.context;
  35. this.context = context;
  36. this.trigger("before", context);
  37. if ((res = next())) {
  38. this.context = previousContext;
  39. return res;
  40. } else {
  41. return;
  42. }
  43. }
  44. doc(context, next) {
  45. let doc;
  46. if (
  47. (doc =
  48. app.docs.findBySlug(context.params.doc) ||
  49. app.disabledDocs.findBySlug(context.params.doc))
  50. ) {
  51. context.doc = doc;
  52. context.entry = doc.toEntry();
  53. this.triggerRoute("entry");
  54. return;
  55. } else {
  56. return next();
  57. }
  58. }
  59. type(context, next) {
  60. const doc = app.docs.findBySlug(context.params.doc);
  61. const type = doc?.types?.findBy("slug", context.params.type);
  62. if (type) {
  63. context.doc = doc;
  64. context.type = type;
  65. this.triggerRoute("type");
  66. return;
  67. } else {
  68. return next();
  69. }
  70. }
  71. entry(context, next) {
  72. let entry;
  73. const doc = app.docs.findBySlug(context.params.doc);
  74. if (!doc) {
  75. return next();
  76. }
  77. let { path } = context.params;
  78. const { hash } = context;
  79. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  80. context.doc = doc;
  81. context.entry = entry;
  82. this.triggerRoute("entry");
  83. return;
  84. } else if (path.slice(-6) === "/index") {
  85. path = path.substr(0, path.length - 6);
  86. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  87. return entry.fullPath();
  88. }
  89. } else {
  90. path = `${path}/index`;
  91. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  92. return entry.fullPath();
  93. }
  94. }
  95. return next();
  96. }
  97. root() {
  98. if (app.isSingleDoc()) {
  99. return "/";
  100. }
  101. this.triggerRoute("root");
  102. }
  103. settings(context) {
  104. if (app.isSingleDoc()) {
  105. return `/#/${context.path}`;
  106. }
  107. this.triggerRoute("settings");
  108. }
  109. offline(context) {
  110. if (app.isSingleDoc()) {
  111. return `/#/${context.path}`;
  112. }
  113. this.triggerRoute("offline");
  114. }
  115. about(context) {
  116. if (app.isSingleDoc()) {
  117. return `/#/${context.path}`;
  118. }
  119. context.page = "about";
  120. this.triggerRoute("page");
  121. }
  122. news(context) {
  123. if (app.isSingleDoc()) {
  124. return `/#/${context.path}`;
  125. }
  126. context.page = "news";
  127. this.triggerRoute("page");
  128. }
  129. help(context) {
  130. if (app.isSingleDoc()) {
  131. return `/#/${context.path}`;
  132. }
  133. context.page = "help";
  134. this.triggerRoute("page");
  135. }
  136. notFound(context) {
  137. this.triggerRoute("notFound");
  138. }
  139. isIndex() {
  140. return (
  141. this.context?.path === "/" ||
  142. (app.isSingleDoc() && this.context?.entry?.isIndex())
  143. );
  144. }
  145. isSettings() {
  146. return this.context?.path === "/settings";
  147. }
  148. setInitialPath() {
  149. // Remove superfluous forward slashes at the beginning of the path
  150. let path;
  151. if (
  152. (path = location.pathname.replace(/^\/{2,}/g, "/")) !== location.pathname
  153. ) {
  154. page.replace(path + location.search + location.hash, null, true);
  155. }
  156. if (location.pathname === "/") {
  157. if ((path = this.getInitialPathFromHash())) {
  158. page.replace(path + location.search, null, true);
  159. } else if ((path = this.getInitialPathFromCookie())) {
  160. page.replace(path + location.search + location.hash, null, true);
  161. }
  162. }
  163. }
  164. getInitialPathFromHash() {
  165. try {
  166. return new RegExp("#/(.+)").exec(decodeURIComponent(location.hash))?.[1];
  167. } catch (error) {}
  168. }
  169. getInitialPathFromCookie() {
  170. let path;
  171. if ((path = Cookies.get("initial_path"))) {
  172. Cookies.expire("initial_path");
  173. return path;
  174. }
  175. }
  176. replaceHash(hash) {
  177. page.replace(
  178. location.pathname + location.search + (hash || ""),
  179. null,
  180. true,
  181. );
  182. }
  183. };