0

I'm working with Sitecore 10.1 and need help with the Rich Text Editor (RTE). I want all links added to the RTE to automatically replace spaces with dashes in the URL to maintain consistency and meet SEO standards.

Currently, links like http://example.com/Example Link Text are not automatically converting spaces to dashes, despite the following configuration in Sitecore.config:

<replace find=" " mode="on" replaceWith="-"/>
<replace find="%20" mode="on" replaceWith="-"/>

How can I ensure URLs in the RTE automatically convert spaces to dashes?

1
  • Is this for internal links or external links?
    – Gatogordo
    Commented Jun 18 at 6:50

1 Answer 1

3

You will need to create a custom processor inside renderField pipeline to replace space with dashes in external urls inside RTE.

  1. Firstly you will need to create a custom processor as below, that converts the Html inside RTE to HtmlDocument that can be used for searching href attributes in the HTML dom and replacing their values as desired.
using HtmlAgilityPack;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.RenderField;
using System;
using System.Linq;

public class EncodeExternalLinks
{
   public virtual void Process(RenderFieldArgs args)
   {
      Assert.ArgumentNotNull((object)args, nameof(args));
      if (Context.PageMode.IsExperienceEditorEditing)
         return;
      if (args.FieldTypeKey == "rich text")
      {
          var richTextHtml = args.Result.FirstPart;
          var doc = new HtmlDocument();
          doc.LoadHtml(richTextHtml);
          var anchorNodes = doc.DocumentNode.SelectNodes("//a[@href]");
          if (anchorNodes != null && anchorNodes.Any())
          {
             foreach (var anchorNode in anchorNodes)
             {
                Uri originalUrl;
                var hrefAttribute = anchorNode.Attributes.Where(x => x.Name.Equals("href"))?.FirstOrDefault();
                if (hrefAttribute != null && Uri.TryCreate(hrefAttribute.Value, UriKind.Absolute, out originalUrl))
                {
                    hrefAttribute.Value = hrefAttribute.Value.Replace(" ", "-");
                }
             }
             args.Result.FirstPart = doc.DocumentNode.InnerHtml;
          }
       }
    }
}
  1. Thereafter you need to patch the custom processor created above as below after the ExpandLinks processor under the renderField pipeline.
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
      <pipelines>
            <renderField>
                <processor patch:after="*[@type='Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel']" type="SitecoreCustomExtensions.Pipelines.EncodeExternalLinks,SitecoreCustomExtensions" resolve="true" />
            </renderField>
      </pipelines>
    </sitecore>
</configuration>

Once the above changes are done, external link inside RTE will have spaces in the url replaced with dash(-). Let me know in case you have any doubts.

Hope this helps!!!

4
  • This will only affect the way the Rich Text Field is rendered, right? It won't actually modify the field value. Commented Jun 18 at 13:27
  • 1
    yes @VinayJadav this wont affect field value. It will reflect on the website.
    – ckhanna
    Commented Jun 18 at 13:36
  • Thanks alor @ckhanna Commented Jun 19 at 3:16
  • Thanks. Glad it helped @OwaisAhmed!!!
    – ckhanna
    Commented Jun 19 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.