Skip to content

Commit

Permalink
chore(pixiv): optimise commet, confirm regex usable (koishijs#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan authored Jul 31, 2024
1 parent 136fd41 commit 39a3ccb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/pixiv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
35 changes: 0 additions & 35 deletions packages/pixiv/src/utils.ts

This file was deleted.

35 changes: 35 additions & 0 deletions packages/pixiv/src/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable brace-style */
import { Element, h } from 'koishi'

export function normaliseCaption(caption: string): Element {
if (!caption?.trim()) {
return <text></text>
}

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\/(?<id>\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\/(?<id>\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\/(?<id>\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 <a href={url}>{attrs.children || []}</a>
},
})}
</>
)
}

0 comments on commit 39a3ccb

Please sign in to comment.