vsch / flexmark-java

CommonMark/Markdown Java parser with source level AST. CommonMark 0.28, emulation of: pegdown, kramdown, markdown.pl, MultiMarkdown. With HTML to MD, MD to PDF, MD to DOCX conversion modules.
BSD 2-Clause "Simplified" License
2.23k stars 262 forks source link

Can I mix Gitlab extension with Pegdown extension? #475

Open huapeng01016 opened 2 years ago

huapeng01016 commented 2 years ago

I like to use katex and mermaid features in Gitlab extension from my pergdown based application. Is it possible to use both Pegdown and Gitlab extensions. If yes, how to setup the Parser? I am using the standard way in the example to setup the pegdown extensions:

static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions( 
            Extensions.ALL 
            );

static Parser PARSER = Parser.builder(OPTIONS).build();
static HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();

I have not been able to hook in the:

final private static DataHolder OPTIONS = new MutableDataSet()
            .set(Parser.EXTENSIONS, Collections.singleton(GitLabExtension.create()))
            .set(Parser.LISTS_AUTO_LOOSE, false)
            .toImmutable();

Any help would be greatly appreciated.

vsch commented 2 years ago

You can provide a list of extensions to add to Pegdown flags:

Here is a function for flexmarkOptions, you have extensions which will be added:

    public static DataHolder flexmarkOptions(int pegdownExtensions, Extension... extensions) {
        return flexmarkOptions(false, pegdownExtensions, extensions);
    }

For example:

    final private static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(Extensions.ALL, CustomExtension.create());

To add/change option flags you can use the return value and convert it to mutable via .toMutable(), make your changes and then convert back to immutable via .toImmutable()

For example:

static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions( 
            Extensions.ALL,
                        GitLabExtension.create()
            ).toMutable()
                        .set(Parser.LISTS_AUTO_LOOSE, false)
                       .toImmutable();