forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchor.go
42 lines (32 loc) · 1.26 KB
/
anchor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package template
import (
"fmt"
)
// CreateAnchorMarkdown creates HTML anchor for Markdown format.
func CreateAnchorMarkdown(prefix string, value string, anchor bool, escape bool) string {
sanitizedName := SanitizeName(value, escape)
if anchor {
anchorName := fmt.Sprintf("%s_%s", prefix, value)
sanitizedAnchorName := SanitizeName(anchorName, escape)
// the <a> link is purposely not sanitized as this breaks markdown formatting
return fmt.Sprintf("<a name=\"%s\"></a> [%s](#%s)", anchorName, sanitizedName, sanitizedAnchorName)
}
return sanitizedName
}
// CreateAnchorAsciidoc creates HTML anchor for AsciiDoc format.
func CreateAnchorAsciidoc(prefix string, value string, anchor bool, escape bool) string {
sanitizedName := SanitizeName(value, escape)
if anchor {
anchorName := fmt.Sprintf("%s_%s", prefix, value)
sanitizedAnchorName := SanitizeName(anchorName, escape)
return fmt.Sprintf("[[%s]] <<%s,%s>>", sanitizedAnchorName, sanitizedAnchorName, sanitizedName)
}
return sanitizedName
}