fix: 支持b23路径直解析视频ID

This commit is contained in:
sunlei 2026-06-19 17:10:10 +08:00
parent 2d0d8dc4cd
commit 69cf6d99c4
2 changed files with 33 additions and 5 deletions

View File

@ -59,12 +59,19 @@ export function parseBilibiliVideoReference(
const url = new URL(cleaned);
const pathSegments = url.pathname.split('/').filter(Boolean);
const videoSegmentIndex = pathSegments.findIndex(
(segment) => segment.toLowerCase() === 'video',
);
if (videoSegmentIndex < 0) return null;
const hostname = url.hostname.toLowerCase();
let videoIdSegment: string | undefined;
if (hostname === B23_HOST) {
videoIdSegment = pathSegments[0];
} else {
const videoSegmentIndex = pathSegments.findIndex(
(segment) => segment.toLowerCase() === 'video',
);
if (videoSegmentIndex < 0) return null;
videoIdSegment = pathSegments[videoSegmentIndex + 1];
}
const videoIdSegment = pathSegments[videoSegmentIndex + 1];
if (videoIdSegment && BVID_PATTERN.test(videoIdSegment)) {
return {
canonicalVideoId: videoIdSegment,

View File

@ -29,6 +29,22 @@ describe('Bilibili URL parser', () => {
});
});
it('parses b23.tv paths that directly embed BV or av ids', () => {
expect(parseBilibiliVideoReference('https://b23.tv/BV1xx411c7mD')).toEqual({
canonicalVideoId: 'BV1xx411c7mD',
kind: 'bvid',
sourceUrl: 'https://b23.tv/BV1xx411c7mD',
value: 'BV1xx411c7mD',
});
expect(parseBilibiliVideoReference('https://b23.tv/av170001')).toMatchObject(
{
canonicalVideoId: 'av170001',
kind: 'aid',
value: '170001',
},
);
});
it('allows only Bilibili and b23.tv hosts', () => {
expect(isAllowedBilibiliUrl('https://b23.tv/abc123')).toBe(true);
expect(isAllowedBilibiliUrl('https://space.bilibili.com/1')).toBe(true);
@ -56,6 +72,11 @@ describe('Bilibili URL parser', () => {
'https://www.bilibili.com/search?keyword=BV1xx411c7mD',
),
).toBeNull();
expect(
parseBilibiliVideoReference(
'https://space.bilibili.com/1#/video/BV1xx411c7mD',
),
).toBeNull();
});
it('rejects malformed BV path segments with extra characters', () => {