Skip to content

Support custom TextMate themes in consuming modules#103

Open
Dansoftowner wants to merge 6 commits intoJPro-one:mainfrom
Dansoftowner:load-other-module-themes
Open

Support custom TextMate themes in consuming modules#103
Dansoftowner wants to merge 6 commits intoJPro-one:mainfrom
Dansoftowner:load-other-module-themes

Conversation

@Dansoftowner
Copy link
Copy Markdown
Contributor

Overview

This PR makes code theme loading customizable for MarkdownCodeBlock.

This PR is based on #102, so to understand this one, the #102 should be looked at first.

Previously, applications using jpro-mdfx as a library could not load their own TextMate code themes with CSS such as -mdfx-code-theme: "path/to/my/own/theme", even when the resource package was opened correctly, because theme paths were resolved relative to MDFX internal classes instead of the consuming application.

Changes

  • Added MarkdownCodeBlock.createCodeThemeSource(String codeTheme) as an overridable hook for custom theme loading.
  • Added MarkdownView.setThemeResourceClass(Class<?>) and getThemeResourceClass().
  • MarkdownView now passes its configured theme resource class to generated MarkdownCodeBlock instances.
  • MarkdownCodeBlock first attempts to load the requested theme relative to the configured theme resource class.
  • If the theme is not found there, loading falls back to MarkdownCodeBlock.class, preserving support for MDFX bundled themes.
  • Added a nord theme option to the sample application for demonstration

Why

This allows applications and modules using MDFX to package and load their own TextMate code themes. It also keeps the default behavior compatible with existing theme paths.

If MarkdownView is extended, the subclass is used as the default theme resource class, so in many cases calling setThemeResourceClass(Class<?>) explicitly is not needed.

Examples

.markdown-code-block {
    -mdfx-code-theme: "/com/example/app/themes/my-theme.json";
}
MarkdownView markdownView = new MarkdownView(markdown);
markdownView.setThemeResourceClass(MyApplication.class);

When MarkdownView is subclassed, the subclass is used as the default theme resource class:

public class MyMarkdownView extends MarkdownView {
    public MyMarkdownView(String markdown) {
        super(markdown);
    }
}

For advanced cases, MarkdownCodeBlock.createCodeThemeSource(...) can still be overridden:

MarkdownView markdownView = new MarkdownView(markdown) {
    @Override
    public Node generateFencedCodeBlock(String code, String language) {
        return new MarkdownCodeBlock(code, language) {
            @Override
            protected IThemeSource createCodeThemeSource(String codeTheme) {
                return IThemeSource.fromResource(MyApplication.class, codeTheme);
            }
        };
    }
};

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