Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Link Rewriting

Avatar

Level 6

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
Level 4

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

 

View solution in original post

0 Replies

Avatar

Correct answer by
Level 4

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

 

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

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/... 

Avatar

Level 6

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,%...

 

Avatar

Level 6

@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....