Skip to content

Commit 085ac67

Browse files
committed
feat: add flag to append hash to pages to ensure unique titles
issue: kovetskiy#450
1 parent d5c41f6 commit 085ac67

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ GLOBAL OPTIONS:
788788
--drop-h1, --h1_drop don't include the first H1 heading in Confluence output. (default: false) [$MARK_H1_DROP]
789789
--strip-linebreaks, -L remove linebreaks inside of tags, to accomodate non-standard Confluence behavior (default: false) [$MARK_STRIP_LINEBREAK]
790790
--title-from-h1, --h1_title extract page title from a leading H1 heading. If no H1 heading on a page exists, then title must be set in the page metadata. (default: false) [$MARK_H1_TITLE]
791+
--title-append-generated-hash appends a short hash generated from the path of the page (space, parents, and title) to the title (default: false) [$MARK_TITLE_APPEND_GENERATED_HASH]
791792
--minor-edit don't send notifications while updating Confluence page. (default: false) [$MARK_MINOR_EDIT]
792793
--version-message value add a message to the page version, to explain the edit (default: "") [$MARK_VERSION_MESSAGE]
793794
--color value display logs in color. Possible values: auto, never. (default: "auto") [$MARK_COLOR]

main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ var flags = []cli.Flag{
8181
Usage: "extract page title from a leading H1 heading. If no H1 heading on a page exists, then title must be set in the page metadata.",
8282
EnvVars: []string{"MARK_H1_TITLE"},
8383
}),
84+
altsrc.NewBoolFlag(&cli.BoolFlag{
85+
Name: "title-append-generated-hash",
86+
Value: false,
87+
Usage: "appends a short hash generated from the path of the page (space, parents, and title) to the title",
88+
EnvVars: []string{"MARK_TITLE_APPEND_GENERATED_HASH"},
89+
}),
8490
altsrc.NewBoolFlag(&cli.BoolFlag{
8591
Name: "minor-edit",
8692
Value: false,
@@ -309,7 +315,7 @@ func processFile(
309315

310316
parents := strings.Split(cCtx.String("parents"), cCtx.String("parents-delimiter"))
311317

312-
meta, markdown, err := metadata.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
318+
meta, markdown, err := metadata.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1"), parents, cCtx.Bool("title-append-generated-hash"))
313319
if err != nil {
314320
log.Fatal(err)
315321
}
@@ -388,7 +394,7 @@ func processFile(
388394
}
389395
}
390396

391-
links, err := page.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
397+
links, err := page.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1"), parents, cCtx.Bool("title-append-generated-hash"))
392398
if err != nil {
393399
log.Fatalf(err, "unable to resolve relative links")
394400
}

metadata/metadata.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package metadata
33
import (
44
"bufio"
55
"bytes"
6+
"crypto/sha256"
7+
"fmt"
68
"regexp"
79
"strings"
810

@@ -44,7 +46,7 @@ var (
4446
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
4547
)
4648

47-
func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, parents []string) (*Meta, []byte, error) {
49+
func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, parents []string, titleAppendGeneratedHash bool) (*Meta, []byte, error) {
4850
var (
4951
meta *Meta
5052
offset int
@@ -164,6 +166,19 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, parents []s
164166
meta.Parents = append(parents, meta.Parents...)
165167
}
166168

169+
// deterministically generate a hash from the page's parents, space, and title
170+
if titleAppendGeneratedHash {
171+
path := strings.Join(append(meta.Parents, meta.Space, meta.Title), "/")
172+
pathHash := sha256.Sum256([]byte(path))
173+
// postfix is an 8-character hexadecimal string representation of the first 4 out of 32 bytes of the hash
174+
meta.Title = fmt.Sprintf("%s - %x", meta.Title, pathHash[0:4])
175+
log.Debugf(
176+
nil,
177+
"appended hash to page title: %s",
178+
meta.Title,
179+
)
180+
}
181+
167182
return meta, data[offset:], nil
168183
}
169184

page/link.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func ResolveRelativeLinks(
3434
spaceFromCli string,
3535
titleFromH1 bool,
3636
parents []string,
37+
titleAppendGeneratedHash bool,
3738
) ([]LinkSubstitution, error) {
3839
matches := parseLinks(string(markdown))
3940

@@ -46,7 +47,7 @@ func ResolveRelativeLinks(
4647
match.filename,
4748
match.hash,
4849
)
49-
resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1, parents)
50+
resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1, parents, titleAppendGeneratedHash)
5051
if err != nil {
5152
return nil, karma.Format(err, "resolve link: %q", match.full)
5253
}
@@ -71,6 +72,7 @@ func resolveLink(
7172
spaceFromCli string,
7273
titleFromH1 bool,
7374
parents []string,
75+
titleAppendGeneratedHash bool,
7476
) (string, error) {
7577
var result string
7678

@@ -105,7 +107,7 @@ func resolveLink(
105107

106108
// This helps to determine if found link points to file that's
107109
// not markdown or have mark required metadata
108-
linkMeta, _, err := metadata.ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents)
110+
linkMeta, _, err := metadata.ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents, titleAppendGeneratedHash)
109111
if err != nil {
110112
log.Errorf(
111113
err,

0 commit comments

Comments
 (0)