/jcdcdev.Umbraco.ReadingTime

Custom Umbraco property editor for calculating reading time ⏲️

Primary LanguageC#MIT LicenseMIT

jcdcdev.Umbraco.ReadingTime

Umbraco Marketplace GitHub License NuGet Downloads Project Website

Custom Data Type for calculating reading time. With full variant support!

The following editors are currently supported:

  • Rich Text
  • Markdown
  • Block Grid
  • Block List
  • Nested Content
  • Textstring
  • Textarea

Quick Start

  1. Install the NuGet package in your Umbraco CMS website project.

     dotnet add package jcdcdev.Umbraco.ReadingTime
    
  2. Add the Reading Time data type to a document type. You can configure:

    • Words per minute (default is 200)
    • Min Unit (default is Minute)
    • Max Unit (default is Minute) A screenshot of the BackOffice showing Reading Time data type
  3. Save and publish content.

  4. Reading Time will display in the backoffice

    A screenshot of the BackOffice showing Reading Time

Using the value in your templates

In your template, you can accessing the Reading Time property value like any other property:

 @Model.ReadingTime.DisplayTime()

A screenshot of page showing Reading Time

Overriding the default display

The DisplayTime method will format the reading time as a string using Humanizer. This supports variants, meaning the reading time will be displayed based on the pluralisation rules of the current culture (e.g. "1 minute", "2 minutes", "0 minuter").

Min and max TimeUnit values are derived from the Data Type settings. The below example shows how you can ensure only seconds are displayed.

 Model.ReadingTime.DisplayTime(minUnit: TimeUnit.Second, maxUnit: TimeUnit.Second)

Configuration

You can change the average words per minute in the data type settings.

When creating a new data type, the default will be 200 words per minute.

Limitations

Values are derived from published content only.

Unpublished (saved) content is not included in the calculation.

Words per minute applies to all variants.

It is not currently possible to configure words per minute per culture.

Extending

You can extend the data type to support additional editors by implementing the IReadingTimeValueProvider interface.

public class MyCustomReadingTimeValueProvider : IReadingTimeValueProvider
{
    public bool CanConvert(IPropertyType type)
    {
        return type.EditorAlias == "MyCustomEditorAlias";
    }

    public TimeSpan? GetReadingTime(IProperty property, string? culture, string? segment, IEnumerable<string> availableCultures, ReadingTimeConfiguration config)
    {
        var value = property.GetValue(culture, segment, true);
        if (value is string text)
        {
            return text.GetReadingTime(config.WordsPerMinute);
        }

        return null;
     }
 }

Don't forget to register your custom value provider:

public class Composer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.ReadingTimeValueProviders().Append<MyCustomReadingTimeValueProvider>();
    }
}

Contributing

Contributions to this package are most welcome! Please read the Contributing Guidelines.

Acknowledgments (thanks!)