This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = { | ||
description: [ | ||
['meta[property="og:description"]', node => node.element.content], | ||
['meta[name="description"]', node => node.element.content], | ||
], | ||
icon_url: [ | ||
['link[rel="apple-touch-icon"]', node => node.element.href], | ||
['link[rel="apple-touch-icon-precomposed"]', node => node.element.href], | ||
['link[rel="icon"]', node => node.element.href], | ||
['link[rel="fluid-icon"]', node => node.element.href], | ||
['link[rel="shortcut icon"]', node => node.element.href], | ||
['link[rel="Shortcut Icon"]', node => node.element.href], | ||
['link[rel="mask-icon"]', node => node.element.href], | ||
], | ||
image_url: [ | ||
['meta[property="og:image"]', node => node.element.content], | ||
['meta[property="twitter:image"]', node => node.element.content], | ||
['meta[name="thumbnail"]', node => node.element.content], | ||
['img', node => node.element.src], | ||
], | ||
title: [ | ||
['meta[property="og:title"]', node => node.element.content], | ||
['meta[property="twitter:title"]', node => node.element.content], | ||
['meta[name="hdl"]', node => node.element.content], | ||
['title', node => node.element.text], | ||
], | ||
type: [ | ||
['meta[property="og:type"]', node => node.element.content], | ||
], | ||
url: [ | ||
['meta[property="og:url"]', node => node.element.content], | ||
['link[rel="canonical"]', node => node.element.href], | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,40 @@ | ||
const {dom, rule, ruleset} = require('fathom-web'); | ||
|
||
|
||
function buildRuleset(name, rules) { | ||
const reversedRules = Array.from(rules).reverse(); | ||
const builtRuleset = ruleset(...reversedRules.map(([query, handler], order) => rule( | ||
dom(query), | ||
node => [{ | ||
score: order, | ||
flavor: name, | ||
notes: handler(node), | ||
}] | ||
))); | ||
|
||
return doc => { | ||
const kb = builtRuleset.score(doc); | ||
const maxNode = kb.max(name); | ||
if (maxNode) { | ||
const value = maxNode.flavors.get(name); | ||
if (value) { | ||
return value.trim(); | ||
const DEFAULT_RULESET = require('./lib/default-ruleset'); | ||
|
||
const MetadataParser = { | ||
metadataRules: Object.assign({}, DEFAULT_RULESET), | ||
buildRuleset(name, rules) { | ||
const reversedRules = Array.from(rules).reverse(); | ||
const builtRuleset = ruleset(...reversedRules.map(([query, handler], order) => rule( | ||
dom(query), | ||
node => [{ | ||
score: order, | ||
flavor: name, | ||
notes: handler(node), | ||
}] | ||
))); | ||
|
||
return doc => { | ||
const kb = builtRuleset.score(doc); | ||
const maxNode = kb.max(name); | ||
if (maxNode) { | ||
const value = maxNode.flavors.get(name); | ||
if (value) { | ||
return value.trim(); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
const titleRules = buildRuleset('title', [ | ||
['meta[property="og:title"]', node => node.element.content], | ||
['meta[property="twitter:title"]', node => node.element.content], | ||
['meta[name="hdl"]', node => node.element.content], | ||
['title', node => node.element.text], | ||
]); | ||
|
||
const canonicalUrlRules = buildRuleset('url', [ | ||
['meta[property="og:url"]', node => node.element.content], | ||
['link[rel="canonical"]', node => node.element.href], | ||
]); | ||
|
||
|
||
const iconRules = buildRuleset('icon', [ | ||
['link[rel="apple-touch-icon"]', node => node.element.href], | ||
['link[rel="apple-touch-icon-precomposed"]', node => node.element.href], | ||
['link[rel="icon"]', node => node.element.href], | ||
['link[rel="fluid-icon"]', node => node.element.href], | ||
['link[rel="shortcut icon"]', node => node.element.href], | ||
['link[rel="Shortcut Icon"]', node => node.element.href], | ||
['link[rel="mask-icon"]', node => node.element.href], | ||
]); | ||
|
||
const imageRules = buildRuleset('image', [ | ||
['meta[property="og:image"]', node => node.element.content], | ||
['meta[property="twitter:image"]', node => node.element.content], | ||
['meta[name="thumbnail"]', node => node.element.content], | ||
['img', node => node.element.src], | ||
]); | ||
|
||
const descriptionRules = buildRuleset('description', [ | ||
['meta[property="og:description"]', node => node.element.content], | ||
['meta[name="description"]', node => node.element.content], | ||
]); | ||
|
||
const typeRules = buildRuleset('type', [ | ||
['meta[property="og:type"]', node => node.element.content], | ||
]); | ||
|
||
|
||
const metadataRules = { | ||
description: descriptionRules, | ||
icon_url: iconRules, | ||
image_url: imageRules, | ||
title: titleRules, | ||
type: typeRules, | ||
url: canonicalUrlRules | ||
}; | ||
}, | ||
getMetadata(doc) { | ||
const metadata = {}; | ||
|
||
Object.keys(MetadataParser.metadataRules).forEach(metadataKey => { | ||
const metadataRule = MetadataParser.buildRuleset(metadataKey, MetadataParser.metadataRules[metadataKey]); | ||
metadata[metadataKey] = metadataRule(doc); | ||
}); | ||
|
||
return metadata; | ||
} | ||
}; | ||
|
||
|
||
function getMetadata(doc, rules) { | ||
const metadata = {}; | ||
const ruleSet = rules || metadataRules; | ||
|
||
Object.keys(ruleSet).map(metadataKey => { | ||
const metadataRule = ruleSet[metadataKey]; | ||
metadata[metadataKey] = typeof metadataRule === 'function' ? | ||
metadataRule(doc) : | ||
getMetadata(doc, metadataRule); | ||
}); | ||
|
||
return metadata; | ||
} | ||
|
||
module.exports = { | ||
metadataRules, | ||
getMetadata | ||
}; | ||
module.exports = MetadataParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters