Last active
December 17, 2018 19:38
-
-
Save eslam-mahmoud/7669db21683aa1c20075a8472193e1fc to your computer and use it in GitHub Desktop.
Scan WordPress post paragraphs if most of characters are EN lang change direction and alignment to LTR and the other way if Arabic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//TODO use regex | |
let enAlphabet = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
let arAlphabet = 'ض ص ث ق ف غ ع ه خ ح ش س ي ب ل ا ت ن م ئ ء ؤ ر لا ى ة و ز ظ ط ك د ج'.split(' '); //TODO update the list | |
let resutls = document.querySelectorAll(".post p"); | |
resutls.forEach(function(post){ | |
let arScore = 0; | |
let enScore = 0; | |
let postText = post.textContent.trim(); | |
if (postText.length < 1) { | |
return; | |
} | |
let i = postText.length; | |
while (i--) { | |
if (arAlphabet.includes(postText.charAt(i))) { | |
arScore++; | |
} else if (enAlphabet.includes(postText.charAt(i))) { | |
enScore++; | |
} | |
} | |
if (enScore > arScore) { | |
post.style.direction = "ltr"; | |
post.style["text-align"] = "left"; | |
} else { | |
post.style.direction = "rtl"; | |
post.style["text-align"] = "right"; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment