Skip to content

Commit

Permalink
Make module source urls clickable in Markdown output
Browse files Browse the repository at this point in the history
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 `[email protected]:...`, 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 "/".
  • Loading branch information
tomasaschan committed Oct 1, 2021
1 parent ba42be1 commit 1c0ad4f
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 66 deletions.
2 changes: 1 addition & 1 deletion format/templates/markdown_document_modules.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{{ indent 1 "#" }} {{ anchorNameMarkdown "module" .Name }}

Source: {{ .Source }}
Source: [{{ .Source }}]({{ moduleURL .Source }})

Version: {{ .Version }}

Expand Down
2 changes: 1 addition & 1 deletion format/templates/markdown_table_modules.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-Base.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-EscapeCharacters.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-IndentationOfFour.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-OnlyModulecalls.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-WithAnchor.golden
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ The following Modules are called:

### <a name="module_bar"></a> [bar](#module_bar)

Source: baz
Source: [baz](baz)

Version: 4.5.6

### <a name="module_foo"></a> [foo](#module_foo)

Source: bar
Source: [bar](bar)

Version: 1.2.3

### <a name="module_baz"></a> [baz](#module_baz)

Source: baz
Source: [baz](baz)

Version: 4.5.6

### <a name="module_foobar"></a> [foobar](#module_foobar)

Source: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-WithRequired.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/document-WithoutHTML.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ The following Modules are called:

### <a name="module_bar"></a> [bar](#module_bar)

Source: baz
Source: [baz](baz)

Version: 4.5.6

### <a name="module_foo"></a> [foo](#module_foo)

Source: bar
Source: [bar](bar)

Version: 1.2.3

### <a name="module_baz"></a> [baz](#module_baz)

Source: baz
Source: [baz](baz)

Version: 4.5.6

### <a name="module_foobar"></a> [foobar](#module_foobar)

Source: [email protected]:module/path
Source: [[email protected]:module/path](https://github.com/module/path)

Version: v7.8.9

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-Base.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-EscapeCharacters.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-IndentationOfFour.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

#### Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-OnlyModulecalls.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| Name | Source | Version |
|------|--------|---------|
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |
| foobar | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-WithAnchor.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ followed by another line of text.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_bar"></a> [bar](#module_bar) | baz | 4.5.6 |
| <a name="module_foo"></a> [foo](#module_foo) | bar | 1.2.3 |
| <a name="module_baz"></a> [baz](#module_baz) | baz | 4.5.6 |
| <a name="module_foobar"></a> [foobar](#module_foobar) | [email protected]:module/path | v7.8.9 |
| <a name="module_bar"></a> [bar](#module_bar) | [baz](baz) | 4.5.6 |
| <a name="module_foo"></a> [foo](#module_foo) | [bar](bar) | 1.2.3 |
| <a name="module_baz"></a> [baz](#module_baz) | [baz](baz) | 4.5.6 |
| <a name="module_foobar"></a> [foobar](#module_foobar) | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-WithRequired.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-WithoutHTML.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [email protected]:module/path | v7.8.9 |
| bar | [baz](baz) | 4.5.6 |
| foo | [bar](bar) | 1.2.3 |
| baz | [baz](baz) | 4.5.6 |
| foobar | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
8 changes: 4 additions & 4 deletions format/testdata/markdown/table-WithoutHTMLWithAnchor.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ followed by another line of text.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_bar"></a> [bar](#module_bar) | baz | 4.5.6 |
| <a name="module_foo"></a> [foo](#module_foo) | bar | 1.2.3 |
| <a name="module_baz"></a> [baz](#module_baz) | baz | 4.5.6 |
| <a name="module_foobar"></a> [foobar](#module_foobar) | [email protected]:module/path | v7.8.9 |
| <a name="module_bar"></a> [bar](#module_bar) | [baz](baz) | 4.5.6 |
| <a name="module_foo"></a> [foo](#module_foo) | [bar](bar) | 1.2.3 |
| <a name="module_baz"></a> [baz](#module_baz) | [baz](baz) | 4.5.6 |
| <a name="module_foobar"></a> [foobar](#module_foobar) | [[email protected]:module/path](https://github.com/module/path) | v7.8.9 |

## Resources

Expand Down
11 changes: 11 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
}
}

Expand Down

0 comments on commit 1c0ad4f

Please sign in to comment.