Skip to content

Commit

Permalink
短縮されたURLを元のURLに変換する処理を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
shun91 committed Apr 1, 2024
1 parent 3dc3882 commit 100435f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/createNotionPageByTweet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function createNotionPageByTweet({
},
text: {
type: "rich_text",
rich_text: parseTextAndUrl(text),
rich_text: await parseTextAndUrl(text),
},
url: {
url: url,
Expand Down
12 changes: 6 additions & 6 deletions src/parseTextAndUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseTextAndUrl } from "./parseTextAndUrl"; // 適切なパスに置き換えてください
import { test, expect } from "vitest";

test("URLを含むテキストをパースする", () => {
test("URLを含むテキストをパースする", async () => {
const testInput =
"こんにちは、このリンクを見てください:https://example.com またこちらも:https://openai.com";
const expectedOutput = [
Expand Down Expand Up @@ -37,12 +37,12 @@ test("URLを含むテキストをパースする", () => {
},
];

const result = parseTextAndUrl(testInput);
const result = await parseTextAndUrl(testInput);

expect(result).toEqual(expectedOutput);
});

test("URLを含まないテキストをパースする", () => {
test("URLを含まないテキストをパースする", async () => {
const testInput = "こんにちは、このテキストにはURLが含まれていません";
const expectedOutput = [
{
Expand All @@ -53,12 +53,12 @@ test("URLを含まないテキストをパースする", () => {
},
];

const result = parseTextAndUrl(testInput);
const result = await parseTextAndUrl(testInput);

expect(result).toEqual(expectedOutput);
});

test("テキストがURLのみである場合のパース", () => {
test("テキストがURLのみである場合のパース", async () => {
const testInput = "https://onlyurl.com";
const expectedOutput = [
{
Expand All @@ -72,7 +72,7 @@ test("テキストがURLのみである場合のパース", () => {
},
];

const result = parseTextAndUrl(testInput);
const result = await parseTextAndUrl(testInput);

expect(result).toEqual(expectedOutput);
});
26 changes: 24 additions & 2 deletions src/parseTextAndUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
export function parseTextAndUrl(input: string) {
/**
* 短縮されたURLを元のURLに変換する
*/
async function replaceShortUrl(shortUrl: string) {
try {
// fetchリクエストを送信し、リダイレクトを手動で処理する
const response = await fetch(shortUrl, { redirect: "manual" });

if (response.status >= 300 && response.status < 400) {
// リダイレクトレスポンスの場合、LocationヘッダーからURLを取得
return response.headers.get("Location") ?? shortUrl;
}

// リダイレクトがない場合は、元のURLをそのまま返す
return shortUrl;
} catch (error) {
// エラーで元のURLを取得できなかった場合は、短縮URLをそのまま返す
console.warn(error);
return shortUrl;
}
}

export async function parseTextAndUrl(input: string) {
// Define regex to detect URLs in the input string
const urlRegex = /(https?:\/\/[^\s]+)/g;

Expand All @@ -8,7 +30,7 @@ export function parseTextAndUrl(input: string) {

// Loop over the input string to find URLs and split the text around them
while ((match = urlRegex.exec(input)) !== null) {
const matchedUrl = match[0];
const matchedUrl = await replaceShortUrl(match[0]);

// Extract the text before the URL
if (match.index > lastIndex) {
Expand Down

0 comments on commit 100435f

Please sign in to comment.