Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser/renderer): support 'target' and 'role' attributes in link #810

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 57 additions & 21 deletions pkg/parser/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,71 @@ var _ = Describe("links", func() {

Context("draft documents", func() {

It("link with special characters", func() {
source := `a link to https://example.com?a=1&b=2`
expected := types.DraftDocument{
Elements: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{types.StringElement{Content: "a link to "},
types.InlineLink{
Location: types.Location{
Scheme: "https://",
Path: []interface{}{
types.StringElement{
Content: "example.com?a=1",
},
types.SpecialCharacter{
Name: "&",
Context("external link", func() {

It("with special characters", func() {
source := `a link to https://example.com?a=1&b=2`
expected := types.DraftDocument{
Elements: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{types.StringElement{Content: "a link to "},
types.InlineLink{
Location: types.Location{
Scheme: "https://",
Path: []interface{}{
types.StringElement{
Content: "example.com?a=1",
},
types.SpecialCharacter{
Name: "&",
},
types.StringElement{
Content: "b=2",
},
},
types.StringElement{
Content: "b=2",
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})

Context("relative link", func() {

It("with text, target and role", func() {
source := `a link to link:https://example.com[example,window=mytarget,role=myrole]`
expected := types.DraftDocument{
Elements: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{types.StringElement{Content: "a link to "},
types.InlineLink{
Attributes: types.Attributes{
types.AttrInlineLinkText: "example",
types.AttrInlineLinkTarget: "mytarget",
types.AttrRoles: []interface{}{"myrole"},
},
Location: types.Location{
Scheme: "https://",
Path: []interface{}{
types.StringElement{
Content: "example.com",
},
},
},
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/html5/link.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package html5

const (
linkTmpl = `<a href="{{ .URL }}"{{if .Class}} class="{{ .Class }}"{{ end }}>{{ .Text }}</a>`
linkTmpl = `<a href="{{ .URL }}"{{if .Class}} class="{{ .Class }}"{{ end }}{{if .Target}} target="{{ .Target }}"{{ end }}>{{ .Text }}</a>`
)
36 changes: 36 additions & 0 deletions pkg/renderer/sgml/html5/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ next lines</p>
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with text, target and role", func() {
source := `a link to https://example.com[example,window=mytarget,role=myrole]`
expected := `<div class="paragraph">
<p>a link to <a href="https://example.com" class="myrole" target="mytarget">example</a></p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with target and role", func() {
source := `a link to https://example.com[window=mytarget,role=myrole]`
expected := `<div class="paragraph">
<p>a link to <a href="https://example.com" class="bare myrole" target="mytarget">https://example.com</a></p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

Context("with document attribute substitutions", func() {

It("with a document attribute substitution for the whole URL", func() {
Expand Down Expand Up @@ -227,6 +245,24 @@ a link to {scheme}://{path} and https://foo.baz`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with text, target and role", func() {
source := `a link to link:https://example.com[example,window=mytarget,role=myrole]`
expected := `<div class="paragraph">
<p>a link to <a href="https://example.com" class="myrole" target="mytarget">example</a></p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with target and role", func() {
source := `a link to link:https://example.com[window=mytarget,role=myrole]`
expected := `<div class="paragraph">
<p>a link to <a href="https://example.com" class="bare myrole" target="mytarget">https://example.com</a></p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

Context("with document attribute substitutions", func() {

It("relative link with two document attribute substitutions and a reset", func() {
Expand Down
28 changes: 19 additions & 9 deletions pkg/renderer/sgml/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (r *sgmlRenderer) renderLink(ctx *renderer.Context, l types.InlineLink) (st
location := l.Location.Stringify()
text := ""
class := ""
roles, err := r.renderElementRoles(ctx, l.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render link")
}
// TODO; support `mailto:` positional attributes
if t, exists := l.Attributes[types.AttrInlineLinkText]; exists {
switch t := t.(type) {
Expand All @@ -26,19 +30,25 @@ func (r *sgmlRenderer) renderLink(ctx *renderer.Context, l types.InlineLink) (st
return "", errors.Wrap(err, "unable to render link")
}
}
class = roles // can be empty (and it's fine)
} else {
class = "bare"
text = html.EscapeString(location)
if len(roles) > 0 {
class = "bare " + roles
} else {
class = "bare"
}
}

err := r.link.Execute(result, struct {
URL string
Text string
Class string
err = r.link.Execute(result, struct {
URL string
Text string
Class string
Target string
}{
URL: location,
Text: text,
Class: class,
URL: location,
Text: text,
Class: class,
Target: l.Attributes.GetAsStringWithDefault(types.AttrInlineLinkTarget, ""),
})
if err != nil {
return "", errors.Wrap(err, "unable to render link")
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ const (
AttrStyle = "style"
// AttrInlineLinkText the text attribute (first positional) of links
AttrInlineLinkText = "text"
// AttrInlineLinkTarget the 'window' attribute
AttrInlineLinkTarget = "window"
// AttrWidth the `width` attribute used ior images, tables, and so forth
AttrWidth = "width"
// AttrFrame the frame used mostly for tables (all, topbot, sides, none)
Expand Down