Files
tougao_web/src/utils/referenceLinks.js
2026-06-17 09:35:15 +08:00

117 lines
3.4 KiB
JavaScript

function stripHtml(text) {
return String(text || '')
.replace(/<[^>]+>/g, '')
.trim();
}
/** 去掉末尾 [doi] 等杂质,返回纯 DOI */
function cleanDoiString(raw) {
let text = String(raw || '').trim();
if (!text) {
return null;
}
text = text.replace(/\[doi\]$/i, '').trim();
text = text.replace(/[.,;)\]]+$/g, '').trim();
return text || null;
}
/** 从文献行数据中解析 DOI */
export function resolveReferenceDoi(row) {
const candidates = [row && row.doilink, row && row.refer_doi, row && row.doi].filter(Boolean);
for (let i = 0; i < candidates.length; i += 1) {
const text = String(candidates[i]).trim();
if (!text) {
continue;
}
const match = text.match(/10\.\d{4,9}\/[^\s\[]+/i);
if (match) {
return cleanDoiString(match[0]);
}
if (/^10\.\d/i.test(text)) {
return cleanDoiString(text);
}
}
return null;
}
/** 从 book 类型文献中解析 ISBN */
export function resolveReferenceIsbn(row) {
const raw = String((row && row.isbn) || '').trim();
if (!raw) {
return null;
}
const isbn13 = raw.match(/97[89][-\s.]?(?:\d[-\s.]?){9}[\dXx]/i);
if (isbn13) {
return isbn13[0].replace(/\s+/g, '-');
}
const isbn10 = raw.match(/(?<!\d)(?:\d[-\s.]?){9}[\dXx](?!\d)/i);
if (isbn10) {
return isbn10[0].replace(/\s+/g, '-');
}
const compact = raw.replace(/[^\dXx]/gi, '');
if (compact.length === 10 || compact.length === 13) {
return raw.replace(/\s+/g, ' ').trim();
}
return null;
}
export function resolveReferenceTitle(row) {
if (!row) {
return '';
}
return stripHtml(row.title || row.refer_frag || '');
}
/**
* 生成 journal/other 文献跳转链接
* @param {string|null|undefined} doi - 文献 DOI
* @param {string} title - 文献标题(无 DOI 时用于搜索)
*/
export function getReferenceLinks(doi, title) {
const normalizedDoi = cleanDoiString(doi);
const normalizedTitle = stripHtml(title);
const encodedTitle = encodeURIComponent(normalizedTitle);
const pubmedLink = normalizedDoi
? `https://pubmed.ncbi.nlm.nih.gov/?term=${encodeURIComponent(normalizedDoi)}`
: null;
const scholarLink = normalizedDoi
? `https://scholar.google.com/scholar?q=${encodeURIComponent(normalizedDoi)}`
: `https://scholar.google.com/scholar?q=${encodedTitle}`;
const doiLink = normalizedDoi ? `https://doi.org/${encodeURIComponent(normalizedDoi)}` : null;
return {
pubmedLink,
scholarLink,
doiLink,
googleLink: null,
isBook: false
};
}
/** book 类型:用 ISBN 在 Google 检索 */
export function getBookReferenceLinks(row) {
const isbn = resolveReferenceIsbn(row);
const title = resolveReferenceTitle(row);
const searchQuery = isbn || title;
const encodedQuery = encodeURIComponent(searchQuery);
return {
pubmedLink: null,
scholarLink: null,
doiLink: null,
googleLink: searchQuery ? `https://www.google.com/search?q=${encodedQuery}` : null,
isBook: true,
searchQuery
};
}
export function getReferenceLinksFromRow(row) {
if (row && row.refer_type === 'book') {
return getBookReferenceLinks(row);
}
return getReferenceLinks(resolveReferenceDoi(row), resolveReferenceTitle(row));
}