Add support for mermaid via library

Implementation is largely based on: https://github.com/kovetskiy/mark/pull/167

Co-Authored-By: Manuel Rueger <manuel@rueg.eu>
This commit is contained in:
Dreampuf
2023-04-12 13:43:34 +02:00
committed by Manuel Rüger
parent 88c070f524
commit d9d560eda0
16 changed files with 466 additions and 186 deletions

View File

@@ -8,7 +8,6 @@ import (
"io"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/kovetskiy/gopencils"
@@ -52,7 +51,7 @@ type PageInfo struct {
} `json:"version"`
Ancestors []struct {
Id string `json:"id"`
ID string `json:"id"`
Title string `json:"title"`
} `json:"ancestors"`
@@ -141,7 +140,7 @@ func (api *API) FindRootPage(space string) (*PageInfo, error) {
}
return &PageInfo{
ID: page.Ancestors[0].Id,
ID: page.Ancestors[0].ID,
Title: page.Ancestors[0].Title,
}, nil
}
@@ -158,7 +157,7 @@ func (api *API) FindHomePage(space string) (*PageInfo, error) {
return nil, err
}
if request.Raw.StatusCode == 404 || request.Raw.StatusCode != 200 {
if request.Raw.StatusCode == http.StatusNotFound || request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
@@ -193,7 +192,7 @@ func (api *API) FindPage(
// allow 404 because it's fine if page is not found,
// the function will return nil, nil
if request.Raw.StatusCode != 404 && request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusNotFound && request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
@@ -208,11 +207,11 @@ func (api *API) CreateAttachment(
pageID string,
name string,
comment string,
path string,
reader io.Reader,
) (AttachmentInfo, error) {
var info AttachmentInfo
form, err := getAttachmentPayload(name, comment, path)
form, err := getAttachmentPayload(name, comment, reader)
if err != nil {
return AttachmentInfo{}, err
}
@@ -243,7 +242,7 @@ func (api *API) CreateAttachment(
return info, err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return info, newErrorStatusNotOK(request)
}
@@ -276,11 +275,11 @@ func (api *API) UpdateAttachment(
attachID string,
name string,
comment string,
path string,
reader io.Reader,
) (AttachmentInfo, error) {
var info AttachmentInfo
form, err := getAttachmentPayload(name, comment, path)
form, err := getAttachmentPayload(name, comment, reader)
if err != nil {
return AttachmentInfo{}, err
}
@@ -313,7 +312,7 @@ func (api *API) UpdateAttachment(
return info, err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return info, newErrorStatusNotOK(request)
}
@@ -353,23 +352,12 @@ func (api *API) UpdateAttachment(
return shortResponse, nil
}
func getAttachmentPayload(name, comment, path string) (*form, error) {
func getAttachmentPayload(name, comment string, reader io.Reader) (*form, error) {
var (
payload = bytes.NewBuffer(nil)
writer = multipart.NewWriter(payload)
)
file, err := os.Open(path)
if err != nil {
return nil, karma.Format(
err,
"unable to open file: %q",
path,
)
}
defer file.Close()
content, err := writer.CreateFormFile("file", name)
if err != nil {
return nil, karma.Format(
@@ -378,7 +366,7 @@ func getAttachmentPayload(name, comment, path string) (*form, error) {
)
}
_, err = io.Copy(content, file)
_, err = io.Copy(content, reader)
if err != nil {
return nil, karma.Format(
err,
@@ -436,7 +424,7 @@ func (api *API) GetAttachments(pageID string) ([]AttachmentInfo, error) {
return nil, err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
@@ -459,7 +447,7 @@ func (api *API) GetPageByID(pageID string) (*PageInfo, error) {
return nil, err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
@@ -507,7 +495,7 @@ func (api *API) CreatePage(
return nil, err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
@@ -521,7 +509,7 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ne
if page.Type != "blogpost" && len(page.Ancestors) > 0 {
// picking only the last one, which is required by confluence
oldAncestors = []map[string]interface{}{
{"id": page.Ancestors[len(page.Ancestors)-1].Id},
{"id": page.Ancestors[len(page.Ancestors)-1].ID},
}
}
@@ -547,7 +535,7 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ne
"ancestors": oldAncestors,
"body": map[string]interface{}{
"storage": map[string]interface{}{
"value": string(newContent),
"value": newContent,
"representation": "storage",
},
},
@@ -573,7 +561,7 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ne
return err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return newErrorStatusNotOK(request)
}
@@ -654,7 +642,7 @@ func (api *API) RestrictPageUpdatesCloud(
return err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return newErrorStatusNotOK(request)
}
@@ -685,7 +673,7 @@ func (api *API) RestrictPageUpdatesServer(
return err
}
if request.Raw.StatusCode != 200 {
if request.Raw.StatusCode != http.StatusOK {
return newErrorStatusNotOK(request)
}
@@ -715,13 +703,13 @@ func (api *API) RestrictPageUpdates(
}
func newErrorStatusNotOK(request *gopencils.Resource) error {
if request.Raw.StatusCode == 401 {
if request.Raw.StatusCode == http.StatusUnauthorized {
return errors.New(
"Confluence API returned unexpected status: 401 (Unauthorized)",
)
}
if request.Raw.StatusCode == 404 {
if request.Raw.StatusCode == http.StatusNotFound {
return errors.New(
"Confluence API returned unexpected status: 404 (Not Found)",
)