sideBar.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { s3ContentsToTree } from "../utils/s3Helper";
  2. import _ from "lodash";
  3. const trimPrefix = (str, prefix) => {
  4. return str.replace(new RegExp(`^${prefix}`), "docs");
  5. };
  6. const trimSuffix = (str) => {
  7. return str.replace(/.md$/, "");
  8. };
  9. const filterLocaleContents = (contents, prefix) => {
  10. return contents!
  11. .filter((cont) => new RegExp(`^${prefix}/`).test(cont.Key!))
  12. .map((cont) => {
  13. return {
  14. ...cont,
  15. Key: trimSuffix(trimPrefix(cont.Key, prefix)),
  16. };
  17. });
  18. };
  19. const sideBarReducer = (r, label, i, a, Content) => {
  20. const extra = {
  21. collapsed: a.length !== i + 1,
  22. };
  23. if (a.length === i + 1) {
  24. extra.items = undefined;
  25. } else {
  26. extra.link = undefined
  27. }
  28. return extra;
  29. };
  30. export const buildSideBar = (contents) => {
  31. const rootContents = filterLocaleContents(contents, "docs");
  32. const zhHKContents = filterLocaleContents(contents, "zh-HK/docs");
  33. const rootSideBar = s3ContentsToTree(
  34. rootContents,
  35. { key: "link", label: "text", children: "items" },
  36. sideBarReducer
  37. )[0].items;
  38. const zhHKSideBar = s3ContentsToTree(
  39. zhHKContents,
  40. { key: "link", label: "text", children: "items" },
  41. sideBarReducer
  42. )[0].items;
  43. return { rootSideBar, zhHKSideBar };
  44. };