gomarkdoc icon indicating copy to clipboard operation
gomarkdoc copied to clipboard

Generate wrong index for go 1.18 generic code

Open chen3feng opened this issue 2 years ago • 3 comments

eg:

type DList [func NewDListT any *DList[T]](<#func-newdlist>)

See the following link for more examples:

https://github.com/chen3feng/contalgo/blob/decca4ca119775b6bed62a1ef5b35e76696f17f3/README.md#index

Thanks.

chen3feng avatar Aug 01 '22 06:08 chen3feng

Screenshot: image

Generated markdown:

- [type Stack](<#type-stack>)
  - [func New[T any]() Stack[T]](<#func-new>)            <-- wrong output
  - [func (s *Stack[T]) Clear()](<#func-stackt-clear>)
  - [func (s *Stack[T]) Contains(item T) bool](<#func-stackt-contains>)

MarvinJWendt avatar Oct 08 '22 21:10 MarvinJWendt

There is probably more wrong markdown generated here than just the line you point out. Looking at the other links generated, they rather tend to work by accident, because [...] is not immediately followed by (...). I'm afraid the whole markdown thing is falling apart in light of Go generics, as there is no common markdown way to escape square/rounded brackets in order to not totally confuse markdown interpreters in case of Go generics signatures.

thediveo avatar Oct 13 '22 12:10 thediveo

It's possible to have that kind of links looking fine if you put them into a code block:

- [`func New[T any]() Stack[T]`](https://example.com)

That would change the look slightly, but it works.

EDIT: I just found a way to escape the links:

- [func New[T any]\() Stack[T]](https://example.com)

So the solution is to put a backslash between [] and (). @princjef do you have any ETA on this?

MarvinJWendt avatar Oct 13 '22 15:10 MarvinJWendt

@princjef any update on this?

MarvinJWendt avatar Jan 05 '23 21:01 MarvinJWendt

One fix would be to replace []() with their html code counterpart:

r := strings.NewReplacer(
	"[", "&#91;",
	"]", "&#93;",
	"(", "&#40;",
	")", "&#41;",
)

https://go.dev/play/p/MMr04QkvnTW -- although, that would be really annoying when reading in an editor.

But, yeah, the strings.ReplaceAll(s, "](", "]\(") seems like a nicer output.

Also, there's an option to replace them with some similar unicode-symbol, e.g. https://symbl.cc/en/27E6/.

egonelbre avatar Apr 07 '23 09:04 egonelbre

Gomarkdoc does some general escaping with \ which generally solves this problem, I think it's just being skipped here (accidental omission). Let me see if applying that escaping is enough without messing up the rendering. Raw readability of the code will suffer, but up until now I've favored proper rendering over maximum readability of the raw code

princjef avatar Jun 11 '23 22:06 princjef