Search code examples
javascripthtmlbloggerunicode-escapes

Unicode text converted into decimal- Blogger


I'm trying to share some Malayalam text to Facebook feed dialog via Blogger but I'm facing some problem. Here's the issue. I'm using the feed dialog code directly in the HTML part of the blog post and because of that, the final text is automatically converted into Unicode decimal by blogger and Facebook is displaying the text in the same unreadable format.

An example

function FBShareOp(){
 var name = 'ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്'
 var description = "ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്"
 var share_image  = 'IMAGE LINK ';
 var share_url  = 'URL'; 
 var share_capt = 'ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്';

so in the above code, I'm using the custom Malayalam text ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ് and after the post is published, blogger is converting that text to ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ് and Facebook is displaying the text exactly like that.

So how can I make it work? I don't want blogger to format it like that. Is there any way to post that text without such formatting so that Facebook can display it properly? Thanks in advance.


Solution

  • This happens because the XML parser that Blogger uses escapes certain characters. A way to stop the content from getting escaped is by enclosing it within -

    <![CDATA[ 
     ... Your code ...
    ]]>
    

    But inside the post editor, the above method is not working. Instead, you will have to decode the Entities via a JavaScript function (As demonstrated in the following answer). Your code will change in the following manner -

    <script>
    
       var decodeEntities = (function() {
         // this prevents any overhead from creating the object each time
         var element = document.createElement('div');
    
         function decodeHTMLEntities(str) {
          if(str && typeof str === 'string') {
            // strip script/html tags
            str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
            str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
            element.innerHTML = str;
            str = element.textContent;
            element.textContent = '';
           }
    
           return str;
        }
    
       return decodeHTMLEntities;
       })();
    
       function FBShareOp(){
       var name = decodeEntities('ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്');
       var description = decodeEntities("ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്");
       var share_image  = 'IMAGE LINK ';
       var share_url  = 'URL'; 
       var share_capt = decodeEntities('ഇതൊരു ടെസ്റ്റ് പോസ്റ്റാണ്');
       }
    
    </script>