asjqkkkk / markdown_widget

📖Rendering markdown by flutter!Welcome for pr and issue.
MIT License
332 stars 95 forks source link

Support Multiple PreConfig for different languages #114

Open huntrist opened 1 year ago

huntrist commented 1 year ago

Is your feature request related to a problem? Please describe. Currently, only support 1 PreConfig, but sometimes, there might be multiple code blocks of DIFFERENT languages, and the language used might not be known before the markdown text is rendered.

Describe the solution you'd like Please consider support multiple PreConfig: let user define ALL the languages they want to be supported, and MarkdownWidget chooses the correct one based on the Language marking in the markdown

Describe alternatives you've considered N/A

Additional context N/A

asjqkkkk commented 1 year ago

Hi @huntrist , you can use the PreConfig like this:

            return MarkdownWidget(
                data: data!,
                config: config.copy(configs: [
                  PreConfig().copy(wrapper: (child, text){
                    ///detecting the current language through `text`
                    return YourCodeWidget(differentLanguage);
                  })
                ]),
            );

or custom your own CodeBlockNode

Because the content in CodeBlock cannot be determined currently, it may be pure text, a particular programming language, or a mixture of multiple languages. Therefore, I believe that in order to achieve such functionality, it needs to be determined by the user themselves.

huntrist commented 1 year ago

Hi @huntrist , you can use the PreConfig like this:

            return MarkdownWidget(
                data: data!,
                config: config.copy(configs: [
                  PreConfig().copy(wrapper: (child, text){
                    ///detecting the current language through `text`
                    return YourCodeWidget(differentLanguage);
                  })
                ]),
            );

or custom your own CodeBlockNode

Because the content in CodeBlock cannot be determined currently, it may be pure text, a particular programming language, or a mixture of multiple languages. Therefore, I believe that in order to achieve such functionality, it needs to be determined by the user themselves.

thanks, this could work, but I still think making it accept multiple PreConfig for different languages, and detect language marking by the MarkdownWidget is a cleaner approach. let me elaborate:

I believe it is a common practice specifying language with a "language marking" (called "info string" in the CommonMark spec: see text right after example-141: https://spec.commonmark.org/0.30/#example-141)

i.e. ``` javascript

if we let user specify multiple PreConfig for different languages, and MarkdownWidget can automatically detects the "language name marking" (javascript in this case), then the highlights can be applied accordingly. If no matching PreConfig for the language marking is found (or if no language marking is given), then apply the default PreConfig.

asjqkkkk commented 1 year ago

Hi @huntrist , you can use the PreConfig like this:

            return MarkdownWidget(
                data: data!,
                config: config.copy(configs: [
                  PreConfig().copy(wrapper: (child, text){
                    ///detecting the current language through `text`
                    return YourCodeWidget(differentLanguage);
                  })
                ]),
            );

or custom your own CodeBlockNode Because the content in CodeBlock cannot be determined currently, it may be pure text, a particular programming language, or a mixture of multiple languages. Therefore, I believe that in order to achieve such functionality, it needs to be determined by the user themselves.

thanks, this could work, but I still think making it accept multiple PreConfig for different languages, and detect language marking by the MarkdownWidget is a cleaner approach. let me elaborate:

I believe it is a common practice specifying language with a "language marking" (called "info string" in the CommonMark spec: see text right after example-141: https://spec.commonmark.org/0.30/#example-141)

i.e. ``` javascript

if we let user specify multiple PreConfig for different languages, and MarkdownWidget can automatically detects the "language name marking" (javascript in this case), then the highlights can be applied accordingly. If no matching PreConfig for the language marking is found (or if no language marking is given), then apply the default PreConfig.

Hi @huntrist , that's a good idea, I will consider how to implement this feature in the upcoming version.