Files
xianyu-hunter-hermes-skill/project/xianyu-hunter/lib/links.mjs
T

63 lines
2.2 KiB
JavaScript

const GOofishDesktopBase = 'https://www.goofish.com/item';
const GOofishMobileBase = 'https://h5.m.goofish.com/item';
export function normalizeItemId(value = '') {
const text = String(value || '').trim();
if (!text) return '';
const queryMatch = text.match(/[?&](?:id|itemId)=([0-9]+)/i);
if (queryMatch) return queryMatch[1];
const pathMatch = text.match(/(?:item|itemId)[=/]([0-9]+)/i);
if (pathMatch) return pathMatch[1];
const digits = text.match(/\b([0-9]{8,})\b/);
return digits ? digits[1] : '';
}
export function buildItemLinks(itemOrId = {}) {
const raw = typeof itemOrId === 'object' && itemOrId !== null ? itemOrId : { id: itemOrId };
const id = normalizeItemId(raw.id || raw.itemId || raw.href || raw.url || raw.detailUrl || raw.originalHref || raw.shareUrl);
const source = String(raw.href || raw.url || raw.detailUrl || raw.originalHref || raw.shareUrl || '').trim();
let categoryId = String(raw.categoryId || '').trim();
if (!categoryId && source) {
const m = source.match(/[?&]categoryId=([^&#]+)/i);
if (m) categoryId = decodeURIComponent(m[1]);
}
if (!id) {
return {
id: '',
desktopUrl: source,
mobileUrl: source,
webUrl: source,
primaryUrl: source,
markdownUrl: source,
};
}
const categoryQuery = categoryId ? `&categoryId=${encodeURIComponent(categoryId)}` : '';
const desktopUrl = `${GOofishDesktopBase}?id=${encodeURIComponent(id)}${categoryQuery}`;
const mobileUrl = `${GOofishMobileBase}?id=${encodeURIComponent(id)}`;
return {
id,
desktopUrl,
mobileUrl,
webUrl: desktopUrl,
primaryUrl: mobileUrl,
markdownUrl: mobileUrl,
};
}
export function enrichItemLinks(item = {}) {
const links = buildItemLinks(item);
if (!links.id && !links.primaryUrl) return item;
return {
...item,
id: item.id || links.id,
href: links.primaryUrl || item.href || item.url || '',
mobileHref: links.mobileUrl || item.mobileHref || '',
webHref: links.desktopUrl || item.webHref || '',
originalHref: item.originalHref || item.href || item.url || '',
};
}
export function displayItemUrl(itemOrId = {}) {
return buildItemLinks(itemOrId).primaryUrl || '';
}