How to automatically add nofollow tag to external links in Full Description?

11 months ago
I wanted to implement this feature on my nopCommerce 4.7-based website.

I want to automatically add the rel="nofollow" tag to all external URLs on the Full Description section of the Product Details page.

However, I am not sure how to accomplish that. Any help I can get from the experts?

Thanks,
11 months ago
You can modify the core code, or create a plugin to override the method:

\Presentation\Nop.Web\Factories\ProductModelFactory.cs
public virtual async Task<IEnumerable<ProductOverviewModel>> PrepareProductOverviewModelsAsync(IEnumerable<Product> products,
...{
...    foreach (var product in products)
    {
        var model = new ProductOverviewModel
        {
...
            FullDescription = await _localizationService.GetLocalizedAsync(product, x => x.FullDescription),
...

11 months ago
New York wrote:
You can modify the core code, or create a plugin to override the method:

\Presentation\Nop.Web\Factories\ProductModelFactory.cs
public virtual async Task<IEnumerable<ProductOverviewModel>> PrepareProductOverviewModelsAsync(IEnumerable<Product> products,
...{
...    foreach (var product in products)
    {
        var model = new ProductOverviewModel
        {
...
            FullDescription = await _localizationService.GetLocalizedAsync(product, x => x.FullDescription),
...



Hello New York,

First of all, Thank you for taking the time to solve this puzzle. However, I tried all the possible code and none of them worked. Thanks
11 months ago
Maybe you can use jQuery for this.
Something like
 $(".full-description a").attr('rel', 'nofollow');


To be safe. I would add the class name to the product description for an external link, something like "external-link" and use where full-description is the wrapper div for full description

$(".full-description a.external-link").attr('rel', 'nofollow');
11 months ago
sanju.dahal741 wrote:
Maybe you can use jQuery for this.
Something like
 $(".full-description a").attr('rel', 'nofollow');


To be safe. I would add the class name to the product description for an external link, something like "external-link" and use where full-description is the wrapper div for full description

$(".full-description a.external-link").attr('rel', 'nofollow');


Hello sanju, this does work but not on HTML source. How to do it server-side? It should be easy but its way more difficult to achive.
11 months ago
RE: "To be safe. I would add the class name to the product description for an external link, something like "external-link"

But doesn't that means that the admin user (who writes the description and embeds the html link) has to add that class to the link?
     <a class="external-link" href=...
Then he may as well just put in the rel="nofollow"  ;)
    <a rel="nofollow" href=...

I.e., the op requirement is "I want to automatically".


RE: "How to do it server-side? ...I tried all the possible code and none of them worked. "
I don't know what you tried.  Unlike the JavaScript / JQuery having ability to recognize and manipulate the DOM, the server-side just has a string / "text".  There may be .NET libraries that can do it, but I think that can be a lot of overhead.
You just need a way to identify "external" links and do a string replace.  I asked AI to differentiate between external and internal links, and it recommended RegEx:
string input = @"<a href=""https://www.externalone.com/"">externalone.com</a> 
                 <a href=""http://example.com/"">Example</a>
                 <a href=""/internal-link"">Internal</a>
                 <a href=""~/another-internal-link"">Another Internal</a>";

string pattern = @"<a\s+(?![^>]*\brel=)[^>]*\bhref=""https?://[^""]*""";

string replacement = @"<a rel=""nofollow"" $0";

string result = Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase);


(I did not test this ;)

11 months ago
pnkrdb wrote:
I wanted to implement this feature on my nopCommerce 4.7-based website.

I want to automatically add the rel="nofollow" tag to all external URLs on the Full Description section of the Product Details page.

However, I am not sure how to accomplish that. Any help I can get from the experts?

Thanks,

Using the nopcommerce component is quite simple.