Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Eleventy filter for automatically converting quotes to smartquotes.
eleventyConfig.addFilter("smartquotes", (post) => {
const hawaii = new RegExp(/Hawai'i/g);
const slang = new RegExp(/'(cause|em|til|twas)/g);
const apostrophes = new RegExp(/(\b)'(\b)/g);
const years = new RegExp(/(\s)'(\d)/g);
const openDoubles = new RegExp(/(\s|^|>)"/g);
const closeDoubles = new RegExp(/"/g);
const openSingles = new RegExp(/(\s|^|>)'/g);
const closeSingles = new RegExp(/'/g);
return post
.replace(hawaii, "Hawaiʻi").replace(slang, "’$1")
.replace(apostrophes, "$1’$2").replace(years, "$1’$2")
.replace(openDoubles, "$1“").replace(closeDoubles, "”")
.replace(openSingles, "$1‘").replace(closeSingles, "’");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment