import { s3ContentsToTree } from "../utils/s3Helper"; import _ from "lodash"; const trimPrefix = (str, prefix, to) => { return str.replace(prefix, to); }; const trimSuffix = (str) => { return str.replace(/.md$/, ""); }; const filterLocaleContents = (contents, prefix, sortMap) => { return contents! .filter((cont) => cont.Key!.startsWith(prefix) && cont.Key !== `${prefix}index.md`) .map((cont) => { return { ...cont, Key: trimSuffix(trimPrefix(cont.Key, prefix, "")), link: trimSuffix(`/${cont.Key}`), sortKey: trimPrefix(cont.Key, prefix, ""), }; }) .sort((a, b) => sortMap?.[a.sortKey] - sortMap?.[b.sortKey]); }; const sideBarReducer = (r, label, i, a, Content) => { const extra = { collapsed: a.length !== i + 1, link: Content.link, }; if (a.length === i + 1) { extra.items = undefined; } else { extra.link = undefined; } return extra; }; export const buildSideBar = (contents, sideBarSortMap) => { const rootContents = filterLocaleContents( contents, "docs/", sideBarSortMap["zh-CN"] ); const zhHKContents = filterLocaleContents( contents, "zh-HK/docs/", sideBarSortMap["zh-HK"] ); const enUSContents = filterLocaleContents( contents, "en-US/docs/", sideBarSortMap["en-US"] ); const rootSideBar = s3ContentsToTree( rootContents, { label: "text", children: "items" }, sideBarReducer ); const zhHKSideBar = s3ContentsToTree( zhHKContents, { label: "text", children: "items" }, sideBarReducer ); const enUSSideBar = s3ContentsToTree( enUSContents, { label: "text", children: "items" }, sideBarReducer ); return { rootSideBar, zhHKSideBar, enUSSideBar }; };