sideBar.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { isArray, ensureStartingSlash, removeExtention } from '../utils';
  2. export function isSideBarConfig(sidebar) {
  3. return sidebar === false || sidebar === 'auto' || isArray(sidebar);
  4. }
  5. export function isSideBarGroup(item) {
  6. return item.children !== undefined;
  7. }
  8. export function isSideBarEmpty(sidebar) {
  9. return isArray(sidebar) ? sidebar.length === 0 : !sidebar;
  10. }
  11. /**
  12. * Get the `SideBarConfig` from sidebar option. This method will ensure to get
  13. * correct sidebar config from `MultiSideBarConfig` with various path
  14. * combinations such as matching `guide/` and `/guide/`. If no matching config
  15. * was found, it will return `auto` as a fallback.
  16. */
  17. export function getSideBarConfig(sidebar, path) {
  18. if (isSideBarConfig(sidebar)) {
  19. return sidebar;
  20. }
  21. path = ensureStartingSlash(path);
  22. for (const dir in sidebar) {
  23. // make sure the multi sidebar key starts with slash too
  24. if (path.startsWith(ensureStartingSlash(dir))) {
  25. return sidebar[dir];
  26. }
  27. }
  28. return 'auto';
  29. }
  30. /**
  31. * Get flat sidebar links from the sidebar items. This method is useful for
  32. * creating the "next and prev link" feature. It will ignore any items that
  33. * don't have `link` property and removes `.md` or `.html` extension if a
  34. * link contains it.
  35. */
  36. export function getFlatSideBarLinks(sidebar) {
  37. return sidebar.reduce((links, item) => {
  38. if (item.link) {
  39. links.push({ text: item.text, link: removeExtention(item.link) });
  40. }
  41. if (isSideBarGroup(item)) {
  42. links = [...links, ...getFlatSideBarLinks(item.children)];
  43. }
  44. return links;
  45. }, []);
  46. }