diff --git a/CHANGELOG.md b/CHANGELOG.md index 057885c..3c4d722 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v0.2.0 / 2022-07-28 + +- Added the `stripHtml` option, on by default. +- Added the `replacements` option. + ## v0.1.0 / 2022-02-23 - Use Metalsmith v2.4's `metalsmith.match()` instead of [`minimatch`](https://www.npmjs.com/package/minimatch). diff --git a/README.md b/README.md index 4aadc4d..b7df91d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Metalsmith(__dirname) ## File metadata -This plugin adds a metadata field named `readingTime` to each file which can be used with templating engines, such as with [Handlebars](https://www.npmjs.com/package/handlebars): +This plugin adds a metadata field named `readingTime` to each file which can be used with templating engines, such as with [`handlebars`](https://www.npmjs.com/package/handlebars): ```handlebars Reading time: {{ readingTime }} @@ -45,7 +45,7 @@ Reading time: {{ readingTime }} The rest of the page content. ``` -Reading time will be reported in minutes in the form "# min read" per [reading-time](https://www.npmjs.com/package/reading-time). +Reading time will be reported in minutes in the form "# min read" per [`reading-time`](https://www.npmjs.com/package/reading-time). ## Options @@ -53,7 +53,19 @@ Reading time will be reported in minutes in the form "# min read" per [reading-t Type: `string` Default: `**/*` -A [minimatch](https://www.npmjs.com/package/minimatch) glob pattern to find input files. +A [`micromatch`](https://www.npmjs.com/package/micromatch) glob pattern to find input files. + +### `stripHtml` (optional) + +Type: `boolean` Default: `true` + +Whether to strip HTML tags from content before evaluating the reading time or not. + +### `replacements` (optional) + +type: `(string|RegExp)[][]` Default: `[]` + +A list of tuples fed to `String.replace()` to get rid of meaningless content before evaluating the reading time. ### `readingTime` (optional) diff --git a/lib/fixtures/advanced/config.json b/lib/fixtures/advanced/config.json new file mode 100644 index 0000000..01212bf --- /dev/null +++ b/lib/fixtures/advanced/config.json @@ -0,0 +1,9 @@ +{ + "options": { + "stripHtml": false, + "replacements": [ + ["/Lorem ipsum .+/si", ""], + ["foo", "bar"] + ] + } +} diff --git a/lib/fixtures/advanced/expected/lorem-ipsum.md b/lib/fixtures/advanced/expected/lorem-ipsum.md new file mode 100644 index 0000000..820db04 --- /dev/null +++ b/lib/fixtures/advanced/expected/lorem-ipsum.md @@ -0,0 +1,12 @@ +Reading time: 1 min read + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tempus, purus vel dignissim interdum, nunc diam lacinia est, sit amet vestibulum eros tortor nec mauris. Suspendisse ac dignissim turpis. Fusce augue erat, scelerisque id ultrices ac, tristique in neque. Nunc at nisi a neque malesuada elementum. Etiam laoreet aliquam maximus. Etiam congue dolor et nulla iaculis scelerisque. Integer lacinia ante imperdiet sapien venenatis, nec ultrices tellus iaculis. + +Integer at tincidunt libero. Mauris eu porttitor lorem. Sed porttitor lacus eu ipsum finibus vulputate. Nam suscipit erat leo, eget elementum sapien auctor eu. Vivamus lectus magna, eleifend ac molestie eu, mattis vel justo. Sed bibendum, mi ut sodales vulputate, elit tellus tempor magna, ut rutrum ex mauris vel turpis. Suspendisse sagittis ligula tortor, non vestibulum orci tempus ut. Pellentesque at leo vel risus tempus aliquet. Quisque ac interdum orci. + +Fusce nunc risus, rutrum eget felis bibendum, faucibus varius sem. Quisque interdum mi et hendrerit lacinia. Fusce vehicula iaculis orci, mattis consectetur ipsum rhoncus vitae. Sed eu nunc ut ante accumsan consequat ac id lorem. Donec sagittis hendrerit congue. Aliquam auctor libero est, vel molestie dolor fermentum non. Ut commodo, orci vitae gravida egestas, magna odio pulvinar nisi, quis volutpat est ligula aliquet tortor. Etiam imperdiet sodales molestie. Etiam et posuere lectus. Sed rutrum, leo quis scelerisque eleifend, leo odio ultricies sem, nec consectetur purus tortor nec dolor. Suspendisse euismod nunc sit amet erat fringilla bibendum. Proin et tortor non eros gravida facilisis eget eget ipsum. Vivamus non ipsum lorem. Etiam sem erat, tristique at mauris et, porta cursus velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu libero fermentum, malesuada orci at, pellentesque urna. + +Nullam nunc nibh, posuere eu sem eget, suscipit interdum augue. Sed semper consectetur orci, id placerat augue consequat sed. Maecenas maximus fermentum nulla, eu sagittis nisl auctor id. Sed finibus mattis tristique. Etiam vitae ipsum sed augue pulvinar congue. Aliquam lacus lorem, lacinia ac sollicitudin quis, cursus sit amet felis. Praesent nec neque dolor. Nullam eget orci non sapien iaculis pellentesque in non lacus. Duis quam elit, ornare at viverra consequat, aliquam non metus. Quisque iaculis pretium commodo. Aliquam erat volutpat. Pellentesque vehicula libero nec dolor faucibus molestie. Sed porttitor enim in dui aliquet cursus. Praesent lacinia id ipsum sit amet elementum. + +Mauris auctor sodales leo id ultrices. Sed molestie viverra turpis sed pharetra. Phasellus eu congue velit. Praesent non tellus a nisl suscipit pharetra. Sed sed efficitur arcu, a imperdiet lectus. Donec varius neque tortor, sit amet suscipit nisl eleifend a. Proin volutpat, eros nec condimentum finibus, ipsum est imperdiet arcu, id consequat eros velit quis lorem. Ut pharetra faucibus turpis a sagittis. Proin fermentum velit a felis ornare varius. Nunc rhoncus purus eget ipsum egestas, vel consequat erat viverra. Ut interdum faucibus imperdiet. Donec aliquet aliquet mattis. Mauris posuere orci purus, vel hendrerit ligula tristique eget. Nullam euismod, lacus ac semper volutpat, est sapien aliquam eros, id ultrices nulla nibh elementum turpis. Proin id lorem a elit maximus rhoncus. Donec non metus finibus, tincidunt tortor condimentum, ullamcorper risus. diff --git a/lib/fixtures/advanced/expected/svg.md b/lib/fixtures/advanced/expected/svg.md new file mode 100644 index 0000000..328231a --- /dev/null +++ b/lib/fixtures/advanced/expected/svg.md @@ -0,0 +1,122 @@ +Reading time: 120 min read + + +Here is a pretty large SVG file: + + + +image/svg+xml2008 Jun 12 03:54:25 + + + + + + + + +OMC - Martin Weinelt + + + + + + + + +km + + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + + +km + + + + + + + + + + +2008 Jun 12 03:54:25 + + + + + + + +OMC - Martin Weinelt + + + + + + + +km + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + +km + + + + + + + + + + diff --git a/lib/fixtures/advanced/src/lorem-ipsum.md b/lib/fixtures/advanced/src/lorem-ipsum.md new file mode 100644 index 0000000..89f8170 --- /dev/null +++ b/lib/fixtures/advanced/src/lorem-ipsum.md @@ -0,0 +1,12 @@ +Reading time: {{ readingTime }} + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tempus, purus vel dignissim interdum, nunc diam lacinia est, sit amet vestibulum eros tortor nec mauris. Suspendisse ac dignissim turpis. Fusce augue erat, scelerisque id ultrices ac, tristique in neque. Nunc at nisi a neque malesuada elementum. Etiam laoreet aliquam maximus. Etiam congue dolor et nulla iaculis scelerisque. Integer lacinia ante imperdiet sapien venenatis, nec ultrices tellus iaculis. + +Integer at tincidunt libero. Mauris eu porttitor lorem. Sed porttitor lacus eu ipsum finibus vulputate. Nam suscipit erat leo, eget elementum sapien auctor eu. Vivamus lectus magna, eleifend ac molestie eu, mattis vel justo. Sed bibendum, mi ut sodales vulputate, elit tellus tempor magna, ut rutrum ex mauris vel turpis. Suspendisse sagittis ligula tortor, non vestibulum orci tempus ut. Pellentesque at leo vel risus tempus aliquet. Quisque ac interdum orci. + +Fusce nunc risus, rutrum eget felis bibendum, faucibus varius sem. Quisque interdum mi et hendrerit lacinia. Fusce vehicula iaculis orci, mattis consectetur ipsum rhoncus vitae. Sed eu nunc ut ante accumsan consequat ac id lorem. Donec sagittis hendrerit congue. Aliquam auctor libero est, vel molestie dolor fermentum non. Ut commodo, orci vitae gravida egestas, magna odio pulvinar nisi, quis volutpat est ligula aliquet tortor. Etiam imperdiet sodales molestie. Etiam et posuere lectus. Sed rutrum, leo quis scelerisque eleifend, leo odio ultricies sem, nec consectetur purus tortor nec dolor. Suspendisse euismod nunc sit amet erat fringilla bibendum. Proin et tortor non eros gravida facilisis eget eget ipsum. Vivamus non ipsum lorem. Etiam sem erat, tristique at mauris et, porta cursus velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu libero fermentum, malesuada orci at, pellentesque urna. + +Nullam nunc nibh, posuere eu sem eget, suscipit interdum augue. Sed semper consectetur orci, id placerat augue consequat sed. Maecenas maximus fermentum nulla, eu sagittis nisl auctor id. Sed finibus mattis tristique. Etiam vitae ipsum sed augue pulvinar congue. Aliquam lacus lorem, lacinia ac sollicitudin quis, cursus sit amet felis. Praesent nec neque dolor. Nullam eget orci non sapien iaculis pellentesque in non lacus. Duis quam elit, ornare at viverra consequat, aliquam non metus. Quisque iaculis pretium commodo. Aliquam erat volutpat. Pellentesque vehicula libero nec dolor faucibus molestie. Sed porttitor enim in dui aliquet cursus. Praesent lacinia id ipsum sit amet elementum. + +Mauris auctor sodales leo id ultrices. Sed molestie viverra turpis sed pharetra. Phasellus eu congue velit. Praesent non tellus a nisl suscipit pharetra. Sed sed efficitur arcu, a imperdiet lectus. Donec varius neque tortor, sit amet suscipit nisl eleifend a. Proin volutpat, eros nec condimentum finibus, ipsum est imperdiet arcu, id consequat eros velit quis lorem. Ut pharetra faucibus turpis a sagittis. Proin fermentum velit a felis ornare varius. Nunc rhoncus purus eget ipsum egestas, vel consequat erat viverra. Ut interdum faucibus imperdiet. Donec aliquet aliquet mattis. Mauris posuere orci purus, vel hendrerit ligula tristique eget. Nullam euismod, lacus ac semper volutpat, est sapien aliquam eros, id ultrices nulla nibh elementum turpis. Proin id lorem a elit maximus rhoncus. Donec non metus finibus, tincidunt tortor condimentum, ullamcorper risus. diff --git a/lib/fixtures/advanced/src/svg.md b/lib/fixtures/advanced/src/svg.md new file mode 100644 index 0000000..19e4f84 --- /dev/null +++ b/lib/fixtures/advanced/src/svg.md @@ -0,0 +1,122 @@ +Reading time: {{ readingTime }} + + +Here is a pretty large SVG file: + + + +image/svg+xml2008 Jun 12 03:54:25 + + + + + + + + +OMC - Martin Weinelt + + + + + + + + +km + + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + + +km + + + + + + + + + + +2008 Jun 12 03:54:25 + + + + + + + +OMC - Martin Weinelt + + + + + + + +km + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + +km + + + + + + + + + + diff --git a/lib/fixtures/basic/expected/svg.md b/lib/fixtures/basic/expected/svg.md new file mode 100644 index 0000000..3956ddc --- /dev/null +++ b/lib/fixtures/basic/expected/svg.md @@ -0,0 +1,122 @@ +Reading time: 1 min read + + +Here is a pretty large SVG file: + + + +image/svg+xml2008 Jun 12 03:54:25 + + + + + + + + +OMC - Martin Weinelt + + + + + + + + +km + + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + + +km + + + + + + + + + + +2008 Jun 12 03:54:25 + + + + + + + +OMC - Martin Weinelt + + + + + + + +km + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + +km + + + + + + + + + + diff --git a/lib/fixtures/basic/src/svg.md b/lib/fixtures/basic/src/svg.md new file mode 100644 index 0000000..19e4f84 --- /dev/null +++ b/lib/fixtures/basic/src/svg.md @@ -0,0 +1,122 @@ +Reading time: {{ readingTime }} + + +Here is a pretty large SVG file: + + + +image/svg+xml2008 Jun 12 03:54:25 + + + + + + + + +OMC - Martin Weinelt + + + + + + + + +km + + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + + +km + + + + + + + + + + +2008 Jun 12 03:54:25 + + + + + + + +OMC - Martin Weinelt + + + + + + + +km + + + + + + + +2008 Jun 12 04:45:24 + + + + + + + + + +OMC - Martin Weinelt + + + + + + + + + +km + + + + + + + + + + diff --git a/lib/index.js b/lib/index.js index f65e9de..9b91dcf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,14 +6,40 @@ const readingTime = require('reading-time'); module.exports = (options) => { options = deepmerge({ pattern: '**/*', + stripHtml: true, + replacements: [], readingTime: {}, }, options || {}); return (files, metalsmith, done) => { // For each file that matches the given pattern - metalsmith.match(options.pattern) + metalsmith.match(options.pattern, Object.keys(files)) .forEach((filename) => { - const contents = files[filename].contents.toString(); + let contents = files[filename].contents.toString(); + + if (options.stripHtml) { + contents = contents + // XML + .replace(/<\?xml[^>]+\?>/gs, ' ') // namespaces + .replace(//gs, ' ') // comments + // HTML + .replace(/<[a-zA-Z0-9]+[^>]*>/gs, ' ') // start tag + .replace(/<\/[a-zA-Z0-9]+>/gs, ' ') // end tag + .trim(); + } + + if (options.replacements && options.replacements.length) { + options.replacements.forEach((replacement) => { + let pattern = replacement[0]; + const matches = pattern.match(/^\/(.+)\/([a-z]*)/); + if (matches) { + pattern = new RegExp(matches[1], matches[2]); + } + + contents = contents.replace(pattern, replacement[1]); + }); + } + files[filename].readingTime = readingTime(contents, options.readingTime).text; }); diff --git a/package-lock.json b/package-lock.json index d4e0f8d..2657328 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "metalsmith-reading-time", - "version": "0.1.2", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index abdffaa..33c5471 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "metalsmith-reading-time", - "version": "0.1.2", + "version": "0.2.0", "description": "A Metalsmith plugin to estimate pages' reading times.", "keywords": [ "metalsmith",