友好时间显示
This commit is contained in:
浪子 2025-06-04 10:33:16 +08:00
parent 1f535bb9d6
commit 6b8f758dcf
6 changed files with 40 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
tooot.php
Parsedown.php
/assets/images/covers

View File

@ -22,6 +22,11 @@ function themeConfig($form) {
$form->addInput($twitterurl);
$mastodonurl = new Typecho_Widget_Helper_Form_Element_Text('mastodonurl', NULL, NULL, _t('mastodon'), _t('会在个人信息显示'));
$form->addInput($mastodonurl);
$friendlyTime = new Typecho_Widget_Helper_Form_Element_Radio('friendlyTime',
array('0' => _t('否'),
'1' => _t('是')),
'0', _t('是否显示友好时间'), _t('默认不显示友好时间,显示标准时间格式'));
$form->addInput($friendlyTime);
$showProfile = new Typecho_Widget_Helper_Form_Element_Radio('showProfile',
array('0'=> _t('否'), '1'=> _t('是')),
'0', _t('是否在文章页面显示作者信息'), _t('选择"是"将在文章页面包含显示作者信息。'));
@ -193,22 +198,15 @@ function getPermalinkFromCoid($coid) {
*/
class ImageStructureProcessor {
public static function processContent($content, $widget, $lastResult = null) {
// 首先应用之前的过滤器结果
$content = empty($lastResult) ? $content : $lastResult;
// 检查内容是否为空
if (empty($content) || !is_string($content)) {
return $content;
}
if ($widget instanceof Widget_Archive) {
try {
// 使用 DOM 操作确保结构完整性
$dom = new DOMDocument('1.0', 'UTF-8');
// 添加错误处理
libxml_use_internal_errors(true);
// 添加基础 HTML 结构以确保正确解析
$content = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><div>' . $content . '</div></body></html>';
// 直接加载内容到 DOM
$dom->loadHTML($content,
LIBXML_HTML_NOIMPLIED |
LIBXML_HTML_NODEFDTD |
@ -270,10 +268,6 @@ class ImageStructureProcessor {
return $content;
}
}
// u6ce8u91cau6389u8fd9u884cuff0cu9632u6b62u8986u76d6u8fd9u4e2au94a9u5b50u7684u5176u4ed6u6ce8u518c
// Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = function($content, $widget) {
// return ImageStructureProcessor::processContent($content, $widget);
// };
/**
* 处理图片为封面图裁剪为5:3最大宽度500px转换为webp
@ -759,4 +753,31 @@ class AttachmentHelper {
<?php
}
}
?>
?>
<?php
/**
* 友好时间显示函数
* @param int $time 时间戳
* @param int $threshold 阈值超过此值则显示标准日期格式Y-m-d
* @return string
*/
function time_ago($time, $threshold = 31536000) { // 31536000秒 = 1年
$now = time();
$difference = $now - $time;
// 如果时间差超过阈值默认1年则返回标准日期格式不带时间
if ($difference >= $threshold) {
return date('Y-m-d', $time);
}
// 1年以内的时间返回友好格式如 "3天前"
$periods = array("", "分钟", "小时", "", "", "个月", "");
$lengths = array("60", "60", "24", "7", "4.35", "12");
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths); $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
return $difference . $periods[$j] . "";
}

View File

@ -7,7 +7,7 @@
<img src="<?php $this->options->logoUrl() ?>" class="avatar" width="48" height="48" />
<a datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time" href="<?php $this->permalink() ?>"
itemprop="datePublished">
<?php $this->date('Y-m-d'); ?>
<?php $options = Helper::options();if ($options->friendlyTime == '1') {echo time_ago($this->created);} else {$this->date('Y-m-d H:i'); }?>
</a>
</header>
<div class="description" itemprop="about">

View File

@ -22,7 +22,7 @@
<img src="<?php $this->options->logoUrl() ?>" class="avatar" width="48" height="48" />
<a datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time" href="<?php $this->permalink() ?>"
itemprop="datePublished">
<?php $this->date('Y-m-d H:i'); ?>
<?php $options = Helper::options();if ($options->friendlyTime == '1') {echo time_ago($this->created);} else {$this->date('Y-m-d H:i'); }?>
</a>
</header>
<div class="description" itemprop="about">
@ -58,7 +58,7 @@
<div class="meta">
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16"><path d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z"></path></svg>
<time>
<?php $this->date('Y-m-d'); ?>
<?php $options = Helper::options();if ($options->friendlyTime == '1') {echo time_ago($this->created);} else {$this->date('Y-m-d H:i'); }?>
</time>
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16"><path d="M408.551619 97.52381a73.142857 73.142857 0 0 1 51.736381 21.430857L539.306667 197.973333A73.142857 73.142857 0 0 0 591.067429 219.428571H804.571429a73.142857 73.142857 0 0 1 73.142857 73.142858v560.761904a73.142857 73.142857 0 0 1-73.142857 73.142857H219.428571a73.142857 73.142857 0 0 1-73.142857-73.142857V170.666667a73.142857 73.142857 0 0 1 73.142857-73.142857h189.123048z m0 73.142857H219.428571v682.666666h585.142858V292.571429h-213.504a146.285714 146.285714 0 0 1-98.499048-38.13181L487.619048 249.734095 408.551619 170.666667zM365.714286 633.904762v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z m-365.714285-195.047619v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z"></path></svg>
<?php $this->category(','); ?>

View File

@ -16,7 +16,7 @@
$imageToDisplay = process_cover_image($imageToDisplay);
}
?>
<article class="post--card" id="loadpost" itemscope itemtype="http://schema.org/Article">
<article class="post--card" id="loadpost">
<img src="<?php echo $imageToDisplay; ?>" alt="<?php $this->title() ?>" class="cover" itemprop="image"/>
<div class="content">
<h2 class="post--title">
@ -32,7 +32,7 @@
<path d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z"></path>
</svg>
<time datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time">
<?php $this->date('Y-m-d'); ?>
<?php $options = Helper::options();if ($options->friendlyTime == '1') {echo time_ago($this->created);} else {$this->date('Y-m-d H:i'); }?>
</time>
</div>
</div>

View File

@ -9,7 +9,7 @@
<path
d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z">
</path>
</svg><time class="humane--time"><?php $this->date('Y-m-d'); ?></time>
</svg><time class="humane--time"><?php $options = Helper::options();if ($options->friendlyTime == '1') {echo time_ago($this->created);} else {$this->date('Y-m-d H:i'); }?></time>
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16">
<path
d="M408.551619 97.52381a73.142857 73.142857 0 0 1 51.736381 21.430857L539.306667 197.973333A73.142857 73.142857 0 0 0 591.067429 219.428571H804.571429a73.142857 73.142857 0 0 1 73.142857 73.142858v560.761904a73.142857 73.142857 0 0 1-73.142857 73.142857H219.428571a73.142857 73.142857 0 0 1-73.142857-73.142857V170.666667a73.142857 73.142857 0 0 1 73.142857-73.142857h189.123048z m0 73.142857H219.428571v682.666666h585.142858V292.571429h-213.504a146.285714 146.285714 0 0 1-98.499048-38.13181L487.619048 249.734095 408.551619 170.666667zM365.714286 633.904762v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z m-365.714285-195.047619v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z">