From a3580082ea335ae3bec595663d17513e88dc5529 Mon Sep 17 00:00:00 2001 From: Gabriel Glachant Date: Tue, 10 Jan 2023 17:15:29 +0100 Subject: [PATCH] Add flag to force relative URLs. --- cmd/gomarkdoc/command.go | 11 ++++++++++- format/github.go | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/gomarkdoc/command.go b/cmd/gomarkdoc/command.go index 473deed..6bb3bad 100644 --- a/cmd/gomarkdoc/command.go +++ b/cmd/gomarkdoc/command.go @@ -56,6 +56,7 @@ type commandOptions struct { templateFileOverrides map[string]string verbosity int includeUnexported bool + forceRelativeURLs bool check bool embed bool version bool @@ -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") @@ -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", @@ -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 } @@ -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": diff --git a/format/github.go b/format/github.go index 1c8718c..d077efe 100644 --- a/format/github.go +++ b/format/github.go @@ -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) { @@ -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)),