examples.ts 6.1 KB

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