Skip to content

Commit

Permalink
Define parent pages from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Aug 10, 2023
1 parent 8b72bc8 commit b426d5c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/kovetskiy/lorg"
Expand All @@ -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`
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/mark/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func ResolveRelativeLinks(
base string,
spaceFromCli string,
titleFromH1 bool,
parents []string,
) ([]LinkSubstitution, error) {
matches := parseLinks(string(markdown))

Expand All @@ -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)
}
Expand All @@ -68,6 +69,7 @@ func resolveLink(
link markdownLink,
spaceFromCli string,
titleFromH1 bool,
parents []string,
) (string, error) {
var result string

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion pkg/mark/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
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 (
meta *Meta
offset int
Expand Down Expand Up @@ -171,5 +171,10 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool) (*Meta, []b
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
}

0 comments on commit b426d5c

Please sign in to comment.