checklist.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 _fsextra = require("fs-extra");
  38. var _fsextra2 = _interopRequireDefault(_fsextra);
  39. var _path = require("path");
  40. var _path2 = _interopRequireDefault(_path);
  41. const getDocContent = async (docPath) => {
  42. const accessPath = docPath.startsWith("@site/")
  43. ? docPath.replace("@site/", "./")
  44. : docPath;
  45. return new Promise((resolve) => {
  46. _fsextra2.default.readFile(
  47. _path2.default.resolve(accessPath),
  48. (err, data) => {
  49. if (err) {
  50. resolve(undefined);
  51. }
  52. return resolve(data.toString());
  53. },
  54. );
  55. });
  56. };
  57. const getChecklistItems = (docContent) => {
  58. const regex = /<ChecklistItem[\s\n\r\t]+id="((?:\w|\d|-|_)+)"[\s\n\r\t]*>/g;
  59. const matches = docContent.matchAll(regex);
  60. const itemIds = Array.from(matches).map((match) => match[1]);
  61. return itemIds;
  62. };
  63. const getUnitById = (id) => {
  64. // tutorial/<unit-name>/<ui-scope(optional)>/<tutorial-slug>
  65. const unitId = id.split("/")[1];
  66. return unitId;
  67. };
  68. function plugin() {
  69. return {
  70. name: "docusaurus-plugin-refine-checklist",
  71. configureWebpack(config) {
  72. return {
  73. resolve: {
  74. alias: {
  75. "@checklists": _path2.default.join(
  76. _optionalChain([
  77. config,
  78. "access",
  79. (_) => _.resolve,
  80. "optionalAccess",
  81. (_2) => _2.alias,
  82. "optionalAccess",
  83. (_3) => _3["@generated"],
  84. ]),
  85. "docusaurus-plugin-refine-checklist",
  86. "default",
  87. ),
  88. },
  89. },
  90. };
  91. },
  92. async contentLoaded({ allContent, actions }) {
  93. if (!process.env.DISABLE_CHECKLISTS) {
  94. console.log("Composing Refine tutorial checklists...");
  95. const { createData } = actions;
  96. const currentVersion =
  97. allContent["docusaurus-plugin-content-docs"].default
  98. .loadedVersions[0];
  99. const allDocs = currentVersion.docs;
  100. const allTutorials = allDocs.filter(
  101. (doc) =>
  102. doc.id.startsWith("tutorial/") &&
  103. doc.id !== "tutorial/tutorial",
  104. );
  105. const tutorialsWithChecklist = await Promise.all(
  106. allTutorials.map(async (tutorial) => {
  107. const docContent = await getDocContent(tutorial.source);
  108. const checklistItemIds = getChecklistItems(
  109. _nullishCoalesce(docContent, () => ""),
  110. );
  111. return {
  112. id: tutorial.id,
  113. unit: getUnitById(tutorial.id),
  114. title: tutorial.title,
  115. checklist: checklistItemIds.map((id, index) => ({
  116. id,
  117. index,
  118. })),
  119. };
  120. }),
  121. );
  122. const data = {
  123. items: tutorialsWithChecklist,
  124. };
  125. await createData(
  126. `tutorial-checklist-data.json`,
  127. JSON.stringify(data),
  128. );
  129. } else {
  130. const { createData } = actions;
  131. await createData(
  132. `tutorial-checklist-data.json`,
  133. JSON.stringify({ items: [] }),
  134. );
  135. }
  136. },
  137. };
  138. }
  139. exports.default = plugin;