From 8e10236be5dd3b43b5a38f041e500dec837b6df5 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Thu, 18 Dec 2014 17:36:46 -0700 Subject: [PATCH] support replacing [refid][] syntax link content with alternate content --- inline.go | 19 ++++++++++++++----- markdown.go | 7 ++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/inline.go b/inline.go index b8b7659..72b64d8 100644 --- a/inline.go +++ b/inline.go @@ -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() diff --git a/markdown.go b/markdown.go index 976f95f..42ae4d1 100644 --- a/markdown.go +++ b/markdown.go @@ -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.