Skip to content

Commit 9038fcc

Browse files
committed
Cleanup & docs
1 parent 24408b5 commit 9038fcc

File tree

9 files changed

+61
-31
lines changed

9 files changed

+61
-31
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { resolveSequence, resolveParallel } from './src/util/promises.js';
3636
import addExif from './src/exif.js';
3737

3838
import { isURL } from './src/util/url.js';
39-
import { isFeed, processFeed } from './src/util/feeds.js';
39+
import { isFeed, processFeed } from './src/feeds.js';
4040

4141
import get_style_attribute_value from './src/get-style-attribute-value.js';
4242

src/cleanup-item.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import createDOMPurify from 'dompurify';
55

66
import { hyphenateDom } from './hyphenate.js';
77
import { textToIso6391, getLanguageAttribute } from './util/language.js';
8-
import { getUrlOrigin } from './util/url-origin.js';
8+
import { getURLOrigin } from './util/url.js';
99
import { setIdsAndReturnHeadings, nestHeadings } from './headings.js';
1010

1111
import {
@@ -120,7 +120,7 @@ export default async function cleanupItem(
120120
stripping the URL down to its origin,
121121
but just in case, let’s strip it ourselves.
122122
*/
123-
referrer: getUrlOrigin(url),
123+
referrer: getURLOrigin(url),
124124
referrerPolicy: 'strict-origin-when-cross-origin',
125125
timeout: 10 * 1000
126126
},

src/util/feeds.js renamed to src/feeds.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import { isURL } from './url.js';
1+
import { isURL } from './util/url.js';
22

3+
/*
4+
Specification:
5+
6+
RFC 4287: The Atom Syndication Format
7+
https://datatracker.ietf.org/doc/html/rfc4287
8+
*/
39
function isAtomFeed(doc) {
410
return (
511
doc.documentElement.localName === 'feed' &&
612
doc.documentElement.namespaceURI === 'http://www.w3.org/2005/Atom'
713
);
814
}
915

16+
/*
17+
Specification:
18+
19+
RSS 2.0 Specification
20+
https://www.rssboard.org/rss-specification
21+
*/
1022
function isRssFeed(doc) {
1123
return doc.documentElement.localName === 'rss';
1224
}

src/remote-resources.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
extForMimetype,
66
isImageURL
77
} from './util/file-mimetype.js';
8-
import { getUrlOrigin } from './util/url-origin.js';
8+
import { getURLOrigin } from './util/url.js';
99

1010
export default function remoteResources(doc) {
1111
let srcs = new Map();
@@ -31,7 +31,7 @@ export default function remoteResources(doc) {
3131
srcs.set(src, {
3232
original: src,
3333
mapped: `rr-${uuid()}${ext}`,
34-
origin: getUrlOrigin(doc.baseURI),
34+
origin: getURLOrigin(doc.baseURI),
3535
mimetype: mime
3636
});
3737
}

src/util/url-origin.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/util/url.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Polyfill for URL.canParse(), which was added in later Node.js versions
12
export function isURL(ref) {
23
try {
34
new URL(ref);
@@ -7,3 +8,13 @@ export function isURL(ref) {
78
}
89
return false;
910
}
11+
12+
export function getURLOrigin(str) {
13+
let origin;
14+
try {
15+
origin = new URL(str).origin;
16+
} catch (err) {
17+
// ignore
18+
}
19+
return origin && origin !== 'null' ? origin : undefined;
20+
}

src/util/wrap-html-fragment.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/*
2+
A minimal HTML shell for a DOM fragment,
3+
such as the content of a XML feed entry,
4+
to pass to the JSDOM constructor.
5+
*/
16
export default function wrapHTMLFragment(item) {
2-
return `<!doctype html>
3-
<html>
4-
<head>
5-
<title>${item.title}</title>
6-
</head>
7-
<body><article>${item.content}</article></body>
8-
</html>`;
7+
return `
8+
<!doctype html>
9+
<html>
10+
<head>
11+
<title>${item.title || ''}</title>
12+
</head>
13+
<body>
14+
<article>${item.content || ''}</article>
15+
</body>
16+
</html>
17+
`;
918
}

test/url-origin.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/url.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import tape from 'tape';
2+
import { getURLOrigin, isURL } from '../src/util/url.js';
3+
4+
tape('getURLOrigin', t => {
5+
t.equal(getURLOrigin('invalid'), undefined);
6+
t.equal(getURLOrigin('file:///Users/myuser/'), undefined);
7+
t.equal(getURLOrigin('https://github.com/user/repo'), 'https://github.com');
8+
t.end();
9+
});
10+
11+
tape('isURL', t => {
12+
t.equal(isURL('invalid'), false);
13+
t.equal(isURL('file:///Users/myuser/'), true);
14+
t.equal(isURL('https://github.com/user/repo'), true);
15+
t.end();
16+
});

0 commit comments

Comments
 (0)