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

Support dependency injection of extensions #561

Open
paulirwin opened this issue Nov 20, 2024 · 2 comments
Open

Support dependency injection of extensions #561

paulirwin opened this issue Nov 20, 2024 · 2 comments

Comments

@paulirwin
Copy link

I'm working on a custom markdown extension for my app but I need to access some global state that is in a service. I'd like to be able to use a Provider with a factory so that I can inject the service and use that when creating the extension. I'd imagine that the markedExtensions array could be updated to be (MarkedExtension | Provider)[] to support this. Currently I'm having to work around this by providing a MARKED_OPTIONS factory that uses the extensions property of MarkedOptions to do this, but this is very hacky as I have to split the extension into renderer, tokenizer, etc. and it makes it difficult to share things between them.

I'd like to be able to do something like the following in my app config:

export const appConfig: ApplicationConfig = {
    providers: [
        ...,
        provideMarkdown({
            markedExtensions: [
                {
                    provide: MARKED_EXTENSION,
                    useFactory: myCustomMarkedExtensionFactory,
                    deps: [MyService, ...],
                }
            ]
        }),
    ]
}
@jfcere
Copy link
Owner

jfcere commented Nov 22, 2024

Hi @paulirwin,

In the end the factory will still need to return an array of MarkedExtension but I guess this is what you want to achieve right?

@paulirwin
Copy link
Author

I am fine with either having the factory return the entire markedExtensions array, or being able to include multiple providers in that array, either works for me. Also it would be nice if that factory could return a Promise that is awaited too, so that I don't have to hack some synchronous code together like I'm doing now.

Basically, I have data that is loaded from a service (which loads data from an API) that I need to use in my custom Markdown extension to determine if matches should be turned into links. Marked docs say that they can support async, although the Typescript types don't seem to support that. But if I can asynchronously load the data from the API and then return the extension populated with the data, that would be fine too.

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

No branches or pull requests

2 participants