[feat] support memos v22+ (#505)

This commit is contained in:
钟意 2024-08-13 16:23:06 +08:00 committed by GitHub
parent 99ae3b1a48
commit 7321d7cd8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 92 additions and 70 deletions

View File

@ -1,73 +1,95 @@
utils.jq(() => { utils.jq(() => {
$(function () { const els = Array.from(document.getElementsByClassName('ds-memos'));
const els = document.getElementsByClassName('ds-memos');
for (var i = 0; i < els.length; i++) { els.forEach(el => {
const el = els[i];
const api = el.getAttribute('api'); const api = el.getAttribute('api');
if (api == null) { if (!api) return;
continue;
}
const default_avatar = el.getAttribute('avatar') || def.avatar; const default_avatar = el.getAttribute('avatar') || def.avatar;
const limit = el.getAttribute('limit'); const limit = el.getAttribute('limit');
const host = api.replace(/https:\/\/(.*?)\/(.*)/i, '$1'); const host = api.match(/https:\/\/(.*?)\/(.*)/i)[1];
// layout
utils.request(el, api, function(data) { utils.request(el, api, async data => {
var users = []; let memos = versionHandlers.identify(data);
const filter = el.getAttribute('user'); if (memos.version === "feature" )return;
if (filter && filter.length > 0) {
users = filter.split(","); const users = el.getAttribute('user')?.split(",") || [];
const hide = el.getAttribute('hide')?.split(",") || [];
await Promise.all(memos.data.slice(0, limit || memos.data.length).map(item =>
createMemoCell(item, memos, users, hide, default_avatar, host).then(cell => $(el).append(cell))
));
});
async function createMemoCell(item, memos, users, hide, default_avatar, host) {
const versionHandler = versionHandlers[memos.version] || versionHandlers["feature"];
return `<div class="timenode">
<div class="header">${!users.length && !hide.includes('user') ? await versionHandler.buildUser(item, memos, default_avatar) : ''}
<span>${versionHandler.buildDate(item).toLocaleString()}</span></div>
<div class="body">${marked.parse(item.content || '')}
<div class="tag-plugin image">${versionHandler.buildImages(item, host).join('')}</div>
</div></div>`;
} }
var hide = [];
const hideStr = el.getAttribute('hide'); // Memos版本管理
if (hideStr && hideStr.length > 0) { const versionHandlers = {
hide = hideStr.split(","); "22-": {
} buildUser: async (item, memos, default_avatar) =>
data.forEach((item, i) => { `<div class="user-info">${default_avatar ? `<img src="${default_avatar}">` : ''}<span>${item.creatorName}</span></div>`,
if (limit && i >= limit) { buildDate: item => new Date(item.createdTs * 1000),
return; buildImages: (item, host) => (item.resourceList || []).filter(res => res.type?.includes('image/')).map(res =>
} `<div class="image-bg"><img src="${res.externalLink || `https://${host}/o/r/${res.id}`}"></div>`
if (item.user && item.user.login && users.length > 0) { )
if (!users.includes(item.user.login)) { },
return; "22+": {
} buildUser: async (item, memos, default_avatar) => {
} const creatorId = item?.creator.split('/')[1];
let date = new Date(item.createdTs * 1000) let user = memos.users.find(user => user.id === parseInt(creatorId));
var cell = '<div class="timenode" index="' + i + '">'; if (!user) {
cell += '<div class="header">'; if (!memos.requests[creatorId]) {
if (!users.length && !hide.includes('user')) { memos.requests[creatorId] = fetch(`${memos.site}/api/v1/users/${creatorId}`)
cell += '<div class="user-info">'; .then(response => response.json())
if (default_avatar.length > 0) { .then(data => {
cell += `<img src="${default_avatar}">`; if (data.username) {
} user = data;
cell += '<span>' + item.creatorName + '</span>'; memos.users.push(data);
cell += '</div>';
}
cell += '<span>' + date.toLocaleString() + '</span>';
cell += '</div>';
cell += '<div class="body">';
cell += marked.parse(item.content || '');
var imgs = [];
for (let res of item.resourceList) {
if (res.type?.includes('image/')) {
imgs.push(res);
}
}
if (imgs.length > 0) {
cell += '<div class="tag-plugin image">';
for (let img of imgs) {
if (img.externalLink?.length > 0) {
cell += `<div class="image-bg"><img src="${img.externalLink}" data-fancybox="memos"></div>`;
} else { } else {
cell += `<div class="image-bg"><img src="https://${host}/o/r/${img.id}" data-fancybox="memos"></div>`; user = null;
} }
})
.finally(() => delete memos.requests[creatorId]);
} }
cell += '</div>'; await memos.requests[creatorId];
user = memos.users.find(user => user.id === parseInt(creatorId));
} }
cell += '</div>'; const name = user ? user.nickname || user.username : 'memos';
cell += '</div>'; const avatarUrl = user?.avatarUrl ? `${memos.site}${user.avatarUrl}` : default_avatar || '';
$(el).append(cell); return `<div class="user-info">${avatarUrl ? `<img src="${avatarUrl}">` : ''}<span>${name}</span></div>`;
}); },
}); buildDate: item => new Date(item.createTime),
buildImages: (item) => (item.resources || []).filter(res => res.type?.includes('image/')).map(res =>
`<div class="image-bg"><img src="${res.externalLink || `https://${host}/o/r/${res.id}`}"></div>`
)
},
"feature": {
buildUser: async () => "memos",
buildDate: () => new Date(),
buildImages: () => []
},
identify: (data) => {
let memos = { version: "feature", users: [], site: api.split('/api/v1')[0], requests: {}, data: [] }
if (Array.isArray(data)) {
memos.version = "22-";
memos.data = data;
} else if (data.memos) {
memos.version = "22+";
memos.data = data.memos;
} else {
memos.version = "feature";
console.log("当前Memos版本过高请到Stellar社区反馈");
} }
return memos
}
};
}); });
}); });