Skip to content

Commit

Permalink
Add flag to force relative URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
gglachant committed Jan 10, 2023
1 parent 1569f69 commit a358008
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cmd/gomarkdoc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type commandOptions struct {
templateFileOverrides map[string]string
verbosity int
includeUnexported bool
forceRelativeURLs bool
check bool
embed bool
version bool
Expand Down Expand Up @@ -99,6 +100,7 @@ func buildCommand() *cobra.Command {
opts.repository.Remote = viper.GetString("repository.url")
opts.repository.DefaultBranch = viper.GetString("repository.defaultBranch")
opts.repository.PathFromRoot = viper.GetString("repository.path")
opts.forceRelativeURLs = viper.GetBool("forceRelativeURLs")

if opts.check && opts.output == "" {
return errors.New("gomarkdoc: check mode cannot be run without an output set")
Expand Down Expand Up @@ -221,6 +223,12 @@ func buildCommand() *cobra.Command {
"",
"Manual override for the path from the root of the git repository used in place of automatic detection.",
)
command.Flags().BoolVar(
&opts.forceRelativeURLs,
"force-relative-urls",
false,
"Force relative URLs.",
)
command.Flags().BoolVar(
&opts.version,
"version",
Expand All @@ -244,6 +252,7 @@ func buildCommand() *cobra.Command {
_ = 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"))
_ = viper.BindPFlag("forceRelativeURLs", command.Flags().Lookup("force-relative-urls"))

return command
}
Expand Down Expand Up @@ -351,7 +360,7 @@ func resolveOverrides(opts commandOptions) ([]gomarkdoc.RendererOption, error) {
var f format.Format
switch opts.format {
case "github":
f = &format.GitHubFlavoredMarkdown{}
f = &format.GitHubFlavoredMarkdown{ForceRelativeURLs: opts.forceRelativeURLs}
case "azure-devops":
f = &format.AzureDevOpsMarkdown{}
case "plain":
Expand Down
6 changes: 4 additions & 2 deletions format/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ 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 {
ForceRelativeURLs bool
}

// Bold converts the provided text to bold
func (f *GitHubFlavoredMarkdown) Bold(text string) (string, error) {
Expand Down Expand Up @@ -84,7 +86,7 @@ func (f *GitHubFlavoredMarkdown) CodeHref(loc lang.Location) (string, error) {
}

// If there's no repo, we can only compute a relative href.
if loc.Repo == nil {
if loc.Repo == nil || f.ForceRelativeURLs {
return fmt.Sprintf(
"%s#%s",
filepath.ToSlash(filepath.Base(relative)),
Expand Down

0 comments on commit a358008

Please sign in to comment.