From b426d5c6b119303ae0d8424be96985ecb3adb765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Wed, 9 Aug 2023 13:06:31 +0200 Subject: [PATCH] Define parent pages from CLI --- README.md | 4 +++- go.mod | 4 ++-- go.sum | 8 ++++---- main.go | 25 +++++++++++++++++++------ pkg/mark/link.go | 6 ++++-- pkg/mark/meta.go | 7 ++++++- 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c08e0f1..81c7d87 100644 --- a/README.md +++ b/README.md @@ -739,7 +739,7 @@ USAGE: mark [global options] [arguments...] VERSION: - 9.5.2 + 9.8.0 DESCRIPTION: Mark is a tool to update Atlassian Confluence pages from markdown. Documentation is available here: https://github.com/kovetskiy/mark @@ -762,6 +762,8 @@ GLOBAL OPTIONS: --config value, -c value use the specified configuration file. (default: "~/.config/mark") [$MARK_CONFIG] --ci run on CI mode. It won't fail if files are not found. (default: false) [$MARK_CI] --space value use specified space key. If the space key is not specified, it must be set in the page metadata. [$MARK_SPACE] + --parents value A list containing the parents of the document separated by parents-delimiter (default: '/'). These will be preprended to the ones defined in the document itself. [$MARK_PARENTS] + --parents-delimiter value The delimiter used for the nested parent (default: "/") [$MARK_PARENTS_DELIMITER] --mermaid-provider value defines the mermaid provider to use. Supported options are: cloudscript, mermaid-go. (default: "cloudscript") [$MARK_MERMAID_PROVIDER] --help, -h show help --version, -v print the version diff --git a/go.mod b/go.mod index 441608d..e554667 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 github.com/yuin/goldmark v1.5.5 - golang.org/x/tools v0.11.0 + golang.org/x/tools v0.12.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -34,6 +34,6 @@ require ( github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3 // indirect - golang.org/x/sys v0.10.0 // indirect + golang.org/x/sys v0.11.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index fc28a9e..0301f83 100644 --- a/go.sum +++ b/go.sum @@ -69,10 +69,10 @@ github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3 h1:BhVaeQJc3xalHGONn21 github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3/go.mod h1:NtepZ8TEXErPsmQDMUoN72f8aIy4+xNinSJ3f1giess= golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= -golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= +golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 7297c4b..31c1e5c 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" "github.com/kovetskiy/lorg" @@ -21,7 +22,7 @@ import ( ) const ( - version = "9.7.1" + version = "9.8.0" usage = "A tool for updating Atlassian Confluence pages from markdown." description = `Mark is a tool to update Atlassian Confluence pages from markdown. Documentation is available here: https://github.com/kovetskiy/mark` ) @@ -140,6 +141,18 @@ var flags = []cli.Flag{ Usage: "use specified space key. If the space key is not specified, it must be set in the page metadata.", EnvVars: []string{"MARK_SPACE"}, }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "parents", + Value: "", + Usage: "A list containing the parents of the document separated by parents-delimiter (default: '/'). These will be preprended to the ones defined in the document itself.", + EnvVars: []string{"MARK_PARENTS"}, + }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "parents-delimiter", + Value: "/", + Usage: "The delimiter used for the parents list", + EnvVars: []string{"MARK_PARENTS_DELIMITER"}, + }), altsrc.NewStringFlag(&cli.StringFlag{ Name: "mermaid-provider", Value: "cloudscript", @@ -171,9 +184,7 @@ func main() { }), EnableBashCompletion: true, HideHelpCommand: true, - Action: func(cCtx *cli.Context) error { - return RunMark(cCtx) - }, + Action: RunMark, } if err := app.Run(os.Args); err != nil { @@ -265,7 +276,9 @@ func processFile( markdown = bytes.ReplaceAll(markdown, []byte("\r\n"), []byte("\n")) - meta, markdown, err := mark.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1")) + parents := strings.Split(cCtx.String("parents"), cCtx.String("parents-delimiter")) + + meta, markdown, err := mark.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1"), parents) if err != nil { log.Fatal(err) } @@ -342,7 +355,7 @@ func processFile( } } - links, err := mark.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1")) + links, err := mark.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1"), parents) if err != nil { log.Fatalf(err, "unable to resolve relative links") } diff --git a/pkg/mark/link.go b/pkg/mark/link.go index 0215a8b..c107c64 100644 --- a/pkg/mark/link.go +++ b/pkg/mark/link.go @@ -32,6 +32,7 @@ func ResolveRelativeLinks( base string, spaceFromCli string, titleFromH1 bool, + parents []string, ) ([]LinkSubstitution, error) { matches := parseLinks(string(markdown)) @@ -44,7 +45,7 @@ func ResolveRelativeLinks( match.filename, match.hash, ) - resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1) + resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1, parents) if err != nil { return nil, karma.Format(err, "resolve link: %q", match.full) } @@ -68,6 +69,7 @@ func resolveLink( link markdownLink, spaceFromCli string, titleFromH1 bool, + parents []string, ) (string, error) { var result string @@ -102,7 +104,7 @@ func resolveLink( // This helps to determine if found link points to file that's // not markdown or have mark required metadata - linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1) + linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents) if err != nil { log.Errorf( err, diff --git a/pkg/mark/meta.go b/pkg/mark/meta.go index 160291d..6e3ec02 100644 --- a/pkg/mark/meta.go +++ b/pkg/mark/meta.go @@ -46,7 +46,7 @@ var ( reHeaderPatternMacro = regexp.MustCompile(`