Editor — pure text for calculating Reading time

Hi, I’m adding a “reading time” plugin to a website, and it requires purely words (no markup whatsoever) to calculate the time.

Is there a builtin method to get pure text out of the editor? Or do I need to loop through blocks and get pure text of out of those? is that even possible? Thank you!

It’s allright. Easy:

	$text = A::join(
		array_map(
			function ($block) { return strip_tags($block['content']); },
			$this->content()->Content()->blocks()->toArray()
		),
		"\n"
	);
	return (new ReadingTime($text))->setWordsPerMinute(160)->minutes();

($this->content() is there because I dumped it into page model)

I actually replaced this “rocket science” code with simpler:

public function readingTime() {
  $text = strip_tags($this->content()->Content()->blocks());
  return (new ReadingTime($text))->setWordsPerMinute(160)->minutes();
}
2 Likes