|
| 1 | +# jcdcdev.Umbraco.ReadingTime |
| 2 | + |
| 3 | +[](https://umbraco.com/products/umbraco-cms/) |
| 4 | +[](https://www.nuget.org/packages/jcdcdev.Umbraco.ReadingTime) |
| 5 | +[](../LICENSE) |
| 6 | +[](https://www.nuget.org/packages/jcdcdev.Umbraco.ReadingTime/) |
| 7 | + |
| 8 | +Custom Data Type for calculating reading time. |
| 9 | + |
| 10 | +The following editors are currently supported: |
| 11 | + |
| 12 | +- Rich Text |
| 13 | +- Markdown |
| 14 | +- Block Grid |
| 15 | +- Block List |
| 16 | +- Nested Content |
| 17 | +- Textstring |
| 18 | +- Textarea |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +1. Install the [NuGet package](https://www.nuget.org/packages/jcdcdev.Umbraco.ReadingTime) in your Umbraco CMS website project. |
| 23 | +2. Add the Reading Time data type to a document type. |
| 24 | + - Note: You can configure the words per minute in the data type settings. |
| 25 | +3. Save and publish content. |
| 26 | +4. Reading Time will display in the backoffice |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Using the value in your templates |
| 31 | + |
| 32 | +In your template, you can accessing the Reading Time property. |
| 33 | + |
| 34 | +```csharp |
| 35 | +<div class="alert alert-info"> |
| 36 | + Read in @Model.ReadingTime.DisplayTime() |
| 37 | +</div> |
| 38 | +``` |
| 39 | + |
| 40 | +## Configuration |
| 41 | + |
| 42 | +You can change the average words per minute in the data type settings. |
| 43 | + |
| 44 | +When creating a new data type, the default will be 200 words per minute. To change this default, adjust your `appsettings.json` file: |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "ReadingTime": { |
| 49 | + "WordsPerMinute": 200 |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Limitations |
| 55 | + |
| 56 | +**Values are derived from published content only.** |
| 57 | + |
| 58 | +Draft content is _not_ included in the calculation. |
| 59 | + |
| 60 | +## Extending |
| 61 | + |
| 62 | +You can extend the data type to support additional editors by implementing the `IReadingTimeValueProvider` interface. |
| 63 | + |
| 64 | +```csharp |
| 65 | +public class MyCustomReadingTimeValueProvider : IReadingTimeValueProvider |
| 66 | +{ |
| 67 | + public bool CanConvert(IPropertyType type) |
| 68 | + { |
| 69 | + return type.EditorAlias == "MyCustomEditorAlias"; |
| 70 | + } |
| 71 | + |
| 72 | + public TimeSpan? GetReadingTime(IProperty property, string? culture, string? segment, IEnumerable<string> availableCultures, ReadingTimeConfiguration config) |
| 73 | + { |
| 74 | + var value = property.GetValue(culture, segment, true); |
| 75 | + if (value is string text) |
| 76 | + { |
| 77 | + return text.GetReadingTime(config.WordsPerMinute); |
| 78 | + } |
| 79 | + |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | +``` |
| 84 | + |
| 85 | +Don't forget to register your custom value provider: |
| 86 | + |
| 87 | +```csharp |
| 88 | +public class Composer : IComposer |
| 89 | +{ |
| 90 | + public void Compose(IUmbracoBuilder builder) |
| 91 | + { |
| 92 | + builder.ReadingTimeValueProviders().Append<MyCustomReadingTimeValueProvider>(); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | +## Contributing |
| 97 | + |
| 98 | +Contributions to this package are most welcome! Please read the [Contributing Guidelines](CONTRIBUTING.md). |
| 99 | + |
| 100 | +## Acknowledgments (thanks!) |
| 101 | + |
| 102 | +- LottePitcher - [opinionated-package-starter](https://github.com/LottePitcher/opinionated-package-starter) |
0 commit comments