Guest User

Feed View Updated

a guest
Mar 1st, 2024
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.58 KB | None | 0 0
  1. <style>
  2. .feed-container a.card
  3. {
  4.     color: var(--text-normal) !important;
  5.     text-decoration: none !important;
  6.     border-radius: var(--radius-l);
  7.     border: 1px solid var(--divider-color);
  8.     margin: 0 !important;
  9.     transition: border 0.1s ease-in-out, background-color 0.2s ease-in-out;
  10.     padding: 1em !important;
  11.     display: flow;
  12. }
  13. .feed-container a.card:hover
  14. {
  15.     border: 1px solid var(--divider-color-hover);
  16.     background-color: hsla(var(--accent-h), var(--accent-s), calc(var(--accent-l) * 1.5), 0.1);
  17. }
  18. .feed-container a.card a:hover
  19. {
  20.     text-decoration: underline;
  21. }
  22. .feed-container a.card a.tag
  23. {
  24.     margin-block: 0.5em;
  25.     display: block;
  26.     width: fit-content;
  27. }
  28. .feed-container a.card > h3:first-child
  29. {
  30.     margin: 0 !important;
  31.     margin-bottom: 0.5em !important;
  32. }
  33. .feed-container a.card p
  34. {
  35.     margin: 0 !important;
  36.     line-height: 1.5em;
  37.     overflow: hidden;
  38.     display: -webkit-box;
  39.     -webkit-line-clamp: 3;
  40.     -webkit-box-orient: vertical;
  41. }
  42. .feed-container a.card .feed-time
  43. {
  44.     display: inline-block;
  45.     margin-top: 0.6em;
  46.     font-size: var(--font-smallest);
  47.     color: var(--text-muted);
  48. }
  49. .feed-container
  50. {
  51.     display: grid;
  52.     grid-template-columns: repeat(auto-fill, minmax(14em, 1fr));
  53.     gap: 0.5em;
  54. }
  55. </style>
  56.  
  57. <script>
  58.     function getHumanReadableTimeDiff(oldTime, newTime)
  59.     {
  60.         const diff = oldTime - newTime;
  61.         const formatter = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
  62.         let time = formatter.format(Math.round(diff / 86400000), 'day');
  63.         if (time == "today")
  64.             time = formatter.format(Math.round(diff / 3600000), 'hour');
  65.         if (time == "this hour")
  66.             time = formatter.format(Math.round(diff / 600000), 'minute');
  67.         if (time == "this minute")
  68.             time = "now";
  69.         return time;
  70.     }
  71.     async function createFeed()
  72.     {
  73.         if (window.location.pathname != "/" && window.location.pathname != "/index.html")
  74.            return;
  75.         let rssResp = await fetch("/lib/rss.xml");
  76.         if (!rssResp.ok) return;
  77.         let rss = await rssResp.text();
  78.         let rssDoc = await new DOMParser().parseFromString(rss, "text/xml");
  79.         let items = Array.from(rssDoc.querySelectorAll("item"));
  80.         function getItem(itemEl)
  81.         {
  82.             let regex = /<!-?-?\[CDATA\[([\w\W]+?)]]>/;
  83.             let title = itemEl.querySelector("title")?.textContent;
  84.             let description = itemEl.querySelector("description")?.textContent;
  85.             let link = itemEl.querySelector("link")?.textContent;
  86.             let date = itemEl.querySelector("pubDate")?.textContent;
  87.             title = title?.match(regex)?.[1] ?? title?.match(regex)?.[0] ?? title;
  88.             description = description?.match(regex)?.[1] ?? description?.match(regex)?.[0] ?? description;
  89.             link = link?.match(regex)?.[1] ?? link?.match(regex)?.[0] ?? link;
  90.             date = new Date(date);
  91.             return {title: title, description: description, link: link, date: date}
  92.         }
  93.         let rssItems = items.map((i) => getItem(i));
  94.         let container = document.createElement("div");
  95.         container.classList.add("feed-container");
  96.         document.querySelector(".markdown-preview-sizer").append(container);
  97.         for (let item of rssItems)
  98.         {
  99.             let card = document.createElement("a");
  100.             container.append(card);
  101.             card.href = item.link;
  102.             card.classList.add("card", "internal-link");
  103.             let title = document.createElement("h3");
  104.             card.append(title);
  105.             title.innerHTML = item.title;
  106.             let description = document.createElement("p");
  107.             card.append(description);
  108.             description.innerHTML = item.description;
  109.             let timeEl = document.createElement("span");
  110.             timeEl.classList.add("feed-time");
  111.             card.append(timeEl);
  112.             timeEl.innerHTML = getHumanReadableTimeDiff(item.date, Date.now());
  113.         }
  114.         container.querySelectorAll("a.tag").forEach((t) => {t.style = ""; t.parentElement.before(t)});
  115.         container.querySelectorAll("img").forEach((el) => el.remove());
  116.         container.querySelectorAll("[src]").forEach(el => el.src = el.src.replace("https://docs.obsidianweb.net", ""));
  117.         container.querySelectorAll("[href]").forEach(el => el.href = el.href.replace("https://docs.obsidianweb.net", ""));
  118.         container.querySelectorAll("br:first-child").forEach(el => el.remove());
  119.         setupLinks(container);
  120.     }
  121.     onPageLoad(createFeed);
  122. </script>
Advertisement
Add Comment
Please, Sign In to add comment