forked from microlinkhq/metascraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthor.js
More file actions
66 lines (54 loc) · 1.67 KB
/
Copy pathauthor.js
File metadata and controls
66 lines (54 loc) · 1.67 KB
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
const isUrl = require('is-url')
const toTitle = require('to-title-case')
/**
* Wrap a rule with validation and formatting logic.
*
* @param {Function} rule
* @return {Function} wrapped
*/
function wrap(rule) {
return ($) => {
let value = rule($)
if (typeof value != 'string') return
if (isUrl(value)) return
if (value.includes('|')) return
// remove any extra "by" in the start of the string
value = value.replace(/^[\s\n]*by[\s\n]*/im, '')
// remove any extra lines of text
value = value.replace(/\n.*/m, '')
// remove extra spaces that sometimes creep in
value = value.replace(/\s+/g, ' ')
// make it title case, since some sites have it in weird casing
value = toTitle(value)
return value
}
}
/**
* Enforce stricter matching for a `rule`.
*
* @param {Function} rule
* @return {Function} stricter
*/
function strict(rule) {
return ($) => {
let value = rule($)
const regexp = /^\w+\s+\w+/
if (!regexp.test(value)) return
return value
}
}
/**
* Rules.
*/
module.exports = [
wrap(($) => $('meta[property="article:author"]').attr('content')),
wrap(($) => $('meta[name="author"]').attr('content')),
wrap(($) => $('meta[name="sailthru.author"]').attr('content')),
wrap(($) => $('[rel="author"]').first().text()),
wrap(($) => $('[itemprop*="author"] [itemprop="name"]').first().text()),
wrap(($) => $('[itemprop*="author"]').first().text()),
wrap(($) => $('meta[property="book:author"]').attr('content')),
strict(wrap(($) => $('a[class*="author"]').first().text())),
strict(wrap(($) => $('[class*="author"] a').first().text())),
strict(wrap(($) => $('[class*="author"]').first().text())),
]