1
0
Fork 0
Browse Source

feat: add new async filter for generating a wordcloud

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
main
André Jaenisch 7 months ago
parent
commit
2d83d3ae4a
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 13
      filters/index.js
  2. 54
      filters/wordcloud.js
  3. 20
      nunjucks.config.js
  4. 30
      package-lock.json
  5. 1
      package.json
  6. 10
      shortcodes/index.js

13
filters/index.js

@ -8,6 +8,7 @@ const normalizeWhitespace = require('./normalize-whitespace')
const replace = require('./replace')
const readingDifficulty = require('./reading-difficulty')
const withWeatherForecast = require('./with-weather-forecast')
const wordcloud = require('./wordcloud')
const eleventyNavigation = (items) => items
@ -19,6 +20,12 @@ const eleventyNavigationToHtml = (items, config) => {
return `<ul>${items.map((item) => `<li><a href="${item.url}">${item.title}</a></li>`)}</ul>`
}
const url = (path) => path
const asyncFilters = [
{ name: 'wordcloud', value: wordcloud },
]
const filters = [
{ name: 'asEventObject', value: asEventObject },
{ name: 'cssmin', value: (code) => code },
@ -32,8 +39,12 @@ const filters = [
{ name: 'normalize_whitespace', value: normalizeWhitespace },
{ name: 'replace', value: replace },
{ name: 'readingDifficulty', value: readingDifficulty },
{ name: 'url', value: url },
{ name: 'withWeatherForecast', value: withWeatherForecast },
]
module.exports = filters
module.exports = {
async: asyncFilters,
sync: filters
}

54
filters/wordcloud.js

@ -0,0 +1,54 @@
const { tagCloud } = require('tag-cloud')
const wordcloud = function (tagsByPost, callback) {
if (!tagsByPost) {
if (callback) {
// Not handled by Eleventy, but used inside Storybook
return callback(null, '')
}
return Promise.resolve('')
}
const treshold = 4
const tags = {}
tagsByPost.forEach((words) => {
words.forEach((word) => {
if (tags[word]) {
tags[word] = tags[word] + 1
} else {
tags[word] = 1
}
})
})
return new Promise((resolve, reject) => {
const tagsForCloud = Object
.keys(tags)
.filter((tag) => tags[tag] >= treshold)
.map((tag) => ({ tagName: tag, count: tags[tag] }))
tagCloud(
tagsForCloud,
(err, html) => {
if (err) {
if (callback) {
return callback(err)
}
return reject(err)
}
if (callback) {
return callback(null, html)
}
return resolve(html)
},
{
classPrefix: 'tag',
htmlTag: 'li'
}
)
})
}
module.exports = wordcloud

20
nunjucks.config.js

@ -1,11 +1,27 @@
const Nunjucks = require('nunjucks')
const filters = require ('./filters')
const filters = require('./filters')
const shortcodes = require('./shortcodes')
module.exports = (nunjucks) => {
filters.forEach((filter) => {
filters.async.forEach((filter) => {
const { name, value } = filter
nunjucks.addFilter(name, value, true)
})
filters.sync.forEach((filter) => {
const { name, value } = filter
nunjucks.addFilter(name, value)
})
shortcodes.async.forEach((shortcode) => {
const { name, value } = shortcode
nunjucks.addExtension(name, new value())
})
shortcodes.sync.forEach((shortcode) => {
const { name, value } = shortcode
nunjucks.addExtension(name, new value())
})
}

30
package-lock.json generated

@ -12,6 +12,7 @@
"clean-css": "^5.1.3",
"date-fns": "^2.23.0",
"markdown-it": "^12.1.0",
"tag-cloud": "^1.3.3",
"text-readability": "^1.0.5"
},
"devDependencies": {
@ -16772,6 +16773,17 @@
"node": ">= 0.11.15"
}
},
"node_modules/tag-cloud": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/tag-cloud/-/tag-cloud-1.3.3.tgz",
"integrity": "sha512-QU98yoDE8aAYXPEo99poaTmHnxTKxYW6OW652g7esqNr7Hi+P4MTd9WdbWzMAQ1Fotgo9ClHMWeFO5TJ0D4A4A==",
"dependencies": {
"underscore": "1.13.2"
},
"engines": {
"node": ">=12"
}
},
"node_modules/tapable": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/tapable/-/tapable-1.1.3.tgz",
@ -17366,6 +17378,11 @@
"which-boxed-primitive": "^1.0.2"
}
},
"node_modules/underscore": {
"version": "1.13.2",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.2.tgz",
"integrity": "sha512-ekY1NhRzq0B08g4bGuX4wd2jZx5GnKz6mKSqFL4nqBlfyMGiG10gDFhDTMEfYmDL6Jy0FUIZp7wiRB+0BP7J2g=="
},
"node_modules/unfetch": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/unfetch/-/unfetch-4.2.0.tgz",
@ -32524,6 +32541,14 @@
"object.getownpropertydescriptors": "^2.1.2"
}
},
"tag-cloud": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/tag-cloud/-/tag-cloud-1.3.3.tgz",
"integrity": "sha512-QU98yoDE8aAYXPEo99poaTmHnxTKxYW6OW652g7esqNr7Hi+P4MTd9WdbWzMAQ1Fotgo9ClHMWeFO5TJ0D4A4A==",
"requires": {
"underscore": "1.13.2"
}
},
"tapable": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/tapable/-/tapable-1.1.3.tgz",
@ -33008,6 +33033,11 @@
"which-boxed-primitive": "^1.0.2"
}
},
"underscore": {
"version": "1.13.2",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.2.tgz",
"integrity": "sha512-ekY1NhRzq0B08g4bGuX4wd2jZx5GnKz6mKSqFL4nqBlfyMGiG10gDFhDTMEfYmDL6Jy0FUIZp7wiRB+0BP7J2g=="
},
"unfetch": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/unfetch/-/unfetch-4.2.0.tgz",

1
package.json

@ -39,6 +39,7 @@
"clean-css": "^5.1.3",
"date-fns": "^2.23.0",
"markdown-it": "^12.1.0",
"tag-cloud": "^1.3.3",
"text-readability": "^1.0.5"
}
}

10
shortcodes/index.js

@ -0,0 +1,10 @@
const asyncShortcodes = [
]
const shortcodes = [
]
module.exports = {
async: asyncShortcodes,
sync: shortcodes
}
Loading…
Cancel
Save