Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ You can also check out this nice [working implementation](https://github.com/scr
If you only need the metadata available when each article streams, you can
use `item.meta` as usual.

- `guidlink` - Set to `false` to override Feedparser's default behavior, which
is to use an RSS item's `guid` as the item `link` when the item has no `link`
and the `guid` starts with `http:` or `https:`.

- `feedurl` - The url (string) of the feed. FeedParser is very good at
resolving relative urls in feeds, including those embedded in HTML content
fields. But some feeds use relative urls without declaring the `xml:base`
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare namespace FeedParser {
strict?: boolean;
normalize?: boolean;
addmeta?: boolean;
guidlink?: boolean;
feedurl?: string;
resume_saxerror?: boolean;
MAX_BUFFER_LENGTH?: number;
Expand Down
3 changes: 2 additions & 1 deletion lib/feedparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function FeedParser (options) {
if (!('strict' in this.options)) this.options.strict = false;
if (!('normalize' in this.options)) this.options.normalize = true;
if (!('addmeta' in this.options)) this.options.addmeta = true;
if (!('guidlink' in this.options)) this.options.guidlink = true;
if (!('resume_saxerror' in this.options)) this.options.resume_saxerror = true;
// MAX_BUFFER_LENGTH is not part of the public API of sax, but we need to be
// able to handle nodes that are larger than the 64K default
Expand Down Expand Up @@ -1168,7 +1169,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options) {
if (item.categories.length) {
item.categories = _.uniq(item.categories);
}
if (!item.link) {
if (!item.link && (!options || options.guidlink)) {
if (item.guid && /^https?:/.test(item.guid)) {
item.link = item.guid;
}
Expand Down
60 changes: 60 additions & 0 deletions test/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,64 @@ describe('links', function () {
});
});

it('should infer item link from http guid by default (issue #293)', function (done) {
var feedparser = new FeedParser();
var feed = '<?xml version="1.0" encoding="UTF-8"?>' +
'<rss version="2.0">' +
'<channel>' +
'<title>Linkless feed</title>' +
'<link>http://example.com/</link>' +
'<description>Feed with linkless items</description>' +
'<item>' +
'<title>One</title>' +
'<guid>http://example.com/posts/one</guid>' +
'</item>' +
'</channel>' +
'</rss>';

feedparser
.once('readable', function () {
var item = this.read();
assert.equal(item.guid, 'http://example.com/posts/one');
assert.equal(item.link, 'http://example.com/posts/one');
done();
})
.on('error', function (err) {
assert.ifError(err);
done(err);
});

feedparser.end(feed);
});

it('should not infer item link from guid when guidlink is false (issue #293)', function (done) {
var feedparser = new FeedParser({ guidlink: false });
var feed = '<?xml version="1.0" encoding="UTF-8"?>' +
'<rss version="2.0">' +
'<channel>' +
'<title>Linkless feed</title>' +
'<link>http://example.com/</link>' +
'<description>Feed with linkless items</description>' +
'<item>' +
'<title>One</title>' +
'<guid>http://example.com/posts/one</guid>' +
'</item>' +
'</channel>' +
'</rss>';

feedparser
.once('readable', function () {
var item = this.read();
assert.equal(item.guid, 'http://example.com/posts/one');
assert.equal(item.link, null);
done();
})
.on('error', function (err) {
assert.ifError(err);
done(err);
});

feedparser.end(feed);
});

});
1 change: 1 addition & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fp2 = new FeedParser({
strict: false,
normalize: true,
addmeta: true,
guidlink: true,
feedurl: 'https://example.com/feed',
resume_saxerror: true,
MAX_BUFFER_LENGTH: 1024 * 1024,
Expand Down