Skip to content

markdown document preview improvements#9240

Merged
haidubogdan merged 1 commit intoapache:masterfrom
haidubogdan:t_markdown_improvements
Mar 18, 2026
Merged

markdown document preview improvements#9240
haidubogdan merged 1 commit intoapache:masterfrom
haidubogdan:t_markdown_improvements

Conversation

@haidubogdan
Copy link
Contributor

@haidubogdan haidubogdan commented Mar 2, 2026

This PR proposes some hard coded styling improvements for markdown preview renderer.
Swing html renderer has a lot of Css limitations, so future improvements could look at using other html java renderers

Before:

image

After:

image

Closes #6652

  • hardcoded swing html renderer compatible css styling
  • custom <hr> tag renderer
  • strikethrough flexmark extension preparation
  • update vscode regex for striketrhough detection

Know limitations

  • strikethrough element parser requires binary extension from flexmark
  • <code> don't apply padding

^Add meaningful description above

Click to collapse/expand PR instructions

By opening a pull request you confirm that, unless explicitly stated otherwise, the changes -

  • are all your own work, and you have the right to contribute them.
  • are contributed solely under the terms and conditions of the Apache License 2.0 (see section 5 of the license for more information).

Please make sure (eg. git log) that all commits have a valid name and email address for you in the Author field.

If you're a first time contributor, see the Contributing guidelines for more information.

If you're a committer, please label the PR before pressing "Create pull request" so that the right test jobs can run.

PR approval and merge checklist:

  1. Was this PR correctly labeled, did the right tests run? When did they run?
  2. Is this PR squashed?
  3. Are author name / email address correct? Are co-authors correctly listed? Do the commit messages need updates?
  4. Does the PR title and description still fit after the Nth iteration? Is the description sufficient to appear in the release notes?

If this PR targets the delivery branch: don't merge. (full wiki article)

@mbien mbien added this to the NB30 milestone Mar 2, 2026
@mbien mbien added the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Mar 2, 2026
@mbien
Copy link
Member

mbien commented Mar 2, 2026

only took a quick look at the output of some readme files and I think it looks better. Will need some tweaks for the dark theme since the background renders as white atm etc.

@haidubogdan haidubogdan force-pushed the t_markdown_improvements branch from af46460 to 1982844 Compare March 2, 2026 20:50
@Chris2011
Copy link
Contributor

We already talked about "customization" and it is fine to make it hard coded, but can't we do it direclty inside a css file. it is still hard coded but I can change this, if i want in the settings. So my idea is to add the css to the css file and read it from there.

@haidubogdan
Copy link
Contributor Author

We already talked about "customization" and it is fine to make it hard coded, but can't we do it direclty inside a css file. it is still hard coded but I can change this, if i want in the settings. So my idea is to add the css to the css file and read it from there.

I remember being a hustle to include a css resource file and after in runtime in the compiled state to read it.
@lkishalmi used this solution which seems to work.

            String configPath = "Editors/" + MarkdownDataObject.MIME_TYPE +"/FontsColors/" + profile + "/Defaults/viewer.css";
            FileObject config = FileUtil.getSystemConfigFile(configPath)

And this is my worry about this, in my personal opinion it doesn't seem to be efficient.
I would be inclined to use a static String or a bundle property value to be more adapted with how java works.

@Chris2011
Copy link
Contributor

Ok, no worries, I forgot that problem. So everything seems fine :)

@haidubogdan
Copy link
Contributor Author

: )) thanks.
Actually adding the css in a bundle property could be a good idea.
It will be better for maintaining and searching the configuration.

@Chris2011
Copy link
Contributor

Bundle could be also fine, yes.

@neilcsmith-net
Copy link
Member

Bundle would be OK for allowing platform applications to override. Not sure if any are using this. But the config file system is probably a better choice if we want to allow IDE users to override?

Also, in reference to what @mbien mentioned above, probably a light and a dark CSS file should be available, with the correct one loaded on the basis of checking for the dark theme flag?

@haidubogdan
Copy link
Contributor Author

I think there is enough time to see where this goes :) .
I will start with a bundle property implementation and after that if I have time I can look into the darktheme auto-detect logic.

The problem with custom css upload is that Java Swing doesn't really render well css styling, so it's not really useful for users.

Maybe combining Fonts & Colors and styling content with placeholders could work to give some level of customization.

@mbien
Copy link
Member

mbien commented Mar 4, 2026

I can look into the darktheme auto-detect logic.

taking the editor background might be good enough or not setting it at all (-> transparent?) which would inherit it automatically from the theme / customized flatlaf properties.

@neilcsmith-net
Copy link
Member

Better to use an isDarkLaf check similar to https://github.com/apache/netbeans/blob/master/platform/openide.util.ui/src/org/openide/util/ImageUtilities.java#L383 which delegates to -

return UIManager.getBoolean("nb.dark.theme");

Although given how use of this pattern is growing, we could probably do with providing an API for it, probably in the adjacent Utilities class.

@mbien
Copy link
Member

mbien commented Mar 4, 2026

checking for dark theme would imply that it would switch between two sets of hardcoded colors or css. Which I believe would be good to avoid and simply inherit the color from somewhere else.

I am somewhat skeptical that exposing the css to the user is needed and/or useful, given that it would clash with theming if not done carefully. This started as attempt to improve the defaults which is great (defaults are important!), but the customizable css is kept being added as feature creep distracting from the original goal.

Looking through this changeset it appears that there are only a few colors needed atm - with a bit of luck they all can be derived from already existing colors in the IDE.

@neilcsmith-net
Copy link
Member

Customization is secondary to the fact that this belongs on the system filesystem, like other similar configuration.

@haidubogdan
Copy link
Contributor Author

I think I somehow nailed it.
Still need to do some cleanup of the code, but the general idea is here.
The coloring is linked to font the configs and using #6662 event listener idea the redraw is triggered on profile change.

35d4077

Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks promising IMO!

the pre-block seems to use a hardcoded value still. NB's README.md using "FlatLaf Dark" looks like:

Image

@haidubogdan haidubogdan force-pushed the t_markdown_improvements branch from 8fc9b91 to 120d595 Compare March 6, 2026 09:42
@haidubogdan haidubogdan force-pushed the t_markdown_improvements branch from 120d595 to da16438 Compare March 8, 2026 12:50
Copy link
Contributor

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think this is a good improvement. I left a few inline comments though.

@haidubogdan haidubogdan force-pushed the t_markdown_improvements branch from bd27cea to 93a4bc5 Compare March 15, 2026 09:22
Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed this warning while testing (used the dev build with fresh config):

WARNING [org.netbeans.modules.editor.settings.storage.ProfilesTracker]: Ignoring profile 'Earth' (Editors/text/x-markdown-nb-preview/FontsColors/Earth) in favor of 'NetBeansEarth' (Editors/FontsColors/NetBeansEarth) with the same display name.

interestingly this warning shows only up mentioning the Earth profile. (I used the default FlatLafLight and Dark themes)

appendColoringCssRulesFromFontConfigs(ss);
}

public static void addRule(StyleSheet ss, AttributeSet attributeSet, AttributeSet defaultAttributes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a problem while experimenting with this. I tried to set the headings all to a "fancy" font, so that I can recognize the change. See this preview:

Image

The preview shows, that H1 is changed, but H2 remains the base font. That is in contrast to the configuration:

Image Image

The configuration (FontAndColors.xml) indicates that H2 should inherit from H1 and in the settings this is correctly recognized (see final "Font" setting for H2).

I think the problem is that the attribute application here does not follow the inheritance tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the inheritance is not really working and another thing might be that the anchor link element doesn't have any styling options.
The thing is that I've never thought about styling a markdown preview. I don't know what are the end-user behavior and needs.
So, I would try to limit to these 2 feature analysis to free up time.
I think it's at a workable level so far :) .

Copy link
Contributor Author

@haidubogdan haidubogdan Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other possible todos:

  • use 14px for the inline code size (it seems there is some render issue)
  • decrease contrast on code background color for dark modes.
  • line height or padding for code text

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - lets keep this minimal. Please have a look here:

matthiasblaesing@8a71205

There are two different things in here:

  • Remove the inheritance marker. I had a prototype for inheritance, but that is even more complex and brings more questions than answers. For real inheritance you'd need to gather all AttributeSets that are chained using the default attribute and create a composite set (there is a helper in the editor settings for that).
  • Simplify the StyleUtils. With the current implementation addRule will be invoked for each attribute name, in effect adding the AttributeSet multiple times. I removed the iteration over the attributes as there is alreay a guard inside addRule.

@haidubogdan
Copy link
Contributor Author

I thought I could "humble" Swing's Html editor but I think in the future we might need to replace html and css content rendering with something like JavaFX WebView .
I still can't make <span> elements to have padding.

For the moment at least it looks a bit more modern and there is the option to customize using Netbeans font config themes.

Copy link
Contributor

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much. Seems to work nicely. Here is a before (left) and after (after) shot:

Image

To me this looks sane.

@lkishalmi as you are active in this area, could you please also have a look?

@mbien
Copy link
Member

mbien commented Mar 17, 2026

also: don't forget to squash before merging ;). There is also a optional checklist in the PR template in case its useful - if not, feel free to remove it on future PRs. (its mostly intended as a guide for less active committers)

@haidubogdan
Copy link
Contributor Author

All right :) . I think I began to get used to the PR flow, so I think I could remove it in the future.

Closes apache#6652

- bump java release version to 17
- custom <hr> tag renderer
- added flexmark strikethrough extension
- update vscode regex for striketrhough detection
- updated mime type to the latest official text/markdown
- created new mime type for preview
- using a default skeleton css code for margin, paddings ...
- added font configs for markdown preview
- link coloring and font size customization css to font config settings
@haidubogdan haidubogdan force-pushed the t_markdown_improvements branch from f52af58 to 3f4b0ef Compare March 18, 2026 06:32
@BradWalker
Copy link
Member

Hey @haidubogdan , just wanted to say thanks for your efforts on this!

Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree, looks great!

@haidubogdan haidubogdan merged commit b53dd61 into apache:master Mar 18, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Editor Markdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown files are not rendered correctly

6 participants