1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +08:00
Files
go-docx/link.go
2023-02-20 22:48:04 +08:00

52 lines
1.2 KiB
Go

package docxlib
import (
"strconv"
"sync/atomic"
)
// when adding an hyperlink we need to store a reference in the relationship field
//
// this func is not thread-safe
func (f *Docx) addLinkRelation(link string) string {
rel := &Relationship{
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))),
Type: REL_HYPERLINK,
Target: link,
TargetMode: REL_TARGETMODE,
}
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel)
return rel.ID
}
// when adding an image we need to store a reference in the relationship field
//
// this func is not thread-safe
func (f *Docx) addImageRelation(m Media) string {
rel := &Relationship{
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))),
Type: REL_IMAGE,
Target: "media/" + m.Name,
}
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel)
return rel.ID
}
// ReferHref gets the url for a reference
func (f *Docx) ReferHref(id string) (href string, err error) {
f.DocRelation.mu.RLock()
defer f.DocRelation.mu.RUnlock()
for _, a := range f.DocRelation.Relationships {
if a.ID == id {
href = a.Target
return
}
}
err = ErrRefIDNotFound
return
}