treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

Add markdown-code-runner

Open wolfgangwalther opened this issue 5 months ago • 6 comments

We're using markdown-code-runner in Nixpkgs for the docs already and would like to use it with treefmt as well. Upstream PR is here: https://github.com/NixOS/nixpkgs/pull/427460. This works, but ofc it would be nicer if this was integrated in treefmt-nix.

Since mdcr is essentially a wrapper around other formatters, it would be nice to be able to link the existing formatter configurations to markdown code blocks for a specific block.

For example, instead of setting up another nixfmt of nix blocks, it would be better to re-use the existing settings.formatters.nixfmt for it etc.

Has there been any previous work / ideas on either setting up "nested / injected languages" * or re-using these definitions in other formatters?

* "nix in markdown" here, but it generalizes to many other cases, too, I think - for example "bash in nix"...

Some ideas:

  • One way to do it would be to put the language specification on each of the existing formatters. So for example in nixfmt.nix, I'd add markdown-language = "nix"; or so. This is bad, because it (a) forces exactly the same settings in *.nix files and nix blocks inside *.md files - but it could be the case that the latter should use different settings (not that it would matter for nixfmt at all...). Also, this could lead to conflicts, if multiple modules define the same language.
  • Another way would be to have a mapping in the settings for markdown-code-runner, where I can say:
    languages = {
      nix = { ... };
      json = { ... };
    };
    
    Ideally the options would be set up in a way, that would allow me to do something like nix = config.settings.formatter.nixfmt or so, thus making it easy to copy exactly the same settings, but also allowing to modify them.

cc @drupol @Mic92 @MattSturgeon

wolfgangwalther avatar Jul 23 '25 10:07 wolfgangwalther

Maybe one could assign formatters from the upper level through config.formatter.<language> directory to markdown-code-runner?

Mic92 avatar Jul 23 '25 13:07 Mic92

Has there been any previous work / ideas on either setting up "nested / injected languages"

We have previously discussed this in the context of vanilla treefmt and decided that it is out of scope for that project. See https://github.com/numtide/treefmt/discussions/503. <tangent>I suspect there's a more general version of markdown-code-runner one could build that uses treesitter under the hood, which already knows about language injections.</tanget>

One way to do it would be to put the language specification on each of the existing formatters. So for example in nixfmt.nix, I'd add markdown-language = "nix";

I like this idea. I'm pretty sure we'd need to support multiple languages, though: markdown-languages = ["nix"];

This [putting the language specification on each of the existing formatters] is bad, because it (a) forces exactly the same settings in *.nix files and nix blocks inside *.md files

Could we have the formatters the user has enabled be the defaults, but also overridable for markdown-code-runner? (This is straining my knowledge about the nixos module system.)

Also, this could lead to conflicts, if multiple modules define the same language

I don't see the problem here. Does markdown-code-runner not support multiple "presets" for a given language?

jfly avatar Jul 23 '25 18:07 jfly

I don't see the problem here. Does markdown-code-runner not support multiple "presets" for a given language?

What do you mean?

drupol avatar Jul 25 '25 13:07 drupol

@drupol, I just don't understand what conflicts @wolfgangwalther is alluding to in this comment:

Also, this could lead to conflicts, if multiple modules define the same language.

jfly avatar Jul 25 '25 18:07 jfly

We have msdh who is quite close to this.

zimbatm avatar Jul 25 '25 19:07 zimbatm

I just don't understand what conflicts @wolfgangwalther is alluding to in this comment:

Also, this could lead to conflicts, if multiple modules define the same language.

It seems like the config format for markdown-code-runner allows multiple presets for the same language, so maybe that's possible in theory. I'm not sure whether it would work well in practice, because it seems like these different formatters can be ordered explicitly - I think they might be run in whatever order they appear in the config's hashmap.

In any case, this rules out my proposal of defining languages = { nix = /* single formatter only here* /; } on the markdown-code-runner config.


I like this idea. I'm pretty sure we'd need to support multiple languages, though: markdown-languages = ["nix"];

I think I prefer this as well now.

wolfgangwalther avatar Aug 05 '25 15:08 wolfgangwalther