From 292d8636f01fd73875f2d20dcd82fe8c94eb84a2 Mon Sep 17 00:00:00 2001 From: Takeshi Kakeda Date: Sun, 4 Dec 2022 21:57:21 +0900 Subject: [PATCH] Fixed issue which slug became empty when title is only non-alphabetic. --- .../create-long-post.component.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/create-long-post-page/create-long-post/create-long-post.component.ts b/src/app/create-long-post-page/create-long-post/create-long-post.component.ts index 4e7925efb..33408a122 100644 --- a/src/app/create-long-post-page/create-long-post/create-long-post.component.ts +++ b/src/app/create-long-post-page/create-long-post/create-long-post.component.ts @@ -10,6 +10,7 @@ import { BackendApiService, GetSinglePostResponse, ProfileEntryResponse } from " import { GlobalVarsService } from "src/app/global-vars.service"; import { environment } from "src/environments/environment"; import { dataURLtoFile, fileToDataURL } from "src/lib/helpers/data-url-helpers"; +import { createHash } from "crypto-browserify"; const RANDOM_MOVIE_QUOTES = [ "feed_create_post.quotes.quote1", @@ -272,8 +273,7 @@ export class CreateLongPostComponent implements AfterViewInit { ); return; } - - const titleSlug = stringToSlug(this.model.Title); + const titleSlug = tilteToSlug(this.model.Title); const existingSlugMappings = JSON.parse(currentUserProfile.ExtraData?.BlogSlugMap ?? "{}"); // check that there is not a collision with a previous article slug @@ -513,3 +513,14 @@ const stringToSlug = (str: string) => .trim() .replace(/\s+/g, "-") // replace all spaces with - .replace(/-+/g, "-"); // replace multiple - with a single - + +// stringToSlug removes all non-alphabetic characters from title. +// An i18n title was converted by stringToSlug, then slug may be empty. +// So when slug is empty, title converted to SHA1 hash is used as slug. +const tilteToSlug = (title: string) => { + const slug = stringToSlug(title); + if (slug) { + return slug; + } + return createHash("SHA1").update(title).digest("hex"); +} \ No newline at end of file