From 0a581e1739f50bddd4a1a291b7c4081a070eae09 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Tue, 1 Mar 2022 07:48:52 +0900 Subject: [PATCH 1/2] :sparkles: Enable to create readable titles for URL --- title.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- title.ts | 10 ++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/title.test.ts b/title.test.ts index 0ab78d5..42d2134 100644 --- a/title.test.ts +++ b/title.test.ts @@ -1,4 +1,9 @@ -import { encodeTitleURI, revertTitleLc, toTitleLc } from "./title.ts"; +import { + encodeTitleURI, + revertTitleLc, + toReadableTitleURI, + toTitleLc, +} from "./title.ts"; import { assertStrictEquals } from "./deps/testing.ts"; Deno.test("toTitleLc()", async (t) => { @@ -37,3 +42,37 @@ Deno.test("encodeTitleURI()", async (t) => { assertStrictEquals(encodeTitleURI(":title:"), ":title%3A"); }); }); + +Deno.test("toReadableTitleURI()", async (t) => { + await t.step("only \\w", () => { + assertStrictEquals( + toReadableTitleURI("Normal_TitleAAA"), + "Normal_TitleAAA", + ); + }); + + await t.step("with sparce", () => { + assertStrictEquals( + toReadableTitleURI("Title with Spaces"), + "Title_with_Spaces", + ); + }); + + await t.step("with multibyte characters", () => { + assertStrictEquals( + toReadableTitleURI("日本語_(絵文字✨つき) タイトル"), + "日本語_(絵文字✨つき) タイトル", + ); + }); + + await t.step("encoding //", () => { + assertStrictEquals( + toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"), + "スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です", + ); + assertStrictEquals( + toReadableTitleURI("%2Fなども/と同様percent encodingされる"), + "%252Fなども%2Fと同様percent_encodingされる", + ); + }); +}); diff --git a/title.ts b/title.ts index c960b3b..9b7eaf5 100644 --- a/title.ts +++ b/title.ts @@ -40,3 +40,13 @@ export const encodeTitleURI = (title: string): string => { const noEncodeChars = '@$&+=:;",'; const noTailChars = ':;",'; + +/** titleをできるだけpercent encodingせずにURIで使える形式にする + * + * @param title 変換するtitle + * @return 変換後の文字列 + */ +export const toReadableTitleURI = (title: string): string => { + return title.replaceAll(" ", "_") + .replace(/[/?#\{}^|<>]/g, (char) => encodeURIComponent(char)); +}; From cc067ffd86082d54ec7ff25b7546e6dfcb63f653 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Tue, 1 Mar 2022 07:52:35 +0900 Subject: [PATCH 2/2] :bug: Forgot to encode % --- title.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/title.ts b/title.ts index 9b7eaf5..5b6ca10 100644 --- a/title.ts +++ b/title.ts @@ -48,5 +48,5 @@ const noTailChars = ':;",'; */ export const toReadableTitleURI = (title: string): string => { return title.replaceAll(" ", "_") - .replace(/[/?#\{}^|<>]/g, (char) => encodeURIComponent(char)); + .replace(/[/?#\{}^|<>%]/g, (char) => encodeURIComponent(char)); };