|
@@ -7,6 +7,7 @@ use App\Facades\RedisCache;
|
|
|
use App\Models\Channel;
|
|
use App\Models\Channel;
|
|
|
use App\Models\DhammaTerm;
|
|
use App\Models\DhammaTerm;
|
|
|
use App\Models\TermCategory;
|
|
use App\Models\TermCategory;
|
|
|
|
|
+use App\Services\ChannelService;
|
|
|
use App\Services\Markup\Format;
|
|
use App\Services\Markup\Format;
|
|
|
use App\Services\Markup\MarkupService;
|
|
use App\Services\Markup\MarkupService;
|
|
|
use App\Services\Markup\RenderOptions;
|
|
use App\Services\Markup\RenderOptions;
|
|
@@ -60,6 +61,7 @@ class TermService
|
|
|
private readonly AgentGrantService $agents,
|
|
private readonly AgentGrantService $agents,
|
|
|
private readonly MarkupService $markup,
|
|
private readonly MarkupService $markup,
|
|
|
private readonly UserService $users,
|
|
private readonly UserService $users,
|
|
|
|
|
+ private readonly ChannelService $channelSummaries,
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------ 读 */
|
|
/* ------------------------------------------------------------------ 读 */
|
|
@@ -475,21 +477,17 @@ class TermService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 预加载 {@see self::relations()} 与所属 channel。
|
|
|
|
|
- *
|
|
|
|
|
- * channel 只给真属于某个 channel 的加载:studio 级的老数据 channal 是空串,
|
|
|
|
|
- * 统一 `with('channel')` 会拿空串去比 uuid 列,PostgreSQL 直接报错。
|
|
|
|
|
|
|
+ * 预加载 {@see self::relations()},并挂上所属 channel 的摘要 `channel_summary`({@see ChannelService::summaries()},
|
|
|
|
|
+ * 整批一次查询;studio 级术语与 channel 已不存在的是 null)。
|
|
|
*
|
|
*
|
|
|
* @param EloquentCollection<int, DhammaTerm> $terms
|
|
* @param EloquentCollection<int, DhammaTerm> $terms
|
|
|
*/
|
|
*/
|
|
|
private function loadRelations(EloquentCollection $terms): void
|
|
private function loadRelations(EloquentCollection $terms): void
|
|
|
{
|
|
{
|
|
|
$terms->load($this->relations());
|
|
$terms->load($this->relations());
|
|
|
- $terms->filter(fn (DhammaTerm $term) => $term->channelUid() !== null)->load('channel:uid,name');
|
|
|
|
|
- $terms->each(function (DhammaTerm $term) {
|
|
|
|
|
- if ($term->channelUid() === null) {
|
|
|
|
|
- $term->setRelation('channel', null);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $summaries = $this->channelSummaries->summaries($terms->map(fn (DhammaTerm $term) => $term->channelUid())->all());
|
|
|
|
|
+ $terms->values()->each(function (DhammaTerm $term, int $i) use ($summaries) {
|
|
|
|
|
+ $term->channel_summary = $summaries[$i] ?: null;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -517,7 +515,7 @@ class TermService
|
|
|
'contentType' => $term->effectiveContentType(),
|
|
'contentType' => $term->effectiveContentType(),
|
|
|
];
|
|
];
|
|
|
$summary = trim((string) $term->summary);
|
|
$summary = trim((string) $term->summary);
|
|
|
- $items["{$term->guid}|summary"] = $base + ['source' => $summary !== '' ? $summary : (string) $term->note];
|
|
|
|
|
|
|
+ $items["{$term->guid}|summary"] = $base + ['source' => $summary !== '' ? $summary : $this->firstParagraph((string) $term->note)];
|
|
|
if ($withBody) {
|
|
if ($withBody) {
|
|
|
$bodyChannels = ! empty($render['channels']) ? $render['channels'] : $base['channels'];
|
|
$bodyChannels = ! empty($render['channels']) ? $render['channels'] : $base['channels'];
|
|
|
$items["{$term->guid}|body"] = ['channels' => $bodyChannels] + $base + ['source' => trim($summary."\n\n".(string) $term->note)];
|
|
$items["{$term->guid}|body"] = ['channels' => $bodyChannels] + $base + ['source' => trim($summary."\n\n".(string) $term->note)];
|
|
@@ -533,7 +531,7 @@ class TermService
|
|
|
foreach ($terms as $term) {
|
|
foreach ($terms as $term) {
|
|
|
$term->abilities = $abilities[$term->guid];
|
|
$term->abilities = $abilities[$term->guid];
|
|
|
$text = (string) $rendered[Format::Text->value]["{$term->guid}|summary"];
|
|
$text = (string) $rendered[Format::Text->value]["{$term->guid}|summary"];
|
|
|
- $term->summary_text = trim((string) $term->summary) !== '' ? trim($text) : $this->firstParagraph($text);
|
|
|
|
|
|
|
+ $term->summary_text = trim($text);
|
|
|
if ($withBody && $bodyFormat === Format::Html) {
|
|
if ($withBody && $bodyFormat === Format::Html) {
|
|
|
$term->html = (string) $rendered[Format::Html->value]["{$term->guid}|body"];
|
|
$term->html = (string) $rendered[Format::Html->value]["{$term->guid}|body"];
|
|
|
} elseif ($withBody) {
|
|
} elseif ($withBody) {
|
|
@@ -543,18 +541,45 @@ class TermService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 纯文本渲染({@see Format::Text})里段落之间只隔一个换行,第一个非空行就是第一段。
|
|
|
|
|
|
|
+ * 正文源码(markdown / markdown-e2)的第一个自然段,渲染前在源码上取。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 不能渲染后再切:纯文本渲染里段落之间与段内换行都只是一个 `\n`,分不出段。
|
|
|
|
|
+ * 自然段以空行分隔(两种方言相同),围栏(```)内的空行不算;跳过标题、围栏、分隔线、
|
|
|
|
|
+ * 以及只由模板组成的段(`{{toc}}` 等);段首只由模板组成的行(如 `{{ai}}` 提示)去掉。
|
|
|
|
|
+ * 一段也没有时退回整篇。
|
|
|
*/
|
|
*/
|
|
|
- private function firstParagraph(string $text): string
|
|
|
|
|
- {
|
|
|
|
|
- foreach (preg_split('/\n/u', $text) ?: [] as $paragraph) {
|
|
|
|
|
- $paragraph = trim($paragraph);
|
|
|
|
|
- if ($paragraph !== '') {
|
|
|
|
|
- return $paragraph;
|
|
|
|
|
|
|
+ private function firstParagraph(string $source): string
|
|
|
|
|
+ {
|
|
|
|
|
+ $blocks = [];
|
|
|
|
|
+ $lines = [];
|
|
|
|
|
+ $fenced = false;
|
|
|
|
|
+ foreach (preg_split('/\R/u', $source) ?: [] as $line) {
|
|
|
|
|
+ if (preg_match('/^\s*(```|~~~)/u', $line) === 1) {
|
|
|
|
|
+ $fenced = ! $fenced;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (! $fenced && trim($line) === '') {
|
|
|
|
|
+ $blocks[] = $lines;
|
|
|
|
|
+ $lines = [];
|
|
|
|
|
+
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
+ $lines[] = $line;
|
|
|
|
|
+ }
|
|
|
|
|
+ $blocks[] = $lines;
|
|
|
|
|
+
|
|
|
|
|
+ $templateOnly = '/^\s*(?:(?<t>\{\{(?:[^{}]++|(?&t))*\}\})\s*)+$/u';
|
|
|
|
|
+ foreach ($blocks as $lines) {
|
|
|
|
|
+ while ($lines !== [] && preg_match($templateOnly, $lines[0]) === 1) {
|
|
|
|
|
+ array_shift($lines);
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($lines === [] || preg_match('/^\s*(#|```|~~~|([-*_])\s*\2\s*\2[\s\-*_]*$)/u', $lines[0]) === 1) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return implode("\n", $lines);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return '';
|
|
|
|
|
|
|
+ return $source;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|