examples.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function _interopRequireDefault(obj) {
  4. return obj && obj.__esModule ? obj : { default: obj };
  5. }
  6. function _nullishCoalesce(lhs, rhsFn) {
  7. if (lhs != null) {
  8. return lhs;
  9. } else {
  10. return rhsFn();
  11. }
  12. }
  13. function _optionalChain(ops) {
  14. let lastAccessLHS = undefined;
  15. let value = ops[0];
  16. let i = 1;
  17. while (i < ops.length) {
  18. const op = ops[i];
  19. const fn = ops[i + 1];
  20. i += 2;
  21. if (
  22. (op === "optionalAccess" || op === "optionalCall") &&
  23. value == null
  24. ) {
  25. return undefined;
  26. }
  27. if (op === "access" || op === "optionalAccess") {
  28. lastAccessLHS = value;
  29. value = fn(value);
  30. } else if (op === "call" || op === "optionalCall") {
  31. value = fn((...args) => value.call(lastAccessLHS, ...args));
  32. lastAccessLHS = undefined;
  33. }
  34. }
  35. return value;
  36. }
  37. var _path = require("path");
  38. var _path2 = _interopRequireDefault(_path);
  39. const colorByHash = (input) => {
  40. let hash = 0;
  41. let color = "#";
  42. input.split("").forEach((char) => {
  43. hash = char.charCodeAt(0) + ((hash << 5) - hash);
  44. });
  45. for (let i = 0; i < 3; i++) {
  46. const value = (hash >> (i * 8)) & 0xff;
  47. color += ("00" + value.toString(16)).slice(-2);
  48. }
  49. return color;
  50. };
  51. const addColorToTags = (tags) => {
  52. let colors = [
  53. "#ef4444",
  54. "#f97316",
  55. "#f59e0b",
  56. "#eab308",
  57. "#84cc16",
  58. "#22c55e",
  59. "#10b981",
  60. "#14b8a6",
  61. "#06b6d4",
  62. "#0ea5e9",
  63. "#3b82f6",
  64. "#6366f1",
  65. "#8b5cf6",
  66. "#a855f7",
  67. "#d946ef",
  68. "#ec4899",
  69. "#f43f5e",
  70. ];
  71. // if there are more tags than colors, we will reuse colors.
  72. // multiply the colors array until it is bigger than the tags array
  73. while (colors.length < tags.length) {
  74. colors = [...colors, ...colors];
  75. }
  76. const selectedColorIndexes = [];
  77. const tagsWithColor = tags.map((tag) => {
  78. // pick a random color
  79. let randomColorIndex = Math.floor(Math.random() * colors.length);
  80. // if the color is already used, pick another one
  81. while (selectedColorIndexes.includes(randomColorIndex)) {
  82. randomColorIndex = Math.floor(Math.random() * colors.length);
  83. }
  84. const color = colors[randomColorIndex];
  85. selectedColorIndexes.push(randomColorIndex);
  86. return {
  87. name: tag,
  88. color: color,
  89. };
  90. });
  91. return tagsWithColor;
  92. };
  93. function plugin() {
  94. return {
  95. name: "docusaurus-plugin-refine-examples",
  96. configureWebpack(config) {
  97. return {
  98. resolve: {
  99. alias: {
  100. "@examples": _path2.default.join(
  101. _optionalChain([
  102. config,
  103. "access",
  104. (_) => _.resolve,
  105. "optionalAccess",
  106. (_2) => _2.alias,
  107. "optionalAccess",
  108. (_3) => _3["@generated"],
  109. ]),
  110. "docusaurus-plugin-refine-examples",
  111. "default",
  112. ),
  113. },
  114. },
  115. };
  116. },
  117. async contentLoaded({ allContent, actions }) {
  118. if (!process.env.DISABLE_EXAMPLES) {
  119. console.log("Composing Refine examples...");
  120. const { createData } = actions;
  121. const currentVersion =
  122. allContent["docusaurus-plugin-content-docs"].default
  123. .loadedVersions[0];
  124. const allDocs = currentVersion.docs;
  125. const allExamples = allDocs
  126. .filter(
  127. (doc) =>
  128. doc.id.startsWith("examples/") &&
  129. doc.id !== "examples/examples",
  130. )
  131. .map((doc) => {
  132. const titleFromId =
  133. doc.id
  134. .replace("examples/", "")
  135. .split("/")
  136. .slice(0, -1)
  137. .join("-") +
  138. " " +
  139. doc.title
  140. .replace("antd", "Ant Design")
  141. .replace("mui", "Material UI")
  142. .replace("chakra-ui", "Chakra UI");
  143. return {
  144. // ...doc,
  145. id: doc.id,
  146. baseTitle: doc.title,
  147. title: doc.title
  148. .replace("antd", "Ant Design")
  149. .replace("mui", "Material UI")
  150. .replace("chakra-ui", "Chakra UI"),
  151. displayTitle: _nullishCoalesce(
  152. _nullishCoalesce(
  153. doc.frontMatter["example-title"],
  154. () => titleFromId,
  155. ),
  156. () =>
  157. doc.title
  158. .replace("antd", "Ant Design")
  159. .replace("mui", "Material UI")
  160. .replace("chakra-ui", "Chakra UI"),
  161. ),
  162. description: doc.description,
  163. permalink: doc.permalink,
  164. tags: doc.frontMatter["example-tags"] || [],
  165. };
  166. });
  167. const allTags = allExamples
  168. .reduce((acc, example) => [...acc, ...example.tags], [])
  169. .filter((tag, index, self) => self.indexOf(tag) === index);
  170. const data = {
  171. examples: allExamples,
  172. tags: addColorToTags(allTags),
  173. };
  174. await createData(`examples-data.json`, JSON.stringify(data));
  175. } else {
  176. const { createData } = actions;
  177. await createData(
  178. `examples-data.json`,
  179. JSON.stringify({ examples: [], tags: [] }),
  180. );
  181. }
  182. },
  183. };
  184. }
  185. exports.default = plugin;