Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
Return keywords as array instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
pdehaan committed Aug 23, 2016
1 parent 7e8b27f commit b88045b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ function buildRuleset(name, rules) {
if (maxNode) {
const value = maxNode.flavors.get(name);
if (value) {
return value.trim();
if (value instanceof String) {
return value.trim();
}
return value;
}
}
};
}

function keywordsToArray(str) {
const keywords = str.toString().split(',');
return keywords.map(keyword => keyword.trim());
}


const titleRules = buildRuleset('title', [
['meta[property="og:title"]', node => node.element.content],
Expand All @@ -38,7 +46,7 @@ const canonicalUrlRules = buildRuleset('url', [
]);

const keywordsRules = buildRuleset('keywords', [
['meta[name="keywords"]', node => node.element.content],
['meta[name="keywords"]', node => keywordsToArray(node.element.content)],
]);

const iconRules = buildRuleset('icon', [
Expand Down
6 changes: 3 additions & 3 deletions tests/metadataRules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function ruleTest(testName, testRule, expected, testTag) {
const html = buildHTML(testTag);
const doc = stringToDom(html);
const found = testRule(doc);
assert.equal(found, expected, `Unable to find ${testName} in ${html}`);
assert.deepEqual(found, expected, `Unable to find ${testName} in ${html}`);
});
}

Expand Down Expand Up @@ -105,10 +105,10 @@ describe('Type Rule Tests', function() {


describe('Keywords Rule Tests', function() {
const keywords = 'Cats, Kitties, Meow';
const keywords = ['Cats', 'Kitties', 'Meow'];

const ruleTests = [
['keywords', `<meta name="keywords" content="${keywords}" />`],
['keywords', `<meta name="keywords" content="${keywords.join(', ')}" />`],
];

ruleTests.map(([testName, testTag]) => ruleTest(testName, metadataRules.keywords, keywords, testTag));
Expand Down

0 comments on commit b88045b

Please sign in to comment.