Define parent pages from CLI

This commit is contained in:
Manuel Rüger 2023-08-09 13:06:31 +02:00
parent 8b72bc8e20
commit b426d5c6b1
6 changed files with 38 additions and 16 deletions

View File

@ -739,7 +739,7 @@ USAGE:
mark [global options] [arguments...] mark [global options] [arguments...]
VERSION: VERSION:
9.5.2 9.8.0
DESCRIPTION: DESCRIPTION:
Mark is a tool to update Atlassian Confluence pages from markdown. Documentation is available here: https://github.com/kovetskiy/mark 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] --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] --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] --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] --mermaid-provider value defines the mermaid provider to use. Supported options are: cloudscript, mermaid-go. (default: "cloudscript") [$MARK_MERMAID_PROVIDER]
--help, -h show help --help, -h show help
--version, -v print the version --version, -v print the version

4
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7 github.com/urfave/cli/v2 v2.25.7
github.com/yuin/goldmark v1.5.5 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 gopkg.in/yaml.v3 v3.0.1
) )
@ -34,6 +34,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3 // 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 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
) )

8
go.sum
View File

@ -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= 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.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.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= 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 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 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

25
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/kovetskiy/lorg" "github.com/kovetskiy/lorg"
@ -21,7 +22,7 @@ import (
) )
const ( const (
version = "9.7.1" version = "9.8.0"
usage = "A tool for updating Atlassian Confluence pages from markdown." 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` 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.", Usage: "use specified space key. If the space key is not specified, it must be set in the page metadata.",
EnvVars: []string{"MARK_SPACE"}, 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{ altsrc.NewStringFlag(&cli.StringFlag{
Name: "mermaid-provider", Name: "mermaid-provider",
Value: "cloudscript", Value: "cloudscript",
@ -171,9 +184,7 @@ func main() {
}), }),
EnableBashCompletion: true, EnableBashCompletion: true,
HideHelpCommand: true, HideHelpCommand: true,
Action: func(cCtx *cli.Context) error { Action: RunMark,
return RunMark(cCtx)
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -265,7 +276,9 @@ func processFile(
markdown = bytes.ReplaceAll(markdown, []byte("\r\n"), []byte("\n")) 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 { if err != nil {
log.Fatal(err) 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 { if err != nil {
log.Fatalf(err, "unable to resolve relative links") log.Fatalf(err, "unable to resolve relative links")
} }

View File

@ -32,6 +32,7 @@ func ResolveRelativeLinks(
base string, base string,
spaceFromCli string, spaceFromCli string,
titleFromH1 bool, titleFromH1 bool,
parents []string,
) ([]LinkSubstitution, error) { ) ([]LinkSubstitution, error) {
matches := parseLinks(string(markdown)) matches := parseLinks(string(markdown))
@ -44,7 +45,7 @@ func ResolveRelativeLinks(
match.filename, match.filename,
match.hash, match.hash,
) )
resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1) resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1, parents)
if err != nil { if err != nil {
return nil, karma.Format(err, "resolve link: %q", match.full) return nil, karma.Format(err, "resolve link: %q", match.full)
} }
@ -68,6 +69,7 @@ func resolveLink(
link markdownLink, link markdownLink,
spaceFromCli string, spaceFromCli string,
titleFromH1 bool, titleFromH1 bool,
parents []string,
) (string, error) { ) (string, error) {
var result string var result string
@ -102,7 +104,7 @@ func resolveLink(
// This helps to determine if found link points to file that's // This helps to determine if found link points to file that's
// not markdown or have mark required metadata // not markdown or have mark required metadata
linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1) linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents)
if err != nil { if err != nil {
log.Errorf( log.Errorf(
err, err,

View File

@ -46,7 +46,7 @@ var (
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`) reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
) )
func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool) (*Meta, []byte, error) { func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, parents []string) (*Meta, []byte, error) {
var ( var (
meta *Meta meta *Meta
offset int offset int
@ -171,5 +171,10 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool) (*Meta, []b
return nil, data, nil return nil, data, nil
} }
// Prepend parent pages that are defined via the cli flag
if len(parents) > 0 && parents[0] != "" {
meta.Parents = append(parents, meta.Parents...)
}
return meta, data[offset:], nil return meta, data[offset:], nil
} }