Fix for subdirectories related attachments (issue #31) (#151)

This commit is contained in:
klysunkin 2022-01-18 09:07:36 +03:00 committed by GitHub
parent 8d58ff26a3
commit 9a7146c7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 15 deletions

View File

@ -313,7 +313,7 @@ func processFile(
target = page
}
attaches, err := mark.ResolveAttachments(api, target, ".", meta.Attachments)
attaches, err := mark.ResolveAttachments(api, target, filepath.Dir(file), meta.Attachments)
if err != nil {
log.Fatalf(err, "unable to create/update attachments")
}

View File

@ -35,17 +35,14 @@ func ResolveAttachments(
api *confluence.API,
page *confluence.PageInfo,
base string,
replacements map[string]string,
replacements []string,
) ([]Attachment, error) {
attaches := []Attachment{}
for replace, name := range replacements {
attach := Attachment{
Name: name,
Filename: strings.ReplaceAll(name, "/", "_"),
Path: filepath.Join(base, name),
Replace: replace,
}
attaches, err := prepareAttachments(base, replacements)
if err != nil {
return nil, err
}
for _, attach := range attaches {
checksum, err := getChecksum(attach.Path)
if err != nil {
return nil, karma.Format(
@ -55,8 +52,6 @@ func ResolveAttachments(
}
attach.Checksum = checksum
attaches = append(attaches, attach)
}
remotes, err := api.GetAttachments(page.ID)
@ -160,6 +155,22 @@ func ResolveAttachments(
return attaches, nil
}
func prepareAttachments(base string, replacements []string) ([]Attachment, error) {
attaches := []Attachment{}
for _, name := range replacements {
attach := Attachment{
Name: name,
Filename: strings.ReplaceAll(name, "/", "_"),
Path: filepath.Join(base, name),
Replace: name,
}
attaches = append(attaches, attach)
}
return attaches, nil
}
func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
links := map[string]string{}
replaces := []string{}

View File

@ -0,0 +1,55 @@
package mark
import (
"github.com/stretchr/testify/assert"
"testing"
)
var (
replacements = []string{
"image1.jpg",
"images/image2.jpg",
"../image3.jpg",
}
)
func TestPrepareAttachmentsWithWorkDirBase(t *testing.T) {
attaches, err := prepareAttachments(".", replacements)
if err != nil {
println(err.Error())
}
assert.Equal(t, "image1.jpg", attaches[0].Name)
assert.Equal(t, "image1.jpg", attaches[0].Replace)
assert.Equal(t, "image1.jpg", attaches[0].Path)
assert.Equal(t, "images/image2.jpg", attaches[1].Name)
assert.Equal(t, "images/image2.jpg", attaches[1].Replace)
assert.Equal(t, "images/image2.jpg", attaches[1].Path)
assert.Equal(t, "../image3.jpg", attaches[2].Name)
assert.Equal(t, "../image3.jpg", attaches[2].Replace)
assert.Equal(t, "../image3.jpg", attaches[2].Path)
assert.Equal(t, len(attaches), 3)
}
func TestPrepareAttachmentsWithSubDirBase(t *testing.T) {
attaches, _ := prepareAttachments("a/b", replacements)
assert.Equal(t, "image1.jpg", attaches[0].Name)
assert.Equal(t, "image1.jpg", attaches[0].Replace)
assert.Equal(t, "a/b/image1.jpg", attaches[0].Path)
assert.Equal(t, "images/image2.jpg", attaches[1].Name)
assert.Equal(t, "images/image2.jpg", attaches[1].Replace)
assert.Equal(t, "a/b/images/image2.jpg", attaches[1].Path)
assert.Equal(t, "../image3.jpg", attaches[2].Name)
assert.Equal(t, "../image3.jpg", attaches[2].Replace)
assert.Equal(t, "a/image3.jpg", attaches[2].Path)
assert.Equal(t, len(attaches), 3)
}

View File

@ -29,7 +29,7 @@ type Meta struct {
Title string
Layout string
Sidebar string
Attachments map[string]string
Attachments []string
Labels []string
}
@ -72,7 +72,6 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
if meta == nil {
meta = &Meta{}
meta.Type = "page" //Default if not specified
meta.Attachments = make(map[string]string)
}
header := strings.Title(matches[1])
@ -103,7 +102,7 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
meta.Sidebar = strings.TrimSpace(value)
case HeaderAttachment:
meta.Attachments[value] = value
meta.Attachments = append(meta.Attachments, value)
case HeaderLabel:
meta.Labels = append(meta.Labels, value)