import { s3ContentsToTree } from "../utils/s3Helper"; import _ from "lodash"; const trimPrefix = (str, prefix) => { return str.replace(new RegExp(`^${prefix}`), "docs"); }; const trimSuffix = (str) => { return str.replace(/.md$/, ""); }; const filterLocaleContents = (contents, prefix) => { return contents! .filter((cont) => new RegExp(`^${prefix}/`).test(cont.Key!)) .map((cont) => { return { ...cont, Key: trimSuffix(trimPrefix(cont.Key, prefix)), }; }); }; const sideBarReducer = (r, label, i, a, Content) => { const extra = { collapsed: a.length !== i + 1, }; if (a.length === i + 1) { extra.items = undefined; } else { extra.link = undefined } return extra; }; export const buildSideBar = (contents) => { const rootContents = filterLocaleContents(contents, "docs"); const zhHKContents = filterLocaleContents(contents, "zh-HK/docs"); const rootSideBar = s3ContentsToTree( rootContents, { key: "link", label: "text", children: "items" }, sideBarReducer )[0].items; const zhHKSideBar = s3ContentsToTree( zhHKContents, { key: "link", label: "text", children: "items" }, sideBarReducer )[0].items; return { rootSideBar, zhHKSideBar }; };