Created
December 5, 2018 22:45
-
-
Save janrenn/dd58008e6b81a5ceed934c2fee02e3e9 to your computer and use it in GitHub Desktop.
Applies some basic typographic rules on text.
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
/** | |
* Applies some basic typographic rules on text. | |
* | |
* @param {String} text Text the rules should be applied on | |
* @param {Boolean} removeEmpty Whether to remove empty <p>s | |
* @returns {String} Adjusted text | |
* @version 0.2 | |
* @author %AUTHOR% | |
*/ | |
typography: function (text, removeEmpty) { | |
text = text.replace(/ (i|k|o|s|u|v|z|I|K|O|S|U|V|Z|A) /g, ' $1\u00a0').replace(/ (sv\.|Sv\.|ing\.|Ing\.|Dr\.|MUDr\.|JUDr\.|Doc\.|Mgr\.|Bc\.) /g, ' $1\u00a0'); | |
text = text | |
.replace(/([0-9]) (m|cm|km|kg|g|Kč|CZK|USD|$|EUR|min|l|ml|kWh|rok|roky|let|year|years|koruny|korun|%)(<|.|,|\s)/g, '$1\u00a0$2$3') | |
.replace(/([0-9]),(-|–|—) /g, '$1,—\u00a0') | |
.replace(/Kč ([0-9])/g, 'Kč\u00a0$1') | |
.replace(/ ([0-9]+)($|<)/g, '\u00a0$1$2'); | |
text = text.replace(/ (I|II|III|IV|V|VI|VII|VIII|IX|X|XI|XII|XIII|XIV|XV|XVI|XVII|XVIII|XIX|XX|XXI|XXII|XXIII|XXIV|XXV)\. /g, ' $1.\u00a0'); | |
if (removeEmpty) { | |
text = text.replace(/ /g, '\u00a0').replace(/ /g, '\u00a0').replace(/<p[^>]*>[ \s]*<\/p>/gi, ''); | |
} | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment