Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Pass subdirectory as an argument to injectContentFiles #347

Closed
1 of 2 tasks
gergobergo opened this issue Apr 15, 2023 · 3 comments
Closed
1 of 2 tasks

RFC: Pass subdirectory as an argument to injectContentFiles #347

gergobergo opened this issue Apr 15, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@gergobergo
Copy link
Contributor

Which scope/s are relevant/related to the feature request?

content

Information

Currently, when we run the injectContentFiles function, we collect all the available files from the content folder.
I would like to suggest a way, that only the files from the specified subdirectory are returned.

Proposed API change:
function injectContentFiles<Attributes extends Record<string, any>>(options?: { subdirectory?: string }): ContentFile<Attributes>[]

As I see the current implementation comes with the InjectionToken factory. We cannot pass an argument to the factory function so some workaround needs to be done here. One possible solution is to have a service that stores the subdirectory value, and we use the inject function to access this service when we call the getContentFilesList function.

We can change this file to accept a subdirectory value like this:

export const getContentFilesList = (subdirectory?: string) => {
  const targetFolder = subdirectory ? `${subdirectory}/**` : '**';
  return import.meta.glob<Record<string, any>>(
    `/src/content/${targetFolder}/*.md`,
    {
      eager: true,
      import: 'default',
      query: { 'analog-content-list': true },
    }
  );
};

Describe any alternatives/workarounds you're currently using

Currently I think the only workaround is to get all the files, and filter the array for the specific folder files.

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@gergobergo gergobergo added the enhancement New feature or request label Apr 15, 2023
@brandonroberts
Copy link
Member

You cannot define glob paths dynamically at runtime. So it would have to be a filter. It's a reasonable change though.

You can filter the array of returned files also

const posts = injectContentFiles().filter(someFilter)

I think passing a callback function for a filter would work and be flexible enough to use in different ways

@gergobergo
Copy link
Contributor Author

Sounds good to me. I think the signature of the filter method can be passed as a predicate function. I will prepare a PR for this.

gergobergo added a commit to gergobergo/analog that referenced this issue Apr 16, 2023
this feature add a functionality to the injectContentFiles function
to filter out some of the content files before it returns

analogjs#347
gergobergo added a commit to gergobergo/analog that referenced this issue Apr 16, 2023
this feature add a functionality to the injectContentFiles function
to filter out some of the content files before it returns

analogjs#347
@gergobergo
Copy link
Contributor Author

The related PR is here:
#348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants