From 2cf0d6853ac001a9ef08d5fd6279cb581d9b7d62 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Sun, 27 Jul 2025 13:19:17 +0800 Subject: [PATCH] feat: add slug existence check when creating posts (#7617) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /area ui /kind improvement /milestone 2.21.x #### What this PR does / why we need it: This PR adds frontend support for checking whether an slug already exists when creating posts. Note: 1. The current implementation isn’t perfect, some actions (like clicking the “Publish” button) don’t check for duplicate slug yet. 2. Slug checking in the user center might not be accurate, since it may not have permission to query all posts. image #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/7615 Fixes https://github.com/halo-dev/halo/issues/3332 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note 创建文章时支持检查别名是否重复 ``` --- .../components/SinglePageSettingModal.vue | 32 ++++++++++- .../modules/contents/posts/PostEditor.vue | 52 ++++++++++------- .../posts/components/PostSettingModal.vue | 33 ++++++++++- .../modules/contents/posts/PostEditor.vue | 56 ++++++++++++------- .../posts/components/PostSettingEditModal.vue | 25 +++++---- .../posts/components/PostSettingForm.vue | 34 ++++++++++- 6 files changed, 176 insertions(+), 56 deletions(-) diff --git a/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue b/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue index a5099485b..f1d81b984 100644 --- a/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue +++ b/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue @@ -6,7 +6,7 @@ import { toDatetimeLocal, toISOString } from "@/utils/date"; import { randomUUID } from "@/utils/id"; import useSlugify from "@console/composables/use-slugify"; import { useThemeCustomTemplates } from "@console/modules/interface/themes/composables/use-theme"; -import { submitForm } from "@formkit/core"; +import { submitForm, type FormKitNode } from "@formkit/core"; import type { SinglePage } from "@halo-dev/api-client"; import { coreApiClient } from "@halo-dev/api-client"; import { @@ -266,6 +266,28 @@ const { handleGenerateSlug } = useSlugify( computed(() => !isUpdateMode), FormType.SINGLE_PAGE ); + +// fixme: check if slug is unique +// Finally, we need to check if the slug is unique in the database +async function slugUniqueValidation(node: FormKitNode) { + const value = node.value; + if (!value) { + return true; + } + + const fieldSelector = [`spec.slug=${value}`]; + + if (isUpdateMode) { + fieldSelector.push(`metadata.name!=${formState.value.metadata.name}`); + } + + const { data: pagesWithSameSlug } = + await coreApiClient.content.singlePage.listSinglePage({ + fieldSelector, + }); + + return !pagesWithSameSlug.total; +}