mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 12:43:05 +00:00
49 lines
907 B
JavaScript
49 lines
907 B
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* @emails react-core
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import slugify from './slugify';
|
|
|
|
/**
|
|
* Helper method to locate the section containing the current URL/path.
|
|
* This method specifically works with the nav_*.yml format.
|
|
*/
|
|
|
|
type Item = {
|
|
id: string,
|
|
subitems: Array<Item>,
|
|
};
|
|
|
|
type Section = {
|
|
items: Array<Item>,
|
|
};
|
|
|
|
const findSectionForPath = (
|
|
pathname: string,
|
|
sections: Array<Section>,
|
|
): Section | void => {
|
|
let activeSection;
|
|
const slugId = pathname.split('/').slice(-1)[0];
|
|
|
|
sections.forEach(section => {
|
|
const match = section.items.some(
|
|
item =>
|
|
slugId === slugify(item.id) ||
|
|
(item.subitems &&
|
|
item.subitems.some(subitem => slugId === slugify(subitem.id))),
|
|
);
|
|
if (match) {
|
|
activeSection = section;
|
|
}
|
|
});
|
|
|
|
return activeSection;
|
|
};
|
|
|
|
export default findSectionForPath;
|