Get text word count from RedCloth
-
03-07-2021 - |
Pergunta
I've just started using RedCloth as part of a simple wiki feature in my ruby on rails app.
I'd like to be able to display a word count next to wiki pages in the index.
Given the textile pulled from the database as input, how should I go about getting an accurate word count as output?
The most obvious solution would be to simply split by /\s+/
and remove all the known textile tokens such as p.
or #
from the resulting array.
But I'm wondering if there's a more elegant (less tedious) solution, such as a feature of RedCloth or a fast and ready way of removing the markup from the RedCloth html output which would leave just whitespace separated words.
Solução
- Render RedCloth into html.
- Strip html.
- Than do word count.
Outras dicas
Your split idea is, unfortunately, probably the best one. For the sake of performance, I'd do the calculation when you save changes to a wiki page, and just warehouse that number.