diffplug / spotless

Keep your code spotless
Apache License 2.0
4.57k stars 458 forks source link

Format http code in markdown #1272

Closed EricGao888 closed 2 years ago

EricGao888 commented 2 years ago

Sometimes we may have some http code in markdown, e.g. image link like:

<p align="center">
   <img src="xxxx" width="60%" />
 </p>

Currently, markdown.FlexmarkStep in spotless could not fix http code formatting in markdown. It would be pretty handy if spotless could support this feature in the future.

BTW, I'd like to help with it but currently I haven't found a good way to implement this. Any help will be appreciated! Thx

nedtwigg commented 2 years ago

With the Gradle plugin, you can apply different formatters on different blocks (formatter inception).

But this only works if you have a regex that demarcates the nested HTML from the rest of the markdown. If all of the cases you care about fit with '<p ', '</p>' then you could use this pattern to apply prettier or the Eclipse WTP formatter onto the embedded HTML. If that regex doens't capture all of the cases you care about, then you're probably better off improving the Flexmark project directly.

EricGao888 commented 2 years ago

@nedtwigg Thanks for the reply. May I ask whether there is something similar to the formatter inception in the Maven plugin?

nedtwigg commented 2 years ago

There is not, but it might be possible. In the Gradle plugin, FormatExtension is the receiver for adding generic steps. It has this method for defining a block, and then defining the steps within that block.

https://github.com/diffplug/spotless/blob/ce98b688cb4724a95e0abb05f4bbaf398097c1b8/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java#L674-L682

In the Maven plugin, the equivalents would be AbstractSpotlessMojo and FormatterFactory, but I'm not sure how to do a nested DSL in the maven plugin (or if that's even possible).

EricGao888 commented 2 years ago

There is not, but it might be possible. In the Gradle plugin, FormatExtension is the receiver for adding generic steps. It has this method for defining a block, and then defining the steps within that block.

https://github.com/diffplug/spotless/blob/ce98b688cb4724a95e0abb05f4bbaf398097c1b8/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java#L674-L682

In the Maven plugin, the equivalents would be AbstractSpotlessMojo and FormatterFactory, but I'm not sure how to do a nested DSL in the maven plugin (or if that's even possible).

Got it, thanks!

nedtwigg commented 2 years ago

Feel free to comment further if you need more help.