Skip to content

Instantly share code, notes, and snippets.

@janrenn
Created December 5, 2018 22:45
Show Gist options
  • Save janrenn/dd58008e6b81a5ceed934c2fee02e3e9 to your computer and use it in GitHub Desktop.
Save janrenn/dd58008e6b81a5ceed934c2fee02e3e9 to your computer and use it in GitHub Desktop.
Applies some basic typographic rules on text.
/**
* 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(/&nbsp;/g, '\u00a0').replace(/&#160;/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