support replacing [refid][] syntax link content with alternate content

This commit is contained in:
JT Olds 2014-12-18 17:36:46 -07:00
parent 5e8b222b69
commit 8e10236be5
2 changed files with 20 additions and 6 deletions

View File

@ -211,10 +211,10 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
data = data[offset:]
var (
i = 1
noteId int
title, link []byte
textHasNl = false
i = 1
noteId int
title, link, alt_content []byte
textHasNl = false
)
if t == linkDeferredFootnote {
@ -350,6 +350,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// reference style link
case i < len(data) && data[i] == '[':
var id []byte
alt_content_considered := false
// look for the id
i++
@ -379,6 +380,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
id = b.Bytes()
} else {
id = data[1:txtE]
alt_content_considered = true
}
} else {
id = data[linkB:linkE]
@ -394,6 +396,9 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// keep link and title from reference
link = lr.link
title = lr.title
if alt_content_considered {
alt_content = lr.text
}
i++
// shortcut reference style link or reference or inline footnote
@ -503,7 +508,11 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// call the relevant rendering function
switch t {
case linkNormal:
p.r.Link(out, uLink, title, content.Bytes())
if len(alt_content) > 0 {
p.r.Link(out, uLink, title, alt_content)
} else {
p.r.Link(out, uLink, title, content.Bytes())
}
case linkImg:
outSize := out.Len()

View File

@ -222,7 +222,8 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) {
link: []byte(r.Link),
title: []byte(r.Title),
noteId: 0,
hasBlock: false}, true
hasBlock: false,
text: []byte(r.Text)}, true
}
}
// refs are case insensitive
@ -243,6 +244,9 @@ type Reference struct {
Link string
// Title is the alternate text describing the link in more detail.
Title string
// Text is the optional text to override the ref with if the syntax used was
// [refid][]
Text string
}
// ReferenceOverrideFunc is expected to be called with a reference string and
@ -508,6 +512,7 @@ type reference struct {
title []byte
noteId int // 0 if not a footnote ref
hasBlock bool
text []byte
}
// Check whether or not data starts with a reference link.