typemill / typemill

Typemill is a lightweight, flat-file CMS designed for simple, fast, and flexible website and eBook creation using Markdown.
https://typemill.net
MIT License
427 stars 60 forks source link

Reading time? #359

Closed thopanX closed 1 year ago

thopanX commented 1 year ago

Can somebody give me a hint on how to get the number of characters in the content variable–so I can calculate an approximate reading time? I found twig's length filter, but when I try to output it via {{content|length}} the webserver returns an error 500.

trendschau commented 1 year ago

the content variable outputs html and the length filter does not accept it. You should create a plugin that counts the length of the pure markdown content and then output it in the template.

thopanX commented 1 year ago

Alright, thanks.

12.09.2022 09:54:15 trendschau @.***>:

the content variable outputs html and the length filter does not accept it. You should create a plugin that counts the length of the pure markdown content and then output it in the template.

— Reply to this email directly, view it on GitHub[https://github.com/typemill/typemill/issues/359#issuecomment-1243351906], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AAN3C4LCSM52ESSIF6VSWNDV53ORTANCNFSM6AAAAAAQJBIQT4]. You are receiving this because you authored the thread.[Verfolgungsbild][https://github.com/notifications/beacon/AAN3C4KN5KBIYBAXDQNGR23V53ORTA5CNFSM6AAAAAAQJBIQT6WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTSKDQFWE.gif]

trendschau commented 1 year ago

or maybe even better: try a frontend solution with JS, I am sure that there are tons of libraries for that :D

thopanX commented 1 year ago

I've decided to learn how to code plugins for Typemill, and am working through the documentation/tutorial. Found a 404:

https://typemill.net/plugin-developers/tutorial/write-the-php-file > You can find a list of all events in the Event Overview in the documentation](https://typemill.net/for-plugin-developers/documentation/event-overview). > The correct URL is https://typemill.net/plugin-developers/event-overview

Same 404 in https://typemill.net/plugin-developers/tutorial/add-methods > You can find more details in the [event overview](https://typemill.net/for-plugin-developers/documentation/event-overview).

trendschau commented 1 year ago

I have to correct myself: content|length in templates work totally fine for me and it is also possible to strip tags with content|striptags|length

I will close this because there is no issue.

thopanX commented 1 year ago

For other users, this is what I've come up with without any plugin work, just using Twig's features:

Auf Deutsch: https://thopan.uber.space/blog/lesezeit-hinzugefuegt

Appr. Reading time: {% set contentLength = content|striptags|length %} {{ contentLength // 1000 }} minutes

If the content's length is less than 1.000 characters and thus returns “0 minutes“ (the output is floored), you can wrap the stuff with an if-statement and output something like “less than a minute“ in this case.

Hope this helps.

trendschau commented 1 year ago

thank you for sharing!