Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: post slug generation not following selected strategy #6914

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions ui/console-src/composables/use-slugify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@ import { FormType } from "@/types/slug";
import { randomUUID } from "@/utils/id";
import ShortUniqueId from "short-unique-id";
import { slugify } from "transliteration";
import { watch, type Ref } from "vue";
import { computed, watch, type Ref } from "vue";

const uid = new ShortUniqueId();
const Strategy = {
generateByTitle: (value: string) => {
if (!value) return "";
return slugify(value, { trim: true });
},
shortUUID: (value: string) => {
if (!value) return "";
return uid.randomUUID(8);
},
UUID: (value: string) => {
if (!value) return "";
return randomUUID();
},
timestamp: (value: string) => {
if (!value) return "";
return new Date().getTime().toString();
},

type SlugStrategy = (value?: string) => string;

const strategies: Record<string, SlugStrategy> = {
generateByTitle: (value?: string) => slugify(value || "", { trim: true }),
shortUUID: () => uid.randomUUID(8),
UUID: () => randomUUID(),
timestamp: () => new Date().getTime().toString(),
};

const onceList = ["shortUUID", "UUID", "timestamp"];
const onceStrategies = new Set(["shortUUID", "UUID", "timestamp"]);

export default function useSlugify(
source: Ref<string>,
target: Ref<string>,
auto: Ref<boolean>,
formType: FormType
) {
const handleGenerateSlug = (forceUpdate = false, formType: FormType) => {
const globalInfoStore = useGlobalInfoStore();
const mode = globalInfoStore.globalInfo?.postSlugGenerationStrategy;
const globalInfoStore = useGlobalInfoStore();

if (!mode) {
return;
}
if (formType != FormType.POST) {
target.value = Strategy["generateByTitle"](source.value);
return;
}
if (forceUpdate) {
target.value = Strategy[mode](source.value);
const currentStrategy = computed(
() =>
globalInfoStore.globalInfo?.postSlugGenerationStrategy ||
"generateByTitle"
);

const generateSlug = (value: string): string => {
const strategy =
formType === FormType.POST
? strategies[currentStrategy.value]
: strategies.generateByTitle;

return strategy(value);
};

const handleGenerateSlug = (forceUpdate = false) => {
if (
!forceUpdate &&
onceStrategies.has(currentStrategy.value) &&
target.value
) {
return;
}
if (onceList.includes(mode) && target.value) return;
target.value = Strategy[mode](source.value);

target.value = generateSlug(source.value);
};

watch(
() => source.value,
source,
() => {
if (auto.value) {
handleGenerateSlug(false, formType);
handleGenerateSlug(true);
}
},
{
immediate: true,
}
{ immediate: true }
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const { handleGenerateSlug } = useSlugify(
$t('core.page.settings.fields.slug.refresh_message')
"
class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100"
@click="handleGenerateSlug(true, FormType.SINGLE_PAGE)"
@click="handleGenerateSlug(true)"
>
<IconRefreshLine
class="h-4 w-4 text-gray-500 group-hover:text-gray-700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const { handleGenerateSlug } = useSlugify(
)
"
class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100"
@click="handleGenerateSlug(true, FormType.CATEGORY)"
@click="handleGenerateSlug(true)"
>
<IconRefreshLine
class="h-4 w-4 text-gray-500 group-hover:text-gray-700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ const showCancelPublishButton = computed(() => {
$t('core.post.settings.fields.slug.refresh_message')
"
class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100"
@click="handleGenerateSlug(true, FormType.POST)"
@click="handleGenerateSlug(true)"
>
<IconRefreshLine
class="h-4 w-4 text-gray-500 group-hover:text-gray-700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const { handleGenerateSlug } = useSlugify(
)
"
class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100"
@click="handleGenerateSlug(true, FormType.TAG)"
@click="handleGenerateSlug(true)"
>
<IconRefreshLine
class="h-4 w-4 text-gray-500 group-hover:text-gray-700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const publishTimeHelp = computed(() => {
<div
v-tooltip="$t('core.post.settings.fields.slug.refresh_message')"
class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100"
@click="handleGenerateSlug(true, FormType.POST)"
@click="handleGenerateSlug(true)"
>
<IconRefreshLine
class="h-4 w-4 text-gray-500 group-hover:text-gray-700"
Expand Down
Loading