Skip to content

Commit

Permalink
~ adding support for more rss feed
Browse files Browse the repository at this point in the history
  • Loading branch information
joethei committed Jan 13, 2022
1 parent 550a570 commit cbe05fa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "rss-reader",
"name": "RSS Reader",
"version": "1.0.1",
"version": "1.0.2",
"minAppVersion": "0.12.17",
"description": "Read RSS Feeds from within obsidian",
"author": "Johannes Theiner",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rss-reader",
"version": "1.0.1",
"version": "1.0.2",
"description": "Read RSS Feeds from inside obsidian",
"main": "main.js",
"scripts": {
Expand Down Expand Up @@ -46,7 +46,6 @@
"svelte-preprocess": "^4.9.8",
"ts-md5": "^1.2.10",
"ts-node": "^10.4.0",
"tslib": "^2.2.0",
"tslint": "^6.1.3",
"typescript": "^4.2.4"
}
Expand Down
60 changes: 38 additions & 22 deletions src/parser/rssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {Md5} from "ts-md5";

/**
* parser for .rss files, build from scratch
* because I could not find a parser that works on mobile and is up to date.
* because I could not find a parser that
* - works on mobile
* - is up-to-date
* - works for multiple different interpretations of the rss spec
*/

export interface RssFeedContent {
Expand Down Expand Up @@ -44,58 +47,72 @@ export interface RssFeedItem {
}

/**
* return the node with the specified name
* : to get namespaced element
* . to get nested element
* @param element
* @param name
*/
function getElementByName(element: Element | Document, name: string): ChildNode {
let value: ChildNode;
if (typeof element.getElementsByTagName !== 'function') {
if (typeof element.getElementsByTagName !== 'function' && typeof element.getElementsByTagNameNS !== 'function') {
//the required methods do not exist on element, aborting
return;
}

if (name.contains(":")) {
const [namespace, tag] = name.split(":");
const namespaceUri = element.lookupNamespaceURI(namespace);
if (element.getElementsByTagNameNS(namespaceUri, tag).length > 0) {
value = element.getElementsByTagNameNS(namespaceUri, tag)[0].childNodes[0];
const byNamespace = element.getElementsByTagNameNS(namespaceUri, tag);
if (byNamespace.length > 0) {
value = byNamespace[0].childNodes[0];
} else {
//there is no element in that namespace, probably because no namespace has been defined
const tmp = element.getElementsByTagName(name);
if (tmp.length > 0) {
if (tmp[0].childNodes.length === 0) {
value = tmp[0];
} else {
const node = tmp[0].childNodes[0];
if (node !== undefined) {
value = node;
}
}
}
}

} else if (name.contains(".")) {
const [prefix, tag] = name.split(".");
if (element.getElementsByTagName(prefix).length > 0) {
const nodes = Array.from(element.getElementsByTagName(prefix)[0].childNodes);

nodes.forEach((node) => {
if (node.nodeName == tag) {
value = node;
}
});
}

} else {
if (element.getElementsByTagName(name).length > 0) {
if (element.getElementsByTagName(name)[0].childNodes.length == 0) {
value = element.getElementsByTagName(name)[0];
} else {
const node = element.getElementsByTagName(name)[0].childNodes[0];
if (node !== undefined)
value = node;
}
} else if (element.getElementsByTagName(name).length > 0) {
if (element.getElementsByTagName(name)[0].childNodes.length == 0) {
value = element.getElementsByTagName(name)[0];
} else {
const node = element.getElementsByTagName(name)[0].childNodes[0];
if (node !== undefined)
value = node;
}
}
return value;
}

/**
* # to get attribute
* Always returns the last found value for names
* @param element
* @param names possible names
*/
function getContent(element: Element | Document, names: string[]): string {
let value: string;
names.forEach((name) => {
for (let name of names) {
if (name.contains("#")) {
const [elementName, attr] = name.split("#");
const data = getElementByName(element, elementName);
Expand All @@ -108,21 +125,21 @@ function getContent(element: Element | Document, names: string[]): string {
}
}
}
}else {
} else {
const data = getElementByName(element, name);
if (data) {
//@ts-ignore
if (data.nodeValue && data.nodeValue.length > 0) {
value = data.nodeValue;
}
//@ts-ignore
if (data.innerHTML && data.innerHTML.length > 0) {
else if (data.innerHTML && data.innerHTML.length > 0) {
//@ts-ignore
value = data.innerHTML;
}
}
}
});
}
if (value === undefined) {
return "";
}
Expand All @@ -133,7 +150,7 @@ function buildItem(element: Element): RssFeedItem {
return {
title: getContent(element, ["title"]),
description: getContent(element, ["content", "content:encoded", "itunes:summary", "description", "summary", "media:description"]),
content: getContent(element, ["itunes:summary", "description", "summary", "media:description", "content", "content:encoded"]),
content: getContent(element, ["itunes:summary", "description", "summary", "media:description", "content", "content:encoded", "ns0:encoded"]),
category: getContent(element, ["category"]),
link: getContent(element, ["link", "link#href"]),
creator: getContent(element, ["creator", "dc:creator", "author", "author.name"]),
Expand Down Expand Up @@ -178,7 +195,7 @@ export async function getFeedItems(feed: RssFeed): Promise<RssFeedContent> {
try {
const rawData = await request({url: feed.url});
data = new window.DOMParser().parseFromString(rawData, "text/xml");
}catch (e) {
} catch (e) {
console.error(e);
return Promise.resolve(undefined);
}
Expand All @@ -200,14 +217,13 @@ export async function getFeedItems(feed: RssFeed): Promise<RssFeedContent> {
item.language = language;
item.hash = <string>new Md5().appendStr(item.title).appendStr(item.folder).appendStr(item.link).end();

if(!item.image && feed.url.contains("youtube.com/feeds")) {
if (!item.image && feed.url.contains("youtube.com/feeds")) {
item.image = "https://i3.ytimg.com/vi/" + item.id.split(":")[2] + "/hqdefault.jpg";
}

items.push(item);
}
})

const image = getContent(data, ["image", "image.url", "icon"]);

const content: RssFeedContent = {
Expand Down
5 changes: 2 additions & 3 deletions src/view/ItemView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@
{/if}
</span>


<div class="rss-card-items">
<div class="rss-item">
<div class="rss-item-image">
{#if item.image && !item.image.includes(".mp3")}
<img src={item.image} width="250em" alt="Article">
{/if}
</div>
<div class="rss-item">
<div class="rss-item-text">
{#if item.description}
<MarkdownContent content={item.description}/>
{/if}
Expand Down
4 changes: 3 additions & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ input.is-invalid {
padding-top: 10px;
}

.rss-card-items .rss-item {
.rss-card-items .rss-item-text {
padding-left: 5px;
text-overflow: ellipsis;
overflow: hidden;
max-height: 10em;
}

.rss-item-title {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"0.9.2": "0.13.14",
"0.9.3": "0.12.17",
"1.0.0": "0.12.17",
"1.0.1": "0.12.17"
"1.0.1": "0.12.17",
"1.0.2": "0.12.17"
}

0 comments on commit cbe05fa

Please sign in to comment.