Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add excludeLinkAngularBrackets option for github markdown format #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add excludeLinkAngularBrackets option for github markdown format
  • Loading branch information
ajatprabha committed Jul 6, 2023
commit d8f34de1641cb952cde2c95414458731750b3c18
44 changes: 27 additions & 17 deletions cmd/gomarkdoc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ type PackageSpec struct {
}

type commandOptions struct {
repository lang.Repo
output string
header string
headerFile string
footer string
footerFile string
format string
tags []string
excludeDirs []string
templateOverrides map[string]string
templateFileOverrides map[string]string
verbosity int
includeUnexported bool
check bool
embed bool
version bool
repository lang.Repo
output string
header string
headerFile string
footer string
footerFile string
format string
tags []string
excludeDirs []string
excludeLinkAngularBrackets bool
templateOverrides map[string]string
templateFileOverrides map[string]string
verbosity int
includeUnexported bool
check bool
embed bool
version bool
}

// Flags populated by goreleaser
Expand Down Expand Up @@ -98,6 +99,7 @@ func buildCommand() *cobra.Command {
opts.footerFile = viper.GetString("footerFile")
opts.tags = viper.GetStringSlice("tags")
opts.excludeDirs = viper.GetStringSlice("excludeDirs")
opts.excludeLinkAngularBrackets = viper.GetBool("excludeLinkAngularBrackets")
opts.repository.Remote = viper.GetString("repository.url")
opts.repository.DefaultBranch = viper.GetString("repository.defaultBranch")
opts.repository.PathFromRoot = viper.GetString("repository.path")
Expand Down Expand Up @@ -205,6 +207,13 @@ func buildCommand() *cobra.Command {
nil,
"List of package directories to ignore when producing documentation.",
)
command.Flags().BoolVarP(
&opts.excludeLinkAngularBrackets,
"exclude-link-angular-brackets",
"",
false,
"Exclude the angular brackets [](<>) from links in the output. Works with the github format only.",
)
command.Flags().CountVarP(
&opts.verbosity,
"verbose",
Expand Down Expand Up @@ -250,6 +259,7 @@ func buildCommand() *cobra.Command {
_ = viper.BindPFlag("footerFile", command.Flags().Lookup("footer-file"))
_ = viper.BindPFlag("tags", command.Flags().Lookup("tags"))
_ = viper.BindPFlag("excludeDirs", command.Flags().Lookup("exclude-dirs"))
_ = viper.BindPFlag("excludeLinkAngularBrackets", command.Flags().Lookup("exclude-link-angular-brackets"))
_ = viper.BindPFlag("repository.url", command.Flags().Lookup("repository.url"))
_ = viper.BindPFlag("repository.defaultBranch", command.Flags().Lookup("repository.default-branch"))
_ = viper.BindPFlag("repository.path", command.Flags().Lookup("repository.path"))
Expand Down Expand Up @@ -367,7 +377,7 @@ func resolveOverrides(opts commandOptions) ([]gomarkdoc.RendererOption, error) {
var f format.Format
switch opts.format {
case "github":
f = &format.GitHubFlavoredMarkdown{}
f = format.NewGitHubFlavoredMarkdown(opts.excludeLinkAngularBrackets)
case "azure-devops":
f = &format.AzureDevOpsMarkdown{}
case "plain":
Expand Down
24 changes: 23 additions & 1 deletion format/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import (
// Flavored Markdown's syntax and semantics. See GitHub's documentation for
// more details about their markdown format:
// https://guides.github.com/features/mastering-markdown/
type GitHubFlavoredMarkdown struct{}
type GitHubFlavoredMarkdown struct {
excludeHrefAngularBrackets bool
}

// NewGitHubFlavoredMarkdown creates a new GitHubFlavoredMarkdown Format.
func NewGitHubFlavoredMarkdown(excludeLinkAngularBrackets bool) *GitHubFlavoredMarkdown {
return &GitHubFlavoredMarkdown{excludeHrefAngularBrackets: excludeLinkAngularBrackets}
}

// Bold converts the provided text to bold
func (f *GitHubFlavoredMarkdown) Bold(text string) (string, error) {
Expand Down Expand Up @@ -82,6 +89,9 @@ func (f *GitHubFlavoredMarkdown) RawLocalHref(anchor string) string {

// Link generates a link with the given text and href values.
func (f *GitHubFlavoredMarkdown) Link(text, href string) (string, error) {
if f.excludeHrefAngularBrackets {
return f.link(text, href), nil
}
return formatcore.Link(text, href), nil
}

Expand Down Expand Up @@ -162,3 +172,15 @@ func (f *GitHubFlavoredMarkdown) AccordionTerminator() (string, error) {
func (f *GitHubFlavoredMarkdown) Escape(text string) string {
return formatcore.Escape(text)
}

func (f *GitHubFlavoredMarkdown) link(text string, href string) string {
if text == "" {
return ""
}

if href == "" {
return text
}

return fmt.Sprintf("[%s](%s)", formatcore.Escape(text), href)
}