mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
parent
8d58ff26a3
commit
9a7146c7d7
2
main.go
2
main.go
@ -313,7 +313,7 @@ func processFile(
|
|||||||
target = page
|
target = page
|
||||||
}
|
}
|
||||||
|
|
||||||
attaches, err := mark.ResolveAttachments(api, target, ".", meta.Attachments)
|
attaches, err := mark.ResolveAttachments(api, target, filepath.Dir(file), meta.Attachments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err, "unable to create/update attachments")
|
log.Fatalf(err, "unable to create/update attachments")
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,14 @@ func ResolveAttachments(
|
|||||||
api *confluence.API,
|
api *confluence.API,
|
||||||
page *confluence.PageInfo,
|
page *confluence.PageInfo,
|
||||||
base string,
|
base string,
|
||||||
replacements map[string]string,
|
replacements []string,
|
||||||
) ([]Attachment, error) {
|
) ([]Attachment, error) {
|
||||||
attaches := []Attachment{}
|
attaches, err := prepareAttachments(base, replacements)
|
||||||
for replace, name := range replacements {
|
if err != nil {
|
||||||
attach := Attachment{
|
return nil, err
|
||||||
Name: name,
|
}
|
||||||
Filename: strings.ReplaceAll(name, "/", "_"),
|
|
||||||
Path: filepath.Join(base, name),
|
|
||||||
Replace: replace,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for _, attach := range attaches {
|
||||||
checksum, err := getChecksum(attach.Path)
|
checksum, err := getChecksum(attach.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, karma.Format(
|
return nil, karma.Format(
|
||||||
@ -55,8 +52,6 @@ func ResolveAttachments(
|
|||||||
}
|
}
|
||||||
|
|
||||||
attach.Checksum = checksum
|
attach.Checksum = checksum
|
||||||
|
|
||||||
attaches = append(attaches, attach)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
remotes, err := api.GetAttachments(page.ID)
|
remotes, err := api.GetAttachments(page.ID)
|
||||||
@ -160,6 +155,22 @@ func ResolveAttachments(
|
|||||||
return attaches, nil
|
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 {
|
func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
|
||||||
links := map[string]string{}
|
links := map[string]string{}
|
||||||
replaces := []string{}
|
replaces := []string{}
|
||||||
|
55
pkg/mark/attachment_test.go
Normal file
55
pkg/mark/attachment_test.go
Normal 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)
|
||||||
|
}
|
@ -29,7 +29,7 @@ type Meta struct {
|
|||||||
Title string
|
Title string
|
||||||
Layout string
|
Layout string
|
||||||
Sidebar string
|
Sidebar string
|
||||||
Attachments map[string]string
|
Attachments []string
|
||||||
Labels []string
|
Labels []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +72,6 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
|
|||||||
if meta == nil {
|
if meta == nil {
|
||||||
meta = &Meta{}
|
meta = &Meta{}
|
||||||
meta.Type = "page" //Default if not specified
|
meta.Type = "page" //Default if not specified
|
||||||
meta.Attachments = make(map[string]string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
header := strings.Title(matches[1])
|
header := strings.Title(matches[1])
|
||||||
@ -103,7 +102,7 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
|
|||||||
meta.Sidebar = strings.TrimSpace(value)
|
meta.Sidebar = strings.TrimSpace(value)
|
||||||
|
|
||||||
case HeaderAttachment:
|
case HeaderAttachment:
|
||||||
meta.Attachments[value] = value
|
meta.Attachments = append(meta.Attachments, value)
|
||||||
|
|
||||||
case HeaderLabel:
|
case HeaderLabel:
|
||||||
meta.Labels = append(meta.Labels, value)
|
meta.Labels = append(meta.Labels, value)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user