From ced2aed936ee35235ba3b1e4422345d1f672b035 Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Fri, 1 Oct 2021 12:39:21 +0200 Subject: [PATCH 1/3] Make module source urls clickable in Markdown output The main reason this was not already implemented, as outlined in #504 and linked discussions, was that modules sourced from git repositories, with urls like `git@github.com:...`, was rendered by Markdown as URL to an email address, if the url was just shown as `[.source](.source)`. The approach here is to detect if the module source starts with "git@", and, if so, replace that prefix with "https://", as well as replacing the first ":" with a "/". Signed-off-by: Tomas Aschan --- format/templates/markdown_document_modules.tmpl | 2 +- format/templates/markdown_table_modules.tmpl | 2 +- format/testdata/markdown/document-Base.golden | 8 ++++---- .../markdown/document-EscapeCharacters.golden | 8 ++++---- .../markdown/document-IndentationOfFour.golden | 8 ++++---- .../testdata/markdown/document-OnlyModulecalls.golden | 8 ++++---- format/testdata/markdown/document-WithAnchor.golden | 8 ++++---- format/testdata/markdown/document-WithRequired.golden | 8 ++++---- format/testdata/markdown/document-WithoutHTML.golden | 8 ++++---- .../markdown/document-WithoutHTMLWithAnchor.golden | 8 ++++---- format/testdata/markdown/table-Base.golden | 8 ++++---- .../testdata/markdown/table-EscapeCharacters.golden | 8 ++++---- .../testdata/markdown/table-IndentationOfFour.golden | 8 ++++---- format/testdata/markdown/table-OnlyModulecalls.golden | 8 ++++---- format/testdata/markdown/table-WithAnchor.golden | 8 ++++---- format/testdata/markdown/table-WithRequired.golden | 8 ++++---- format/testdata/markdown/table-WithoutHTML.golden | 8 ++++---- .../markdown/table-WithoutHTMLWithAnchor.golden | 8 ++++---- template/template.go | 11 +++++++++++ 19 files changed, 77 insertions(+), 66 deletions(-) diff --git a/format/templates/markdown_document_modules.tmpl b/format/templates/markdown_document_modules.tmpl index fc0a5a93..65254796 100644 --- a/format/templates/markdown_document_modules.tmpl +++ b/format/templates/markdown_document_modules.tmpl @@ -13,7 +13,7 @@ {{ indent 1 "#" }} {{ anchorNameMarkdown "module" .Name }} - Source: {{ .Source }} + Source: [{{ .Source }}]({{ moduleURL .Source }}) Version: {{ .Version }} diff --git a/format/templates/markdown_table_modules.tmpl b/format/templates/markdown_table_modules.tmpl index 891778c2..8f19ad4e 100644 --- a/format/templates/markdown_table_modules.tmpl +++ b/format/templates/markdown_table_modules.tmpl @@ -11,7 +11,7 @@ | Name | Source | Version | |------|--------|---------| {{- range .Module.ModuleCalls }} - | {{ anchorNameMarkdown "module" .Name }} | {{ .Source }} | {{ .Version | default "n/a" }} | + | {{ anchorNameMarkdown "module" .Name }} | [{{ .Source }}]({{ moduleURL .Source }}) | {{ .Version | default "n/a" }} | {{- end }} {{ end }} {{ end -}} \ No newline at end of file diff --git a/format/testdata/markdown/document-Base.golden b/format/testdata/markdown/document-Base.golden index 0d5801c2..3e4371eb 100644 --- a/format/testdata/markdown/document-Base.golden +++ b/format/testdata/markdown/document-Base.golden @@ -68,25 +68,25 @@ The following Modules are called: ### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-EscapeCharacters.golden b/format/testdata/markdown/document-EscapeCharacters.golden index 1203ff24..9c0a6270 100644 --- a/format/testdata/markdown/document-EscapeCharacters.golden +++ b/format/testdata/markdown/document-EscapeCharacters.golden @@ -68,25 +68,25 @@ The following Modules are called: ### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-IndentationOfFour.golden b/format/testdata/markdown/document-IndentationOfFour.golden index f09c3401..096f891f 100644 --- a/format/testdata/markdown/document-IndentationOfFour.golden +++ b/format/testdata/markdown/document-IndentationOfFour.golden @@ -68,25 +68,25 @@ The following Modules are called: ##### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ##### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ##### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ##### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-OnlyModulecalls.golden b/format/testdata/markdown/document-OnlyModulecalls.golden index 3903e55a..8969373c 100644 --- a/format/testdata/markdown/document-OnlyModulecalls.golden +++ b/format/testdata/markdown/document-OnlyModulecalls.golden @@ -4,24 +4,24 @@ The following Modules are called: ### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 \ No newline at end of file diff --git a/format/testdata/markdown/document-WithAnchor.golden b/format/testdata/markdown/document-WithAnchor.golden index da9ac435..16f93199 100644 --- a/format/testdata/markdown/document-WithAnchor.golden +++ b/format/testdata/markdown/document-WithAnchor.golden @@ -68,25 +68,25 @@ The following Modules are called: ### [bar](#module_bar) -Source: baz +Source: [baz](baz) Version: 4.5.6 ### [foo](#module_foo) -Source: bar +Source: [bar](bar) Version: 1.2.3 ### [baz](#module_baz) -Source: baz +Source: [baz](baz) Version: 4.5.6 ### [foobar](#module_foobar) -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-WithRequired.golden b/format/testdata/markdown/document-WithRequired.golden index 555b376e..c41b3811 100644 --- a/format/testdata/markdown/document-WithRequired.golden +++ b/format/testdata/markdown/document-WithRequired.golden @@ -68,25 +68,25 @@ The following Modules are called: ### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-WithoutHTML.golden b/format/testdata/markdown/document-WithoutHTML.golden index 0d5801c2..3e4371eb 100644 --- a/format/testdata/markdown/document-WithoutHTML.golden +++ b/format/testdata/markdown/document-WithoutHTML.golden @@ -68,25 +68,25 @@ The following Modules are called: ### bar -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foo -Source: bar +Source: [bar](bar) Version: 1.2.3 ### baz -Source: baz +Source: [baz](baz) Version: 4.5.6 ### foobar -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/document-WithoutHTMLWithAnchor.golden b/format/testdata/markdown/document-WithoutHTMLWithAnchor.golden index da9ac435..16f93199 100644 --- a/format/testdata/markdown/document-WithoutHTMLWithAnchor.golden +++ b/format/testdata/markdown/document-WithoutHTMLWithAnchor.golden @@ -68,25 +68,25 @@ The following Modules are called: ### [bar](#module_bar) -Source: baz +Source: [baz](baz) Version: 4.5.6 ### [foo](#module_foo) -Source: bar +Source: [bar](bar) Version: 1.2.3 ### [baz](#module_baz) -Source: baz +Source: [baz](baz) Version: 4.5.6 ### [foobar](#module_foobar) -Source: git@github.com:module/path +Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/format/testdata/markdown/table-Base.golden b/format/testdata/markdown/table-Base.golden index 21ef339e..483dc009 100644 --- a/format/testdata/markdown/table-Base.golden +++ b/format/testdata/markdown/table-Base.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/format/testdata/markdown/table-EscapeCharacters.golden b/format/testdata/markdown/table-EscapeCharacters.golden index bb82f742..e47fb53c 100644 --- a/format/testdata/markdown/table-EscapeCharacters.golden +++ b/format/testdata/markdown/table-EscapeCharacters.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/format/testdata/markdown/table-IndentationOfFour.golden b/format/testdata/markdown/table-IndentationOfFour.golden index 19c86fd8..169b2e67 100644 --- a/format/testdata/markdown/table-IndentationOfFour.golden +++ b/format/testdata/markdown/table-IndentationOfFour.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | #### Resources diff --git a/format/testdata/markdown/table-OnlyModulecalls.golden b/format/testdata/markdown/table-OnlyModulecalls.golden index 233c728e..66303249 100644 --- a/format/testdata/markdown/table-OnlyModulecalls.golden +++ b/format/testdata/markdown/table-OnlyModulecalls.golden @@ -2,7 +2,7 @@ | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | \ No newline at end of file +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | \ No newline at end of file diff --git a/format/testdata/markdown/table-WithAnchor.golden b/format/testdata/markdown/table-WithAnchor.golden index a1aaaccc..1d25acfd 100644 --- a/format/testdata/markdown/table-WithAnchor.golden +++ b/format/testdata/markdown/table-WithAnchor.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| [bar](#module_bar) | baz | 4.5.6 | -| [foo](#module_foo) | bar | 1.2.3 | -| [baz](#module_baz) | baz | 4.5.6 | -| [foobar](#module_foobar) | git@github.com:module/path | v7.8.9 | +| [bar](#module_bar) | [baz](baz) | 4.5.6 | +| [foo](#module_foo) | [bar](bar) | 1.2.3 | +| [baz](#module_baz) | [baz](baz) | 4.5.6 | +| [foobar](#module_foobar) | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/format/testdata/markdown/table-WithRequired.golden b/format/testdata/markdown/table-WithRequired.golden index 08ed2ee3..c4f70c41 100644 --- a/format/testdata/markdown/table-WithRequired.golden +++ b/format/testdata/markdown/table-WithRequired.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/format/testdata/markdown/table-WithoutHTML.golden b/format/testdata/markdown/table-WithoutHTML.golden index 6840055f..0fd97d14 100644 --- a/format/testdata/markdown/table-WithoutHTML.golden +++ b/format/testdata/markdown/table-WithoutHTML.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| bar | baz | 4.5.6 | -| foo | bar | 1.2.3 | -| baz | baz | 4.5.6 | -| foobar | git@github.com:module/path | v7.8.9 | +| bar | [baz](baz) | 4.5.6 | +| foo | [bar](bar) | 1.2.3 | +| baz | [baz](baz) | 4.5.6 | +| foobar | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/format/testdata/markdown/table-WithoutHTMLWithAnchor.golden b/format/testdata/markdown/table-WithoutHTMLWithAnchor.golden index dfaadf1d..ff90f5e9 100644 --- a/format/testdata/markdown/table-WithoutHTMLWithAnchor.golden +++ b/format/testdata/markdown/table-WithoutHTMLWithAnchor.golden @@ -59,10 +59,10 @@ followed by another line of text. | Name | Source | Version | |------|--------|---------| -| [bar](#module_bar) | baz | 4.5.6 | -| [foo](#module_foo) | bar | 1.2.3 | -| [baz](#module_baz) | baz | 4.5.6 | -| [foobar](#module_foobar) | git@github.com:module/path | v7.8.9 | +| [bar](#module_bar) | [baz](baz) | 4.5.6 | +| [foo](#module_foo) | [bar](bar) | 1.2.3 | +| [baz](#module_baz) | [baz](baz) | 4.5.6 | +| [foobar](#module_foobar) | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources diff --git a/template/template.go b/template/template.go index ee6d71c8..eb2107de 100644 --- a/template/template.go +++ b/template/template.go @@ -209,6 +209,17 @@ func builtinFuncs(config *print.Config) gotemplate.FuncMap { // nolint:gocyclo "anchorNameAsciidoc": func(prefix string, value string) string { return CreateAnchorAsciidoc(prefix, value, config.Settings.Anchor, config.Settings.Escape) }, + + // resolving module urls + "moduleURL": func(s string) string { + if strings.HasPrefix(s, "git@") { + patched := strings.TrimPrefix(s, "git@") + patched = strings.Replace(patched, ":", "/", 1) + return "https://" + patched + } else { + return s + } + }, } } From 3dbb1a9e1b0e633bbf04b1fc24194760b463eeb6 Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Fri, 1 Oct 2021 18:24:56 +0200 Subject: [PATCH 2/3] Decrease indentation to satisfy linter Signed-off-by: Tomas Aschan --- template/template.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/template/template.go b/template/template.go index eb2107de..ff3d248c 100644 --- a/template/template.go +++ b/template/template.go @@ -212,13 +212,13 @@ func builtinFuncs(config *print.Config) gotemplate.FuncMap { // nolint:gocyclo // resolving module urls "moduleURL": func(s string) string { - if strings.HasPrefix(s, "git@") { - patched := strings.TrimPrefix(s, "git@") - patched = strings.Replace(patched, ":", "/", 1) - return "https://" + patched - } else { + if !strings.HasPrefix(s, "git@") { return s } + + patched := strings.TrimPrefix(s, "git@") + patched = strings.Replace(patched, ":", "/", 1) + return "https://" + patched }, } } From cc9569e46304fa32cce2284090fc6d926d5d7df0 Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Fri, 1 Oct 2021 22:15:48 +0200 Subject: [PATCH 3/3] Update docs Signed-off-by: Tomas Aschan --- docs/reference/markdown-document.md | 8 ++++---- docs/reference/markdown-table.md | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/reference/markdown-document.md b/docs/reference/markdown-document.md index b614611e..b09c92c0 100644 --- a/docs/reference/markdown-document.md +++ b/docs/reference/markdown-document.md @@ -133,25 +133,25 @@ generates the following output: ### [bar](#module\_bar) - Source: baz + Source: [baz](baz) Version: 4.5.6 ### [baz](#module\_baz) - Source: baz + Source: [baz](baz) Version: 4.5.6 ### [foo](#module\_foo) - Source: bar + Source: [bar](bar) Version: 1.2.3 ### [foobar](#module\_foobar) - Source: git@github.com:module/path + Source: [git@github.com:module/path](https://github.com/module/path) Version: v7.8.9 diff --git a/docs/reference/markdown-table.md b/docs/reference/markdown-table.md index f3775a85..6eff4a0b 100644 --- a/docs/reference/markdown-table.md +++ b/docs/reference/markdown-table.md @@ -124,10 +124,10 @@ generates the following output: | Name | Source | Version | |------|--------|---------| - | [bar](#module\_bar) | baz | 4.5.6 | - | [baz](#module\_baz) | baz | 4.5.6 | - | [foo](#module\_foo) | bar | 1.2.3 | - | [foobar](#module\_foobar) | git@github.com:module/path | v7.8.9 | + | [bar](#module\_bar) | [baz](baz) | 4.5.6 | + | [baz](#module\_baz) | [baz](baz) | 4.5.6 | + | [foo](#module\_foo) | [bar](bar) | 1.2.3 | + | [foobar](#module\_foobar) | [git@github.com:module/path](https://github.com/module/path) | v7.8.9 | ## Resources