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

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

View File

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