analogjs / analog

The fullstack meta-framework for Angular. Powered by Vite and Nitro
https://analogjs.org
MIT License
2.5k stars 240 forks source link

RFC: Pass subdirectory as an argument to `injectContentFiles` #347

Closed gergobergo closed 1 year ago

gergobergo commented 1 year ago

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

brandonroberts commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

The related PR is here: https://github.com/analogjs/analog/pull/348