@slashid/docusaurus-theme-slashid
Version:
SlashID theme for Docusaurus.
111 lines (105 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.convertGlobToRegex = convertGlobToRegex;
exports.isCategory = isCategory;
exports.isHtmlSidebarItem = isHtmlSidebarItem;
exports.shouldItemRender = shouldItemRender;
exports.shouldNoItemsRender = shouldNoItemsRender;
exports.shouldPathRender = shouldPathRender;
var _globToRegexp = _interopRequireDefault(require("glob-to-regexp"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* ============================================================================
* Copyright (c) SlashID
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
function shouldPathRender(path, privatePaths, user) {
if (!privatePaths || !path) return true;
const matchingPrivatePaths = privatePaths.filter(privatePath => {
let matches = false;
if (typeof privatePath.path === "string") {
matches = path.includes(privatePath.path);
} else if (privatePath.path instanceof RegExp) {
matches = privatePath.path.test(path);
}
if (!matches) return false;
if (!user) return true;
if (privatePath.groups) {
const userGroups = user.getGroups();
return !privatePath.groups.every(group => userGroups.includes(group));
}
// invalid config - should render
return false;
});
return matchingPrivatePaths.length === 0;
}
/**
* Tests if the given item should be rendered based on the user state and the front matter config.
* @param slashIDProps set by the front matter on the relevant page
* @param user SlashID User instance
* @returns a {boolean} indicating whether the user should be able to see the item
*/
function shouldItemRender(slashIDProps, user) {
// paths take precedence
if (!slashIDProps || !slashIDProps.auth) {
return true;
}
const {
groups
} = slashIDProps;
if (!user) {
return false;
}
if (groups) {
const userGroups = user.getGroups();
return groups.every(group => userGroups.includes(group));
}
return true;
}
function isCategory(item) {
return item.hasOwnProperty("items") && item.type === "category";
}
function isHtmlSidebarItem(item) {
return item.type === "html";
}
function shouldNoItemsRender(items, user, getSlashIDProps) {
return items.filter(item => {
if (isCategory(item)) {
return shouldNoItemsRender(item.items, user, getSlashIDProps);
} else {
// custom items should always be rendered as they might have custom rules
if (isHtmlSidebarItem(item)) return true;
return shouldItemRender(getSlashIDProps(item), user);
}
}).length === 0;
}
/**
* Ensure the privatePaths config is converted to RegExp instances if globs are used.
*/
function convertGlobToRegex(config) {
if (config !== null && config !== void 0 && config.privatePaths) {
let convertedPaths = [];
convertedPaths = config.privatePaths.map(pathConfig => {
if (typeof pathConfig.path === "string") {
try {
return {
...pathConfig,
path: (0, _globToRegexp.default)(pathConfig.path)
};
} catch (e) {
console.error(`Invalid glob pattern: ${pathConfig}`);
return pathConfig;
}
}
return pathConfig;
});
return {
...config,
privatePaths: convertedPaths
};
}
return config;
}