I am trying to make a link rewriter which will open all external links(eg: google.com, facebook.com) in a new tab by default and all internal links(/content/....) in same tab.
I am not sure how should I proceed for that. Need help
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Ronnie09 ,
We use Siglhty for Markup Rendering, Most of the manipulation or condition checks we do in JavaScript Code or Java Backend Code.
You can proceed with the below approach:
Hope this could help you !!!
Thanks
Shiv
Hi @Ronnie09 ,
We use Siglhty for Markup Rendering, Most of the manipulation or condition checks we do in JavaScript Code or Java Backend Code.
You can proceed with the below approach:
Hope this could help you !!!
Thanks
Shiv
Hi @Ronnie09
Another option to could be you write your own Transformer (org.apache.sling.rewriter.Transformer
) to update the links accordingly. However I still prefer doing it in sightly.
Thanks
Dipti
Here here is the example
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomLinkChecker.java
Note: This is a global link rewriter, please restrict the link rewriter to your project itself using rewrite config.
Hi @arunpatidar
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomLinkChecker.java
Using is java class is enough or do I have to do something else?
Can you please guide how can I add target=_blank for external link
Yes java is enough , you need to check if link is external and there is no target attribute and add target attribute at https://github.com/arunpatidar02/aem63app-repo/blob/f99cdbdd649423d8ee4bdbeafe0e840bdf99c97c/java/Cu...
You also configure the pipeline at /apps/yourapp/config/rewriter, there you need to mentioned the pipeline name and path and other properties.
Below is the example of non global CustmLinkChecker
Hi @arunpatidar
How can I set target=_blank. I tried but it is asking for index not sure what to give and attributeimpl doesn't have target.
Hi,
You need to add target attribute to the attribute list
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { final AttributesImpl attributes = new AttributesImpl(atts);
final int index =attributes.getLength(); final String href = attributes.getValue("href"); if (href != null && href.startsWith("/content/aemlab")) { final String target = attributes.getValue("target"); if(target == null){
attributes.setAttribute(index, "","target","target", "CDATA","_blank"); } } contentHandler.startElement(uri, localName, qName, attributes); }
With above code I am getting arrayIndexout of bound exception
I tried with hrefindex but href is overwritten.
Hi,
you need to use addAttribute method but not the set
public void addAttribute(String uri, String localName, String qName, String type, String value)
if(target == null){ attributes.setAttribute( "","target","target", "CDATA","_blank"); }
Views
Likes
Replies
Views
Likes
Replies