Skip to content

Commit 0db08ee

Browse files
authored
Merge branch 'master' into master
2 parents 8064efb + 9676e46 commit 0db08ee

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[og]: <http://ogp.me/>
77
[twitter]: <https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup>
88

9-
Grabity looks through [Open Graph](http://ogp.me/) and [Twitter Cards](https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup) markup to get Information about a link. Its functions will return as much data as they can from the markup. If no [og] or [twitter] tags are found, the returned objects will be empty.
9+
Grabity looks through [Open Graph](http://ogp.me/) and [Twitter Cards](https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup) markup to get Information about a link. Its functions will return as much data as they can from the markup. If no [og] or [twitter] tags are found, it will default to the content of the <title> tag and meta description, and if either the title tag or meta description is missing, the returned property will be empty.
1010

1111
## Getting Started:
1212
```

lib/grabity.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ exports.grabIt = async (url) => {
1818
if(!url) throw new Error("Missing url!");
1919

2020
try{
21-
let {og, twitter, favicon} = await utils.grabInfo(url);
21+
let {og, twitter, favicon, defaults} = await utils.grabInfo(url);
2222
let props = ["title", "description", "image"];
2323
let res = {};
2424
let val;
2525

2626
for(let prop of props){
27-
val = og[prop] || twitter[prop];
27+
val = og[prop] || twitter[prop] || defaults[prop];
2828

2929
if(val) res[prop] = val;
3030
}

test/tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("grabity", () => {
2525
});
2626

2727
describe("#grab", () => {
28-
it("should return all 'twitter:' and 'og:' tags, as well as favicon", async () => {
28+
it("should return all 'twitter:' and 'og:' tags, favicon, default title tag and meta description", async () => {
2929
let it = await grabity.grab(URL);
3030
let keys = Object.keys(tags);
3131

test_setup/tags.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ exports["twitter:card"] = "summary";
1212
exports["twitter:site"] = "@flickr";
1313
exports["twitter:title"] = "Small Island Developing States Photo Submission";
1414
exports["twitter:description"] = "View the album on Flickr.";
15-
exports["twitter:image"] = "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg";
15+
exports["twitter:image"] = "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg";
16+
17+
exports["title"] = "YE OLDE TEST FILE";
18+
exports["description"] = "This is a meta description";

test_setup/test.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<meta name="twitter:description" content="View the album on Flickr." />
1616
<meta name="twitter:image" content="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />
1717

18+
<meta name="description" content="This is a meta description" />
19+
1820
<link rel="icon" href="https://assets-cdn.github.com/favicon.ico">
1921

2022
</head>

utils/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ virtualConsole.sendTo(console, {omitJSDOMErrors: true});
2424
exports.grabInfo = async (url) => {
2525
let og = {};
2626
let twitter = {};
27+
let defaults = {};
2728

2829
try{
2930
let dom = await JSDOM.fromURL(url, {virtualConsole});
3031
let doc = dom.window.document;
3132
let metaEls = doc.getElementsByTagName("meta");
3233
let linkEls = doc.getElementsByTagName("link");
34+
let titleTag = doc.getElementsByTagName("title");
35+
let metaDescTag = doc.querySelector("meta[name='description']");
36+
37+
if(titleTag.length){
38+
defaults.title = titleTag[0].textContent;
39+
}
40+
41+
if(metaDescTag){
42+
defaults.description = metaDescTag.content;
43+
}
3344

3445
for(let meta of metaEls){
3546
filterInfo(meta, OG_PROP, og);
@@ -38,7 +49,7 @@ exports.grabInfo = async (url) => {
3849

3950
let favicon = findFavicon(linkEls);
4051

41-
return {og, twitter, favicon};
52+
return {og, twitter, favicon, defaults};
4253
}
4354
catch(err){
4455
throw err;
@@ -61,12 +72,22 @@ exports.grabAll = async (url) => {
6172
let doc = dom.window.document;
6273
let metaEls = doc.getElementsByTagName("meta");
6374
let linkEls = doc.getElementsByTagName("link");
75+
let titleTag = doc.getElementsByTagName("title");
76+
let metaDescTag = doc.querySelector("meta[name='description']");
77+
78+
if(titleTag.length){
79+
res.title = titleTag[0].textContent;
80+
}
81+
82+
if(metaDescTag){
83+
res.description = metaDescTag.content;
84+
}
6485

6586
for(let meta of metaEls){
6687
filterAll(meta, "og:", res);
6788
filterAll(meta, "twitter:", res);
6889
}
69-
90+
7091
res.favicon = findFavicon(linkEls);
7192

7293
return res;

0 commit comments

Comments
 (0)