-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
204 lines (183 loc) · 5.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* https://github.com/wikimedia/html-metadata
*
* This file wraps all exportable functions so that they
* can be used with Promises.
*/
'use strict';
/*
Import modules
*/
const cheerio = require( 'cheerio' );
const index = require( './lib/index.js' );
/**
* Default exported function that takes a url string or
* request library options dictionary and returns a
* Promise for all available metadata
*
* @param {Object} urlOrOpts url String or options dictionary
* @return {Object} Promise for metadata
*/
exports = module.exports = function ( urlOrOpts ) {
return new Promise( ( resolve, reject ) => {
let url, opts;
if ( urlOrOpts instanceof Object ) {
if ( urlOrOpts.uri ) {
url = urlOrOpts.uri;
}
opts = urlOrOpts;
} else if ( typeof urlOrOpts === String ) {
url = urlOrOpts;
}
if ( !url ) {
reject( 'No uri supplied in argument' );
} else {
resolve(
// eslint-disable-next-line n/no-unsupported-features/node-builtins
fetch( url, opts ).then(
( response ) => response.text().then(
( body ) => index.parseAll( cheerio.load( body ) )
)
)
);
}
} );
};
/**
* Exported function that takes html string and
* returns a Promise for all available metadata
*
* @param {string} html html String HTML of the page
* @return {Object} Promise for metadata
*/
exports.loadFromString = function ( html ) {
return index.parseAll( cheerio.load( html ) );
};
/**
* Returns Object containing all available datatypes, keyed
* using the same keys as in metadataFunctions.
*
* @param {Object} chtml html Cheerio object to parse
* @return {Object} Promise for metadata
*/
exports.parseAll = function ( chtml ) {
return index.parseAll( chtml );
};
/**
* Scrapes BE Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseBEPress = function ( chtml ) {
return index.parseBEPress( chtml );
};
/**
* Scrapes embedded COinS data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseCOinS = function ( chtml ) {
return index.parseCOinS( chtml );
};
/**
* Parses value of COinS title tag
*
* @param {string} title String corresponding to value of title tag in span element
* @return {Object} Promise for metadata
*/
exports.parseCOinSTitle = function ( title ) {
return index.parseCOinSTitle( title );
};
/**
* Scrapes Dublin Core data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseDublinCore = function ( chtml ) {
return index.parseDublinCore( chtml );
};
/**
* Scrapes EPrints data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseEprints = function ( chtml ) {
return index.parseEprints( chtml );
};
/**
* Scrapes general metadata terms given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseGeneral = function ( chtml ) {
return index.parseGeneral( chtml );
};
/**
* Scrapes Highwire Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseHighwirePress = function ( chtml ) {
return index.parseHighwirePress( chtml );
};
/**
* Retrieves JSON-LD for given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for JSON-LD
*/
exports.parseJsonLd = function ( chtml ) {
return index.parseJsonLd( chtml );
};
/**
* Scrapes OpenGraph data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseOpenGraph = function ( chtml ) {
return index.parseOpenGraph( chtml );
};
/**
* Scrapes schema.org microdata given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseSchemaOrgMicrodata = function ( chtml ) {
return index.parseSchemaOrgMicrodata( chtml );
};
/**
* Scrapes Twitter data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parseTwitter = function ( chtml ) {
return index.parseTwitter( chtml );
};
/**
* Scrapes PRISM data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} Promise for metadata
*/
exports.parsePrism = function ( chtml ) {
return index.parsePrism( chtml );
};
/**
* Global exportable list of scraping promises with string keys
*
* @type {Object}
*/
exports.metadataFunctions = index.metadataFunctions;
/*
Export the version
*/
exports.version = require( './package' ).version;