gleam-lang / package-interface

📦 Work with Gleam's package interfaces
Apache License 2.0
5 stars 0 forks source link

Undesired space for documentation #4

Open ghivert opened 1 month ago

ghivert commented 1 month ago

Hi!

In the documentation fields, there's an undesired leading space for every line.

Here's an example, from the package interface of the gleam_stdlib right now (append function here):

{
  // That field has undesired spaces
  "documentation": " Creates a new bit array by joining two bit arrays.\n\n ## Examples\n\n ```gleam\n append(to: from_string(\"butter\"), suffix: from_string(\"fly\"))\n // -> from_string(\"butterfly\")\n ```\n",

  // Rest of the definition.
  "deprecation": null,
  "implementations": {
    "gleam": false,
    "uses-erlang-externals": true,
    "uses-javascript-externals": true,
    "can-run-on-erlang": true,
    "can-run-on-javascript": true
  },
  "parameters": [
    {
      "label": "to",
      "type": {
        "kind": "named",
        "name": "BitArray",
        "package": "",
        "module": "gleam",
        "parameters": [

        ]
      }
    },
    {
      "label": "suffix",
      "type": {
        "kind": "named",
        "name": "BitArray",
        "package": "",
        "module": "gleam",
        "parameters": [

        ]
      }
    }
  ],
  "return": {
    "kind": "named",
    "name": "BitArray",
    "package": "",
    "module": "gleam",
    "parameters": [

    ]
  }
}

I suspect it's due to the /// comments removed, but the space between the triple slash and the comments themselves are still there. It's a bug, because the string as-is is not a valid markdown string.

lpil commented 1 month ago

Which parser are you having trouble with? All markdown specs I've read while slowly making a parser say that preceding whitespace is permitted and has no impact on the document.

ghivert commented 1 month ago

Oh! Thanks for your answer!

I'm using Showdown JS, because of habits mainly. It does not support leading space as you can see in the live demo. My bad for that one, I was 100% convinced MD specs forbad leading space, but I cannot find any sources ensuring this behaviour. I think Showdown should be modified.


I'm still wondering about the leading space, for the code part. Let's say the doc is:

" Bla\n\n ```gleam\n\n   io.debug("hello gleam")\n\n ```"

Which translates to

 Bla

 ```gleam
 io.debug("hello gleam")

Can we be sure parsers will get rid of leading spaces in code blocks? Because I think we'd like to have
```gleam
io.debug("hello gleam")

And not

 io.debug("hello gleam")

Is it a parser thing? Should it be handled in the code directly?

lpil commented 1 month ago

I think that is a problem with that specific markdown parser, but it'd still be good to resolve this. We do have some space trimming code that we use when rendering HTML documentation, let's apply that to the module interface docs too.