Files
ai.interactive.fiction/public/js/smartypants.js
T
2025-04-01 08:37:41 +02:00

66 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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;
}