Skip to content

Commit

Permalink
add links to paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
fast-facts committed Feb 14, 2025
1 parent fc5b430 commit 72f52da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

const (
XMLNS = `http://schemas.openxmlformats.org/package/2006/relationships`
HyperLinkStyle = "a1"
HyperLinkStyle = "Hyperlink"
)

const (
Expand Down
54 changes: 25 additions & 29 deletions docx/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,35 +223,31 @@ func (p *Paragraph) GetStyle() (*ctypes.Style, error) {
return style, nil
}

// func (p *Paragraph) AddLink(text string, link string) *Hyperlink {
// rId := p.rootRef.addLinkRelation(link)

// runChildren := []*RunChild{}
// runChildren = append(runChildren, &RunChild{
// InstrText: &text,
// })
// run := &Run{
// Children: runChildren,
// RunProperty: &RunProperty{
// RunStyle: &RunStyle{
// Val: constants.HyperLinkStyle,
// },
// },
// }

// paraChild := &ParagraphChild{
// Run: run,
// }

// hyperLink := &Hyperlink{
// ID: rId,
// }
// hyperLink.Children = append(hyperLink.Children, paraChild)

// p.Children = append(p.Children, &ParagraphChild{Link: hyperLink})

// return hyperLink
// }
func (p *Paragraph) AddLink(text string, link string) *ctypes.Hyperlink {
rId := p.root.Document.addLinkRelation(link)

runChildren := []ctypes.RunChild{}
runChildren = append(runChildren, ctypes.RunChild{
Text: ctypes.TextFromString(text),
})
run := &ctypes.Run{
Children: runChildren,
Property: &ctypes.RunProperty{
Style: &ctypes.CTString{
Val: constants.HyperLinkStyle,
},
},
}

hyperLink := &ctypes.Hyperlink{
ID: rId,
Run: run,
}

p.ct.Children = append(p.ct.Children, ctypes.ParagraphChild{Link: hyperLink})

return hyperLink
}

// AddDrawing adds a new drawing (image) to the Paragraph.
//
Expand Down
Binary file modified templates/default.docx
Binary file not shown.
6 changes: 3 additions & 3 deletions wml/ctypes/para.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type ParagraphChild struct {
}

type Hyperlink struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
// Run Run
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
Run *Run
Children []ParagraphChild
}

Expand Down

0 comments on commit 72f52da

Please sign in to comment.