| // Copyright 2022 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package golang |
| |
| import ( |
| "fmt" |
| "go/doc/comment" |
| |
| "golang.org/x/tools/gopls/internal/settings" |
| ) |
| |
| // CommentToMarkdown converts comment text to formatted markdown. |
| // The comment was prepared by DocReader, |
| // so it is known not to have leading, trailing blank lines |
| // nor to have trailing spaces at the end of lines. |
| // The comment markers have already been removed. |
| func CommentToMarkdown(text string, options *settings.Options) string { |
| var p comment.Parser |
| doc := p.Parse(text) |
| var pr comment.Printer |
| // The default produces {#Hdr-...} tags for headings. |
| // vscode displays thems, which is undesirable. |
| // The godoc for comment.Printer says the tags |
| // avoid a security problem. |
| pr.HeadingID = func(*comment.Heading) string { return "" } |
| pr.DocLinkURL = func(link *comment.DocLink) string { |
| msg := fmt.Sprintf("https://%s/%s", options.LinkTarget, link.ImportPath) |
| if link.Name != "" { |
| msg += "#" |
| if link.Recv != "" { |
| msg += link.Recv + "." |
| } |
| msg += link.Name |
| } |
| return msg |
| } |
| easy := pr.Markdown(doc) |
| return string(easy) |
| } |