From 77e111252f652ca38ca48ccafc58eb040efa7ca5 Mon Sep 17 00:00:00 2001 From: TTC Maintenance Date: Thu, 3 Dec 2026 12:50:13 +0800 Subject: [PATCH] Respect site-normalized Markdown when reformatting a published review --- .../app/ContentCenter/ReformatPublished.php | 15 ++++++++---- laravel/tests/Feature/ContentCenterTest.php | 23 +++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/laravel/app/ContentCenter/ReformatPublished.php b/laravel/app/ContentCenter/ReformatPublished.php index 5b288d06..2b96e821 100644 --- a/laravel/app/ContentCenter/ReformatPublished.php +++ b/laravel/app/ContentCenter/ReformatPublished.php @@ -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]; diff --git a/laravel/tests/Feature/ContentCenterTest.php b/laravel/tests/Feature/ContentCenterTest.php index a8b334cf..0b9b13c5 100644 --- a/laravel/tests/Feature/ContentCenterTest.php +++ b/laravel/tests/Feature/ContentCenterTest.php @@ -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
legacy
"; + $old['markdown'] = "---\ntitle: legacy\ntags: { }\n---\n
legacy
"; 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', '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'])); } }