|
| 1 | +// assets/js/featured-papers.js |
| 2 | +// Renders featured papers on the homepage by matching arXiv IDs |
| 3 | +// against data/papers.json. Featured IDs are injected by Jekyll |
| 4 | +// via window.FEATURED_IDS (from _data/featured_papers.yml). |
| 5 | + |
| 6 | +(function () { |
| 7 | + 'use strict'; |
| 8 | + |
| 9 | + function extractUrl(v) { |
| 10 | + if (!v) return ''; |
| 11 | + if (typeof v === 'string') return v; |
| 12 | + if (Array.isArray(v)) { var f = v[0]; return f ? (f.link || f.url || f.text || '') : ''; } |
| 13 | + if (typeof v === 'object') return v.link || v.url || v.text || ''; |
| 14 | + return ''; |
| 15 | + } |
| 16 | + |
| 17 | + function cleanAuthors(raw) { |
| 18 | + if (!raw) return ''; |
| 19 | + return raw.split(/[\n;]+/) |
| 20 | + .map(function (a) { return a.replace(/\s*\d+[,\d]*\s*/g, ' ').replace(/[*^∗]/g, '').trim(); }) |
| 21 | + .filter(Boolean).join(', '); |
| 22 | + } |
| 23 | + |
| 24 | + function extractArxivId(v) { |
| 25 | + var url = extractUrl(v); |
| 26 | + var m = url.match(/(\d{4}\.\d{4,5})/); |
| 27 | + return m ? m[1] : ''; |
| 28 | + } |
| 29 | + |
| 30 | + function esc(s) { |
| 31 | + return String(s) |
| 32 | + .replace(/&/g, '&').replace(/</g, '<') |
| 33 | + .replace(/>/g, '>').replace(/"/g, '"'); |
| 34 | + } |
| 35 | + |
| 36 | + function normalise(raw) { |
| 37 | + var authors = raw['作者信息(每人一行,分号换行,数字表示单位信息,*表示Equal Contribution, ^表示通讯作者)'] || raw['作者'] || ''; |
| 38 | + return { |
| 39 | + title: raw['论文标题'] || '', |
| 40 | + authors: cleanAuthors(typeof authors === 'string' ? authors : extractUrl(authors)), |
| 41 | + venue: raw['期刊/会议'] || '', |
| 42 | + pdf: extractUrl(raw['arXiv主页']), |
| 43 | + code: extractUrl(raw['Github仓库链接']), |
| 44 | + project: extractUrl(raw['刊印链接']), |
| 45 | + thumbnail: raw['_thumbnail'] || '', |
| 46 | + arxivId: extractArxivId(raw['arXiv主页']) |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + function render(papers) { |
| 51 | + var container = document.getElementById('featured-papers-list'); |
| 52 | + if (!container) return; |
| 53 | + |
| 54 | + var ids = window.FEATURED_IDS || []; |
| 55 | + // Preserve order from FEATURED_IDS |
| 56 | + var map = {}; |
| 57 | + papers.forEach(function (p) { if (p.arxivId) map[p.arxivId] = p; }); |
| 58 | + var featured = ids.map(function (id) { return map[id]; }).filter(Boolean); |
| 59 | + |
| 60 | + if (!featured.length) { |
| 61 | + container.innerHTML = ''; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + var html = '<div class="featured-papers">'; |
| 66 | + featured.forEach(function (p) { |
| 67 | + html += '<div class="featured-paper">'; |
| 68 | + html += '<div class="featured-paper-meta"><span class="featured-paper-venue">' + esc(p.venue) + '</span></div>'; |
| 69 | + html += '<div class="featured-paper-title">'; |
| 70 | + if (p.pdf) { |
| 71 | + html += '<a href="' + esc(p.pdf) + '" target="_blank" rel="noopener">' + esc(p.title) + '</a>'; |
| 72 | + } else { |
| 73 | + html += esc(p.title); |
| 74 | + } |
| 75 | + html += '</div>'; |
| 76 | + if (p.authors) html += '<div class="featured-paper-authors">' + esc(p.authors) + '</div>'; |
| 77 | + html += '<div class="featured-paper-links">'; |
| 78 | + if (p.pdf) html += '<a href="' + esc(p.pdf) + '" target="_blank" rel="noopener" class="paper-link"><i class="fas fa-file-pdf"></i> PDF</a>'; |
| 79 | + if (p.code) html += '<a href="' + esc(p.code) + '" target="_blank" rel="noopener" class="paper-link"><i class="fab fa-github"></i> Code</a>'; |
| 80 | + if (p.project) html += '<a href="' + esc(p.project) + '" target="_blank" rel="noopener" class="paper-link"><i class="fas fa-newspaper"></i> Paper</a>'; |
| 81 | + html += '</div>'; |
| 82 | + html += '</div>'; |
| 83 | + }); |
| 84 | + html += '</div>'; |
| 85 | + html += '<div style="text-align:right;margin-top:12px;"><a href="/publications/" class="view-all-link">View all publications →</a></div>'; |
| 86 | + container.innerHTML = html; |
| 87 | + } |
| 88 | + |
| 89 | + document.addEventListener('DOMContentLoaded', function () { |
| 90 | + var container = document.getElementById('featured-papers-list'); |
| 91 | + if (!container) return; |
| 92 | + |
| 93 | + fetch('/data/papers.json') |
| 94 | + .then(function (res) { return res.json(); }) |
| 95 | + .then(function (data) { |
| 96 | + var papers = data.filter(function (p) { return p['论文标题']; }).map(normalise); |
| 97 | + render(papers); |
| 98 | + }) |
| 99 | + .catch(function () { container.innerHTML = ''; }); |
| 100 | + }); |
| 101 | +})(); |
0 commit comments