@becklyn/contentful-adapter
Version:
[](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml)
57 lines (56 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPageCache = void 0;
const getPageCache = (pages) => {
const slugCache = {};
const titleCache = {};
pages.forEach(page => {
let slug = page.fields.slug;
if (slug && !slug.startsWith("/")) {
slug = "/" + slug;
}
slugCache[page.sys.id] = slug;
titleCache[page.sys.id] = page.fields.title;
});
const getSlugOfPage = (id) => {
return slugCache[id];
};
const getTitleOfPage = (id) => {
return titleCache[id];
};
const getTitleOfPageBySlug = (slug) => {
for (let id in slugCache) {
const currentSlug = slugCache[id];
if (currentSlug === slug) {
return getTitleOfPage(id);
}
}
return null;
};
const getIdOfPageBySlug = (slug) => {
for (let id in slugCache) {
const currentSlug = slugCache[id];
if (currentSlug === slug) {
return id;
}
}
return null;
};
const isSlugExisting = (slug) => {
for (let id in slugCache) {
const currentSlug = slugCache[id];
if (currentSlug === slug) {
return true;
}
}
return false;
};
return {
getSlugOfPage,
getTitleOfPage,
getTitleOfPageBySlug,
getIdOfPageBySlug,
isSlugExisting,
};
};
exports.getPageCache = getPageCache;