66 lines
1.6 KiB
JavaScript
66 lines
1.6 KiB
JavaScript
/**
|
||
* SmartyPants - Smart typography for web content
|
||
* Converts straight quotes to curly quotes, dashes to em-dashes, etc.
|
||
* Based on the original SmartyPants by John Gruber
|
||
*/
|
||
|
||
const SmartyPants = (function() {
|
||
// Regular expressions for matching
|
||
const quotes = {
|
||
double: {
|
||
opening: /(\s|^)"(\w)/g,
|
||
closing: /(\w)"/g,
|
||
openingNested: /(\s|^)'(\w)/g,
|
||
closingNested: /(\w)'/g
|
||
},
|
||
single: {
|
||
opening: /(\s|^)'(\w)/g,
|
||
closing: /(\w)'/g
|
||
}
|
||
};
|
||
|
||
const dashes = {
|
||
emDash: /--/g,
|
||
enDash: / - /g
|
||
};
|
||
|
||
const ellipses = /\.\.\./g;
|
||
|
||
/**
|
||
* Process text with SmartyPants transformations
|
||
*/
|
||
function process(text) {
|
||
if (!text) return text;
|
||
|
||
let result = text;
|
||
|
||
// Transform double quotes
|
||
result = result.replace(quotes.double.opening, '$1"$2');
|
||
result = result.replace(quotes.double.closing, '$1"');
|
||
|
||
// Transform single quotes
|
||
result = result.replace(quotes.single.opening, '$1\u2018$2');
|
||
result = result.replace(quotes.single.closing, '$1\u2019');
|
||
|
||
// Transform apostrophes (same as closing single quotes)
|
||
result = result.replace(/(\w)'(\w)/g, '$1\u2019$2');
|
||
|
||
// Transform dashes
|
||
result = result.replace(dashes.emDash, '—');
|
||
result = result.replace(dashes.enDash, ' – ');
|
||
|
||
// Transform ellipses
|
||
result = result.replace(ellipses, '…');
|
||
|
||
return result;
|
||
}
|
||
|
||
return {
|
||
process: process
|
||
};
|
||
})();
|
||
|
||
// Make available in browser and Node.js environments
|
||
if (typeof module !== 'undefined' && module.exports) {
|
||
module.exports = SmartyPants;
|
||
} |