This commit is contained in:
parent
41640e0b28
commit
b36abcd3bf
12 changed files with 86 additions and 8 deletions
|
@ -1,19 +1,33 @@
|
|||
<?php
|
||||
|
||||
use Kirby\Cms\Page;
|
||||
|
||||
/**
|
||||
* @method object categories()
|
||||
* @method object tags()
|
||||
* @method object date()
|
||||
* @method object text()
|
||||
*/
|
||||
class ArticlePage extends Page
|
||||
{
|
||||
private const READING_TIME_FORMAT = '%d words, ~%d min reading time';
|
||||
|
||||
/**
|
||||
* @param array|string|null $options
|
||||
*/
|
||||
public function url($options = null): string
|
||||
{
|
||||
$date = $this->date()->toDate('Y/m/d');
|
||||
return '/' . $date .'/' . $this->slug();
|
||||
}
|
||||
|
||||
public function readingTime() {
|
||||
public function readingTime(): string
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
$doc->loadHtml(
|
||||
"<html><head><meta charset=\"UTF-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>"
|
||||
. $this->text()->kirbytext()
|
||||
."</body></html>"
|
||||
$doc->loadHTML(
|
||||
'<html><head><meta charset="UTF-8"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>'
|
||||
. $this->text()->kirbytext()
|
||||
. '</body></html>'
|
||||
);
|
||||
$pElems = $doc->getElementsByTagName('p');
|
||||
|
||||
|
@ -25,6 +39,6 @@ class ArticlePage extends Page
|
|||
$wordCount = count(explode(' ', $text));
|
||||
$readingTime = (int)ceil($wordCount / 150);
|
||||
|
||||
return $wordCount . ' words, ~' . $readingTime . 'min reading time';
|
||||
return sprintf(self::READING_TIME_FORMAT, $wordCount, $readingTime);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue