Skip to content

Commit

Permalink
feat(@galactiks/explorer): add getPagesWithKeywordIdentifier reposito…
Browse files Browse the repository at this point in the history
…ry method
  • Loading branch information
emmanuelgautier committed Sep 16, 2023
1 parent 3ad4b5a commit f356289
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/explorer/src/core/content/compute.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GalactiksConfig } from '@galactiks/config';
import slugify from 'slugify';

import {
alternatesHeaderBuilder,
Expand Down Expand Up @@ -30,6 +29,7 @@ import type {
ContentlayerWebsite,
Person,
} from './types/index.mjs';
import { createIdentifierFromString } from './utils.mjs';

export type ComputeDTO<T> = {
config: GalactiksConfig;
Expand All @@ -42,7 +42,7 @@ function createPage<T>(
identifier: string,
document: Partial<ContentlayerWebPageDocument>
) {
const id = slugify(identifier, { lower: true, trim: true });
const id = createIdentifierFromString(identifier);

return {
_id: id,
Expand Down
11 changes: 11 additions & 0 deletions packages/explorer/src/core/content/repository.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
documentByIdentifierSelector,
documentsByLanguagesSelector,
} from './selectors.mjs';
import { createIdentifierFromString } from './utils.mjs';

let _generated: ContentlayerDataExports;
let _documents: Content[];
Expand Down Expand Up @@ -96,6 +97,16 @@ export const getPagesPartOf = async (
filters?: RepositoryFilters
): Promise<Content[]> =>
(await getPages(filters)).filter((doc) => doc.isPartOf === slug);
export const getPagesWithKeywordIdentifier = async (
keywordIdentifier: string,
filters?: RepositoryFilters
): Promise<Content[]> =>
(await getPages(filters)).filter(
(doc) =>
doc.keywords?.some(
(keyword) => createIdentifierFromString(keyword) === keywordIdentifier
)
);
export const getPageByIdentifier = async (
identifier: string,
filters?: RepositoryFilters
Expand Down
4 changes: 3 additions & 1 deletion packages/explorer/src/core/content/urls.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
pageDepthSelector,
} from './selectors.mjs';
import type { ContentlayerWebPageDocumentWithRender } from './render.mjs';
import { homeIdentifier } from './consts.mjs';

type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
type ContentlayerDocumentWithPath = WithRequired<
Expand Down Expand Up @@ -111,7 +112,8 @@ export const computeDocumentsUrl =

const existingStringProperties: [string, string][] = Object.entries({
...document,
identifier: document.identifier !== 'index' ? document.identifier : '',
identifier:
document.identifier !== homeIdentifier ? document.identifier : '',
isPartOf: isPartOfPath,
}).filter(([, value]) => typeof value === 'string');
return join(
Expand Down
4 changes: 4 additions & 0 deletions packages/explorer/src/core/content/utils.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import slugify from 'slugify';

export const createIdentifierFromString = (s: string) =>
slugify(s, { lower: true, trim: true });

0 comments on commit f356289

Please sign in to comment.