From c4d83e2a551cc7d86c799a8dab99612e307a44ea Mon Sep 17 00:00:00 2001 From: guowei shieh Date: Sat, 11 Dec 2021 20:38:00 -0800 Subject: [PATCH 1/2] add support --- main.go | 56 +++++++++++++++++++++++++++++++----------- pkg/confluence/api.go | 27 ++++++++++++++++++++ pkg/mark/attachment.go | 11 +++++++++ 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 0f44a6c1..c6ed6070 100644 --- a/main.go +++ b/main.go @@ -19,21 +19,22 @@ import ( ) type Flags struct { - FileGlobPatten string `docopt:"-f"` - CompileOnly bool `docopt:"--compile-only"` - DryRun bool `docopt:"--dry-run"` - EditLock bool `docopt:"-k"` - DropH1 bool `docopt:"--drop-h1"` - MinorEdit bool `docopt:"--minor-edit"` - Color string `docopt:"--color"` - Debug bool `docopt:"--debug"` - Trace bool `docopt:"--trace"` - Username string `docopt:"-u"` - Password string `docopt:"-p"` - TargetURL string `docopt:"-l"` - BaseURL string `docopt:"--base-url"` - Config string `docopt:"--config"` - Ci bool `docopt:"--ci"` + FileGlobPatten string `docopt:"-f"` + CompileOnly bool `docopt:"--compile-only"` + DryRun bool `docopt:"--dry-run"` + EditLock bool `docopt:"-k"` + DropH1 bool `docopt:"--drop-h1"` + MinorEdit bool `docopt:"--minor-edit"` + Color string `docopt:"--color"` + Debug bool `docopt:"--debug"` + Trace bool `docopt:"--trace"` + Username string `docopt:"-u"` + Password string `docopt:"-p"` + TargetURL string `docopt:"-l"` + BaseURL string `docopt:"--base-url"` + Config string `docopt:"--config"` + Ci bool `docopt:"--ci"` + PreserveComments bool `docopt:"--preserve-comments"` } const ( @@ -72,6 +73,7 @@ Options: -c --config Use the specified configuration file. [default: $HOME/.config/mark] --ci Runs on CI mode. It won't fail if files are not found. + --preserve-comments Try to preserve the comment from the Confluence page. -h --help Show this message. -v --version Show version. ` @@ -245,6 +247,7 @@ func processFile( ) } + var pageCreated bool var target *confluence.PageInfo if meta != nil { @@ -273,6 +276,7 @@ func processFile( meta.Title, ) } + pageCreated = true } target = page @@ -328,6 +332,28 @@ func processFile( html = buffer.String() } + if flags.PreserveComments && !pageCreated { + comments, err := api.GetInlineComments(target.ID) + if err != nil { + log.Warningf( + err, + "can't fetch comments %s %q", + meta.Type, + meta.Title, + ) + } + + html, err = mark.MergeComments(html, comments) + if err != nil { + log.Warningf( + err, + "can't merge comments %s %q", + meta.Type, + meta.Title, + ) + } + } + err = api.UpdatePage(target, html, flags.MinorEdit, meta.Labels) if err != nil { log.Fatal(err) diff --git a/pkg/confluence/api.go b/pkg/confluence/api.go index 8386d709..059b817d 100644 --- a/pkg/confluence/api.go +++ b/pkg/confluence/api.go @@ -61,6 +61,18 @@ type PageInfo struct { } `json:"_links"` } +type InlineComments struct { + Results []struct { + Extensions struct { + Location string `json:"location"` + InlineProperties struct { + OriginalSelection string `json:"originalSelection"` + MarkerRef string `json:"markerRef"` + } `json:"inlineProperties"` + } `json:"extensions"` + } `json:"results"` +} + type AttachmentInfo struct { Filename string `json:"title"` ID string `json:"id"` @@ -420,6 +432,21 @@ func (api *API) GetPageByID(pageID string) (*PageInfo, error) { return request.Response.(*PageInfo), nil } +func (api *API) GetInlineComments(pageID string) (*InlineComments, error) { + request, err := api.rest.Res( + "content/"+pageID+"/child/comment", &InlineComments{}, + ).Get(map[string]string{"expand": "extensions.inlineProperties", "location": "inline"}) + if err != nil { + return nil, err + } + + if request.Raw.StatusCode != 200 { + return nil, newErrorStatusNotOK(request) + } + + return request.Response.(*InlineComments), nil +} + func (api *API) CreatePage( space string, pageType string, diff --git a/pkg/mark/attachment.go b/pkg/mark/attachment.go index 44f8185b..5d949094 100644 --- a/pkg/mark/attachment.go +++ b/pkg/mark/attachment.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha256" "encoding/hex" + "fmt" "io" "net/url" "os" @@ -160,6 +161,16 @@ func ResolveAttachments( return attaches, nil } +func MergeComments(html string, comments *confluence.InlineComments) (string, error) { + for _, comment := range comments.Results { + selection := comment.Extensions.InlineProperties.OriginalSelection + withComment := fmt.Sprintf(`%s`, + comment.Extensions.InlineProperties.MarkerRef, comment.Extensions.InlineProperties.OriginalSelection) + html = strings.Replace(html, selection, withComment, 1) + } + return html, nil +} + func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte { links := map[string]string{} replaces := []string{} From 68906c2f65b1a5ab5d31fb5f85bd91b8c49f44af Mon Sep 17 00:00:00 2001 From: guowei shieh Date: Sat, 11 Dec 2021 21:12:23 -0800 Subject: [PATCH 2/2] add comment --- pkg/mark/attachment.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/mark/attachment.go b/pkg/mark/attachment.go index 5d949094..e47ed38b 100644 --- a/pkg/mark/attachment.go +++ b/pkg/mark/attachment.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "html" "io" "net/url" "os" @@ -161,14 +162,20 @@ func ResolveAttachments( return attaches, nil } -func MergeComments(html string, comments *confluence.InlineComments) (string, error) { +// The logic of merging comment is the following. Since each comment comes with +// its original selection text, we just search for that and put back the marker. +// Note that this only finds the first instance, so if you put a comment on a +// very common word, like "this", this comment won't be merged correctly. +func MergeComments(body string, comments *confluence.InlineComments) (string, error) { for _, comment := range comments.Results { - selection := comment.Extensions.InlineProperties.OriginalSelection + // TODO: this doesn't handle ' well, as it becomes ‘ + selection := html.EscapeString(comment.Extensions.InlineProperties.OriginalSelection) withComment := fmt.Sprintf(`%s`, - comment.Extensions.InlineProperties.MarkerRef, comment.Extensions.InlineProperties.OriginalSelection) - html = strings.Replace(html, selection, withComment, 1) + comment.Extensions.InlineProperties.MarkerRef, selection) + body = strings.Replace(body, selection, withComment, 1) } - return html, nil + + return body, nil } func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {