diff --git a/src/bot/song-ref.test.ts b/src/bot/song-ref.test.ts index 540183a..64fb568 100644 --- a/src/bot/song-ref.test.ts +++ b/src/bot/song-ref.test.ts @@ -110,6 +110,8 @@ describe("parseSongRef (#90 exact-song selection)", () => { "https://www.bilibili.com/video/BV1yxHQeYEuE/?p=%32", "[URL]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2[/URL]", "[url]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2#reply[/url]", + "[URL=https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2]点击播放[/URL]", + "参考 https://example.com/?p=9 视频 https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2 请播放", ])("preserves the Bilibili page query in %s", (input) => { expect(parseSongRef(input)).toEqual({ id: "BV1yxHQeYEuE?p=2", platform: "bilibili" }); }); diff --git a/src/bot/song-ref.ts b/src/bot/song-ref.ts index afdf6e1..ba1d1aa 100644 --- a/src/bot/song-ref.ts +++ b/src/bot/song-ref.ts @@ -69,9 +69,9 @@ export function parseSongRef(raw: string): SongRef | null { // a "BV..." token never collides with them). const bv = /BV[0-9A-Za-z]{8,12}/.exec(q); if (bv && (/^BV[0-9A-Za-z]{8,12}$/.test(q) || /bilibili\.com|b23\.tv/i.test(q))) { - // TeamSpeak wraps pasted links in [URL] tags. Keep the page query while - // ignoring fragments, which can contain unrelated player state. - const query = /\?(.*)$/.exec(stripUrlBBCode(q).trim().split("#", 1)[0])?.[1]; + // Extract the query attached to this BV id, including in [URL=...] tags. + const suffix = q.slice(bv.index + bv[0].length); + const query = /^\/?\?([^#\s[\]]*)/.exec(suffix)?.[1]; const page = new URLSearchParams(query).get("p"); return { id: page && /^\d+$/.test(page) ? `${bv[0]}?p=${page}` : bv[0], platform: "bilibili" }; }