Respect site-normalized Markdown when reformatting a published review

This commit is contained in:
TTC Maintenance committed 2026-12-03 12:50:13 +08:00
1 parent 4f698df655
commit 77e111252f
2 files changed
+28 -10

No files matched your search

@@ -41,23 +41,28 @@ final class ReformatPublished
foreach ($records as $x) {
abort_unless($x['status'] === 'published' && $x['approval_id'] === $job['approval_id'] && $x['source_key'] === $artifact['source_key'], 409);
}
$current = file_get_contents($path);
$siteMarkdown = Publication::siteMarkdown($artifact['markdown']);
if ($records[0]['artifact_hash'] === $hash && $records[1]['artifact_hash'] === $hash) {
abort_unless(file_get_contents($path) === $artifact['markdown'], 409);
abort_unless(in_array($current, [$artifact['markdown'], $siteMarkdown], true), 409);
return ['artifact' => $artifact, 'artifact_hash' => $hash, 'receipt' => $records[0], 'changed' => false];
}
abort_unless($records[0]['artifact_hash'] === $job['expected_hash'] && $records[1]['artifact_hash'] === $job['expected_hash'] && file_get_contents($path) === $old['markdown'], 409);
$originalMarkdown = [$old['markdown'], Publication::siteMarkdown($old['markdown'])];
abort_unless($records[0]['artifact_hash'] === $job['expected_hash']
&& $records[1]['artifact_hash'] === $job['expected_hash']
&& in_array($current, $originalMarkdown, true), 409);
$backup = $dir.'/reformat-'.$job['approval_id'].'-'.$hash.'.json';
if (! is_file($backup)) {
ArticleDocument::write($backup, json_encode(['artifact' => $old, 'records' => $records], JSON_THROW_ON_ERROR));
ArticleDocument::write($backup, json_encode(['artifact' => $old, 'site_markdown' => $current, 'records' => $records], JSON_THROW_ON_ERROR));
chmod($backup, 0600);
}
try {
ArticleDocument::write($path, $artifact['markdown']);
ArticleDocument::write($path, $siteMarkdown);
(new WeeklyRanking($p))->renderCurrent();
$p->build();
} catch (\Throwable $e) {
ArticleDocument::write($path, $old['markdown']);
ArticleDocument::write($path, $current);
throw new \RuntimeException('格式修复构建失败,原文章已恢复。', 0, $e);
}
$record = $records[0];
+18 -5
View File
@@ -149,12 +149,12 @@ final class ContentCenterTest extends TestCase
}
}
private function repairJob(): array
private function repairJob(bool $normalizeEmptyTags = false): array
{
$input = $this->reviewInput();
$old = app(ArticleArtifact::class)->build($input);
File::makeDirectory($this->root.'/vuepress/docs/.vuepress', 0755, true);
$old['markdown'] = "---\ntitle: legacy\n---\n<div v-pre>legacy</div>";
$old['markdown'] = "---\ntitle: legacy\ntags: { }\n---\n<div v-pre>legacy</div>";
unset($old['title'],$old['format_version']);
$hash = ArticleArtifact::hash($old);
$id = (string) Str::uuid();
@@ -164,7 +164,8 @@ final class ContentCenterTest extends TestCase
foreach ([hash('sha256', $input['source_key']), $id] as $key) {
file_put_contents($dir.'/'.$key.'.json', json_encode($receipt));
}
file_put_contents($this->root.'/vuepress/docs/'.$old['relative_path'], $old['markdown']);
$markdown = $normalizeEmptyTags ? Publication::siteMarkdown($old['markdown']) : $old['markdown'];
file_put_contents($this->root.'/vuepress/docs/'.$old['relative_path'], $markdown);
return ['input' => $input, 'old_artifact' => $old, 'expected_hash' => $hash, 'approval_id' => $id];
}
@@ -179,6 +180,18 @@ final class ContentCenterTest extends TestCase
$this->assertFalse(app(ReformatPublished::class)->run($job)['changed']);
}
public function test_format_repair_accepts_site_normalized_empty_tags(): void
{
$job = $this->repairJob(normalizeEmptyTags: true);
$path = $this->root.'/vuepress/docs/'.$job['old_artifact']['relative_path'];
$this->assertStringContainsString('tags: []', file_get_contents($path));
$repair = app(ReformatPublished::class)->run($job);
$this->assertTrue($repair['changed']);
$this->assertSame(Publication::siteMarkdown($repair['artifact']['markdown']), file_get_contents($path));
$this->assertFalse(app(ReformatPublished::class)->run($job)['changed']);
}
public function test_format_repair_rejects_an_unreviewed_replacement_hash(): void
{
$job = $this->repairJob();
@@ -196,13 +209,13 @@ final class ContentCenterTest extends TestCase
public function test_format_repair_restores_old_article_on_build_failure(): void
{
$job = $this->repairJob();
$job = $this->repairJob(normalizeEmptyTags: true);
file_put_contents($this->root.'/vuepress/node_modules/.bin/vuepress', '<?php exit(1);');
try {
app(ReformatPublished::class)->run($job);
$this->fail('Expected failure');
} catch (\RuntimeException) {
$this->assertSame($job['old_artifact']['markdown'], file_get_contents($this->root.'/vuepress/docs/'.$job['old_artifact']['relative_path']));
$this->assertSame(Publication::siteMarkdown($job['old_artifact']['markdown']), file_get_contents($this->root.'/vuepress/docs/'.$job['old_artifact']['relative_path']));
}
}