MatthewJohn / terrareg

Open source Terraform module registry with UI, optional Git integration and deep analysis
https://gitlab.dockstudios.co.uk/pub/terrareg
GNU General Public License v3.0
268 stars 20 forks source link

Feature Request: syntax highlighting of code blocks #53

Closed freakinhippie closed 2 months ago

freakinhippie commented 3 months ago

It would be amazing if markdown fenced code blocks supported syntax highlighting throughout the UI, such as in the tabs [Readme], [Inputs], [Outputs], etc. The [Usage] field uses syntax highlighting on the generated terraform. Using the same rules in other places would be great.

As a bonus, supporting syntax highlighting of not only terraform code, but other types of code that might be commonly encountered in terraform would be useful as well. Languages and/or formats such as YAML, JSON, python, javascript, etc.

MatthewJohn commented 3 months ago

So..... The README is actually run through the syntax highlighter.. However, I've seen it's not fantastic at detecting languages. For things like example files and such, where there's 100% terraform, it's forced to use HCL syntax highlighting. For the README, it's not (because of the exact fact that it might contain other languages).. so it is enabled.. but unfortunately, if it doesn't detect the language, it doesn't get highlighted Having said that, now I think of it, I've certainly seen things that should look like HCL and it doesn't do it.. so I'll double check

MatthewJohn commented 3 months ago

Created gitlab issue: https://gitlab.dockstudios.co.uk/pub/terrareg/-/issues/529 gitlab-issue-id:529

freakinhippie commented 3 months ago

Does it attempt to detect the language by some means of parsing the fenced block, or by using the fence hint (or whatever it is called?

For example, if the code looks like:

resource "aws_instance" "server" { provider = aws.custom

name = "srv01"

other inputs

}

Does that mean that the parser attempts to detect that is HCL code and apply highlighting? Alternatively, if the code includes the hint:

```terraform
resource "aws_instance" "server" {
  provider = aws.custom

  name = "srv01"
  # other inputs
}
resource "aws_instance" "server" {
  provider = aws.custom

  name = "srv01"
  # other inputs
}


...does that change the behaviour?
MatthewJohn commented 3 months ago

Unfortunately, it still seems to have an issue:

Current: Screenshot from 2024-06-14 17-04-52

Forcing HCL: Screenshot from 2024-06-14 17-04-42

freakinhippie commented 3 months ago

I would be wary of forcing HCL, unless there is a fence hint specifying hcl or terraform. Personally, I frequently have other languages as fenced blocks that would not be parsed properly if the highlighting were forced to HCL for all code blocks.

MatthewJohn commented 3 months ago

Hey @freakinhippie ,

I've managed to find why it wasn't working. Created a PR: https://gitlab.dockstudios.co.uk/pub/terrareg/-/merge_requests/408 Available at: https://terrareg-529-feature-request-syntax-highlighting-of-cod.gitlab-pr.dockstudios.co.uk/modules/test/example/myfirstmodule

Matt

freakinhippie commented 3 months ago

I'm not sure if this is built on top of the 3.6.0 release, but if not there seems to be a regression in the markdown table handling of inputs/outputs. See here

I see the syntax highlighting in the [Readme] section, but were you planning to add it to the Type and Default sections of [Inputs]? Also, in the [Readme] section there are two identical terraform blocks with one highlighted and one not. I presume that is because the first one (without highlighting) doesn't have the fence block hint. If that is the case, would you please specify which languages/formats will be supported, and what their hint should specifically be (eg terraform vs. hcl), somewhere in the documentation?

Thanks again! This is great progress.

MatthewJohn commented 3 months ago

I'm not sure if this is built on top of the 3.6.0 release, but if not there seems to be a regression in the markdown table handling of inputs/outputs. See here

Hmm, I did change the Terraform for that demo module, but locally it seems to work fine. I'm creating a demo environment for main to see if there's an issue with the docker version of the app

I see the syntax highlighting in the [Readme] section, but were you planning to add it to the Type and Default sections of [Inputs]? Also, in the [Readme] section there are two identical terraform blocks with one highlighted and one not. I presume that is because the first one (without highlighting) doesn't have the fence block hint. If that is the case, would you please specify which languages/formats will be supported, and what their hint should specifically be (eg terraform vs. hcl), somewhere in the documentation?

These are the ones from yours above, it seems the syntax highlighting library only support hcl tag - I'll find somewhere to document this (perhaps in https://matthewjohn.github.io/terrareg/modules/best_practices/)

MatthewJohn commented 3 months ago

I've found the issue - it was a bug in the original implementation! Fixed here: https://gitlab.dockstudios.co.uk/pub/terrareg/-/merge_requests/410 I'll get that merged, get it into this branch and get the docs updated

Matt

freakinhippie commented 3 months ago

Wonderful! Thank you!

MatthewJohn commented 2 months ago

This is now released :) https://github.com/MatthewJohn/terrareg/releases/tag/v3.7.0