Skip to content

More flexible fenced code block generation#102

Open
Dansoftowner wants to merge 4 commits intoJPro-one:mainfrom
Dansoftowner:flexible-code-block-gen
Open

More flexible fenced code block generation#102
Dansoftowner wants to merge 4 commits intoJPro-one:mainfrom
Dansoftowner:flexible-code-block-gen

Conversation

@Dansoftowner
Copy link
Copy Markdown
Contributor

@Dansoftowner Dansoftowner commented Apr 29, 2026

Overview

This PR makes fenced code block rendering more flexible in jpro-mdfx.

Previously, MDFXNodeHelper always created a MarkdownCodeBlock directly for fenced code blocks. This made it hard for users to customize code block rendering.

Changes

  • Added MarkdownView.generateFencedCodeBlock(String code, String language).
  • Updated fenced code block generation to call this new method instead of instantiating MarkdownCodeBlock directly.
  • Kept the default behavior unchanged by returning new MarkdownCodeBlock(code, language) from the default implementation.

Why

This allows users to subclass MarkdownView and customize fenced code block rendering, similar to the existing customization points for links and images.

For example, users can now provide their own code block node, wrap the default MarkdownCodeBlock, add extra controls, change layout behavior, or apply app-specific behavior while keeping the default MDFX behavior intact.

Example

Let's say we want to add a context menu to each code block to give the user the ability to easily copy the particular code to the clipboard:

var markdownView = new MarkdownView("", markdownExtensions) {
            // ...

            @Override
            public Node generateFencedCodeBlock(String code, String language) {
                Node codeBlock = super.generateFencedCodeBlock(code, language);

                MenuItem copyItem = new MenuItem("Copy to Clipboard");
                copyItem.setOnAction(event -> {
                    ClipboardContent content = new ClipboardContent();
                    content.putString(code);
                    Clipboard.getSystemClipboard().setContent(content);
                });

                var contextMenu = new ContextMenu(copyItem);

                codeBlock.setOnMouseClicked(event -> {
                   if (event.getButton() == MouseButton.SECONDARY) {
                       contextMenu.show(codeBlock, event.getScreenX(), event.getScreenY());
                   }
                });

                return codeBlock;
            }
        };

It's now possible using the standard api.

Note: This example has been put to the sample application.

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

Successfully merging this pull request may close these issues.

1 participant