Skip to content

Commit

Permalink
Add Nunjucks smartypants filter
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Aug 15, 2023
1 parent 968daec commit 5aec229
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/nunjucks/filters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { markedSmartypants } = require('marked-smartypants')
const filters = require('nunjucks/src/filters')
const slug = require('slug')

// Marked extension for "smart" typographic punctuation
const smartypants = markedSmartypants()

/**
* Format strings into kebab case but also
* handles `errorMessage` → `error-message`
Expand All @@ -20,3 +25,13 @@ exports.kebabCase = function (string) {
exports.slugify = function (string) {
return slug(string, { lower: true })
}

/**
* Format strings with "smart" typographic punctuation
*
* @param {string} string - String to format
* @returns {string} Formatted string
*/
exports.smartypants = function (string) {
return filters.safe(smartypants.hooks.postprocess(string))
}
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"jstransformer-marked": "^1.4.0",
"jstransformer-nunjucks": "^1.2.0",
"marked": "^7.0.2",
"marked-smartypants": "^1.0.5",
"metalsmith": "^2.6.1",
"metalsmith-canonical": "^1.2.0",
"multimatch": "^6.0.0",
Expand Down

0 comments on commit 5aec229

Please sign in to comment.