Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

v0.2.0 #88

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,35 @@ 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 }}

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

### `pattern` (optional)

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)

Expand Down
9 changes: 9 additions & 0 deletions lib/fixtures/advanced/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"options": {
"stripHtml": false,
"replacements": [
["/Lorem ipsum .+/si", ""],
["foo", "bar"]
]
}
}
12 changes: 12 additions & 0 deletions lib/fixtures/advanced/expected/lorem-ipsum.md
Original file line number Diff line number Diff line change
@@ -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.
122 changes: 122 additions & 0 deletions lib/fixtures/advanced/expected/svg.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/fixtures/advanced/src/lorem-ipsum.md
Original file line number Diff line number Diff line change
@@ -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.
122 changes: 122 additions & 0 deletions lib/fixtures/advanced/src/svg.md

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions lib/fixtures/basic/expected/svg.md

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions lib/fixtures/basic/src/svg.md

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down