Skip to content

Commit fc3ad7b

Browse files
Warren BuckleyMatthew-Wise
andcommitted
Adds in Matt Wise work submitted as an issues as opposed to a PR in #32
Co-authored-by: Matthew Wise <[email protected]>
1 parent e18c434 commit fc3ad7b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System.Threading.Tasks;
3+
using Umbraco.Cms.Core.Models;
4+
5+
namespace Our.Umbraco.TagHelpers
6+
{
7+
[HtmlTargetElement("our-link")]
8+
public class LinkTagHelper : TagHelper
9+
{
10+
[HtmlAttributeName("Link")]
11+
public Link? Link { get; set; }
12+
13+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
14+
{
15+
// Ensure we have a Url set on the LinkPicker property editor in Umbraco
16+
if (string.IsNullOrWhiteSpace(Link?.Url))
17+
{
18+
output.SuppressOutput();
19+
return;
20+
}
21+
22+
output.TagName = "a";
23+
var childContent = await output.GetChildContentAsync();
24+
25+
// If we use the TagHelper <umb-link></umb-link>
26+
// Without child DOM elements then it will use the Link Name property inside the <a> it generates
27+
if (childContent == null)
28+
{
29+
output.Content.SetContent(Link.Name);
30+
}
31+
32+
// Set the HREF of the <a>
33+
output.Attributes.SetAttribute("href", Link.Url);
34+
35+
if (string.IsNullOrWhiteSpace(Link.Target))
36+
{
37+
return;
38+
}
39+
40+
// Set the <a target=""> attribute such as _blank etc...
41+
output.Attributes.SetAttribute("target", Link.Target);
42+
43+
// If the target is _blank & not an internal picked content node & external
44+
// Ensure we set the <a rel="noopener"> attribute
45+
if (Link.Target == "_blank" && Link.Type == LinkType.External)
46+
{
47+
output.Attributes.SetAttribute("rel", "noopener");
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)