Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Link Rewriting

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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:

  • In Sling Model, When we are checking for an internal( appending .html) link or external link and passing it to sightly.
  • We can also pass flags true for internal and false for external to sightly.
  • In Sightly use the data-sly-test tag for checking the condition, If it is true, Provide target ="_self", and for false provide target="_blank".

Hope this could help you !!!

Thanks

Shiv

 

Shiv Prakash

View solution in original post

9 Replies

Avatar

Correct answer by
Community Advisor

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:

  • In Sling Model, When we are checking for an internal( appending .html) link or external link and passing it to sightly.
  • We can also pass flags true for internal and false for external to sightly.
  • In Sightly use the data-sly-test tag for checking the condition, If it is true, Provide target ="_self", and for false provide target="_blank".

Hope this could help you !!!

Thanks

Shiv

 

Shiv Prakash

Avatar

Community Advisor

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

Avatar

Community Advisor

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.

e.g. https://adobe-consulting-services.github.io/acs-aem-commons/features/utils-and-apis/static-reference... 



Arun Patidar

Avatar

Level 7

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

Avatar

Community Advisor

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

https://github.com/arunpatidar02/aemaacs-aemlab/blob/master/core/src/main/java/com/community/aemlab/... 



Arun Patidar

Avatar

Level 7

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. 

Avatar

Community Advisor

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); }

https://docs.oracle.com/javase/7/docs/api/org/xml/sax/helpers/AttributesImpl.html#setAttribute(int,%...

 



Arun Patidar

Avatar

Level 7

@arunpatidar 

 

With above code I am getting arrayIndexout of bound exception

I tried with hrefindex but href is overwritten.

 

Ronnie09_0-1663512733652.png

 

Avatar

Community Advisor

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");
                     }

 

https://docs.oracle.com/javase/7/docs/api/org/xml/sax/helpers/AttributesImpl.html#addAttribute(java.... 



Arun Patidar