Skip to content

Commit 945bf3e

Browse files
committed
Add guid link inference option
1 parent 8fdfb59 commit 945bf3e

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ You can also check out this nice [working implementation](https://github.com/scr
121121
If you only need the metadata available when each article streams, you can
122122
use `item.meta` as usual.
123123

124+
- `guidlink` - Set to `false` to override Feedparser's default behavior, which
125+
is to use an RSS item's `guid` as the item `link` when the item has no `link`
126+
and the `guid` starts with `http:` or `https:`.
127+
124128
- `feedurl` - The url (string) of the feed. FeedParser is very good at
125129
resolving relative urls in feeds, including those embedded in HTML content
126130
fields. But some feeds use relative urls without declaring the `xml:base`

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ declare namespace FeedParser {
3636
strict?: boolean;
3737
normalize?: boolean;
3838
addmeta?: boolean;
39+
guidlink?: boolean;
3940
feedurl?: string;
4041
resume_saxerror?: boolean;
4142
MAX_BUFFER_LENGTH?: number;

lib/feedparser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function FeedParser (options) {
7777
if (!('strict' in this.options)) this.options.strict = false;
7878
if (!('normalize' in this.options)) this.options.normalize = true;
7979
if (!('addmeta' in this.options)) this.options.addmeta = true;
80+
if (!('guidlink' in this.options)) this.options.guidlink = true;
8081
if (!('resume_saxerror' in this.options)) this.options.resume_saxerror = true;
8182
// MAX_BUFFER_LENGTH is not part of the public API of sax, but we need to be
8283
// able to handle nodes that are larger than the 64K default
@@ -1168,7 +1169,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options) {
11681169
if (item.categories.length) {
11691170
item.categories = _.uniq(item.categories);
11701171
}
1171-
if (!item.link) {
1172+
if (!item.link && (!options || options.guidlink)) {
11721173
if (item.guid && /^https?:/.test(item.guid)) {
11731174
item.link = item.guid;
11741175
}

test/link.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,64 @@ describe('links', function () {
4747
});
4848
});
4949

50+
it('should infer item link from http guid by default (issue #293)', function (done) {
51+
var feedparser = new FeedParser();
52+
var feed = '<?xml version="1.0" encoding="UTF-8"?>' +
53+
'<rss version="2.0">' +
54+
'<channel>' +
55+
'<title>Linkless feed</title>' +
56+
'<link>http://example.com/</link>' +
57+
'<description>Feed with linkless items</description>' +
58+
'<item>' +
59+
'<title>One</title>' +
60+
'<guid>http://example.com/posts/one</guid>' +
61+
'</item>' +
62+
'</channel>' +
63+
'</rss>';
64+
65+
feedparser
66+
.once('readable', function () {
67+
var item = this.read();
68+
assert.equal(item.guid, 'http://example.com/posts/one');
69+
assert.equal(item.link, 'http://example.com/posts/one');
70+
done();
71+
})
72+
.on('error', function (err) {
73+
assert.ifError(err);
74+
done(err);
75+
});
76+
77+
feedparser.end(feed);
78+
});
79+
80+
it('should not infer item link from guid when guidlink is false (issue #293)', function (done) {
81+
var feedparser = new FeedParser({ guidlink: false });
82+
var feed = '<?xml version="1.0" encoding="UTF-8"?>' +
83+
'<rss version="2.0">' +
84+
'<channel>' +
85+
'<title>Linkless feed</title>' +
86+
'<link>http://example.com/</link>' +
87+
'<description>Feed with linkless items</description>' +
88+
'<item>' +
89+
'<title>One</title>' +
90+
'<guid>http://example.com/posts/one</guid>' +
91+
'</item>' +
92+
'</channel>' +
93+
'</rss>';
94+
95+
feedparser
96+
.once('readable', function () {
97+
var item = this.read();
98+
assert.equal(item.guid, 'http://example.com/posts/one');
99+
assert.equal(item.link, null);
100+
done();
101+
})
102+
.on('error', function (err) {
103+
assert.ifError(err);
104+
done(err);
105+
});
106+
107+
feedparser.end(feed);
108+
});
109+
50110
});

test/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const fp2 = new FeedParser({
88
strict: false,
99
normalize: true,
1010
addmeta: true,
11+
guidlink: true,
1112
feedurl: 'https://example.com/feed',
1213
resume_saxerror: true,
1314
MAX_BUFFER_LENGTH: 1024 * 1024,

0 commit comments

Comments
 (0)