Link Rewriting | Community
Skip to main content
New Participant
September 14, 2022
Solved

Link Rewriting

  • September 14, 2022
  • 3 replies
  • 2390 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shiv_Prakash_Patel

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

 

3 replies

arunpatidar
New Participant
September 14, 2022

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-rewriter/index.html 

Arun Patidar
Ronnie09Author
New Participant
September 14, 2022

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

arunpatidar
New Participant
September 14, 2022

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/CustomLinkChecker.java#L83 

 

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/core/services/transformer/sample/SampleLinkTransformerFactory.java 

Arun Patidar
Dipti_Chauhan
New Participant
September 14, 2022

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

Shiv_Prakash_Patel
Shiv_Prakash_PatelAccepted solution
New Participant
September 14, 2022

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