From 39a3ccb99b3fb63e9bac439c6b5d8784f63e2209 Mon Sep 17 00:00:00 2001 From: Maiko Sinkyaet Tan Date: Wed, 31 Jul 2024 16:08:50 +0800 Subject: [PATCH] chore(pixiv): optimise commet, confirm regex usable (#238) --- packages/pixiv/package.json | 1 + packages/pixiv/src/utils.ts | 35 ----------------------------------- packages/pixiv/src/utils.tsx | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 35 deletions(-) delete mode 100644 packages/pixiv/src/utils.ts create mode 100644 packages/pixiv/src/utils.tsx diff --git a/packages/pixiv/package.json b/packages/pixiv/package.json index 89bb3b958..d32d3ae80 100644 --- a/packages/pixiv/package.json +++ b/packages/pixiv/package.json @@ -66,6 +66,7 @@ "devDependencies": { "@koishijs/assets": "^1.0.4", "@koishijs/plugin-server": "^3.1.8", + "@satorijs/element": "^3.0.0", "koishi": "^4.17.3" }, "publishConfig": { diff --git a/packages/pixiv/src/utils.ts b/packages/pixiv/src/utils.ts deleted file mode 100644 index 2b0497b5b..000000000 --- a/packages/pixiv/src/utils.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* eslint-disable brace-style */ -import { Element, h } from 'koishi' - -export function normaliseCaption(caption: string): Element { - if (!caption?.trim()) { - return h.text('') - } - - return h( - '', - h.transform(h.parse(caption), { - a(attrs) { - let url = (attrs['href'] || '') as string - if (url) { - let m: RegExpMatchArray | null = null - // Convert pixiv://users/1234 to https://www.pixiv.net/u/1234 - if (((m = /pixiv:\/\/users\/(?\d+)/.exec(url)), m?.groups?.id)) { - url = `https://www.pixiv.net/u/${m.groups.id}` - } - // FIXME: Not confirmed yet - // Convert pixiv://illusts/1234 to https://www.pixiv.net/i/1234 - else if (((m = /pixiv:\/\/illusts\/(?\d+)/.exec(url)), m?.groups?.id)) { - url = `https://www.pixiv.net/i/${m.groups.id}` - } - // FIXME: Not confirmed yet - // Convert pixiv://novels/1234 to https://www.pixiv.net/novel/show.php?id=1234 - else if (((m = /pixiv:\/\/novels\/(?\d+)/.exec(url)), m?.groups?.id)) { - url = `https://www.pixiv.net/novel/show.php?id=${m.groups.id}` - } - } - return h('a', { href: url }, ...(attrs.children || [])) - }, - }), - ) -} diff --git a/packages/pixiv/src/utils.tsx b/packages/pixiv/src/utils.tsx new file mode 100644 index 000000000..89e8b1bdf --- /dev/null +++ b/packages/pixiv/src/utils.tsx @@ -0,0 +1,35 @@ +/* eslint-disable brace-style */ +import { Element, h } from 'koishi' + +export function normaliseCaption(caption: string): Element { + if (!caption?.trim()) { + return + } + + return ( + <> + {h.transform(h.parse(caption), { + a(attrs) { + let url = (attrs['href'] || '') as string + if (url) { + let m: RegExpMatchArray | null = null + // Convert pixiv://users/1234 to https://www.pixiv.net/u/1234 + if (((m = /pixiv:\/\/users\/(?\d+)/.exec(url)), m?.groups?.id)) { + url = `https://www.pixiv.net/u/${m.groups.id}` + } + // Convert pixiv://illusts/1234 to https://www.pixiv.net/i/1234 + else if (((m = /pixiv:\/\/illusts\/(?\d+)/.exec(url)), m?.groups?.id)) { + url = `https://www.pixiv.net/i/${m.groups.id}` + } + // Convert pixiv://novels/1234 to https://www.pixiv.net/novel/show.php?id=1234 + else if (((m = /pixiv:\/\/novels\/(?\d+)/.exec(url)), m?.groups?.id)) { + url = `https://www.pixiv.net/novel/show.php?id=${m.groups.id}` + } + // There are also `twitter/` link, since its href is just a valid URL, we don't need to handle it + } + return {attrs.children || []} + }, + })} + + ) +}