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

Commit

Permalink
Add support for language detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanpatel committed Dec 18, 2018
1 parent 73dfd06 commit 66397fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 10 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ const metadataRuleSets = {
],
},

language: {
rules: [
['html[lang]', element => element.getAttribute('lang')],
['meta[name="language" i]', element => element.getAttribute('content')],
],
processors: [
(language, context) => language.split('-')[0]
]
},

type: {
rules: [
['meta[property="og:type"]', element => element.getAttribute('content')],
Expand Down
21 changes: 20 additions & 1 deletion tests/getMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ describe('Get Metadata Tests', function() {
const sampleType = 'article';
const sampleUrl = 'http://www.example.com/';
const sampleProviderName = 'Example Provider';
const sampleLanguage = 'en';


const sampleHtml = `
<html>
<html lang="en-CA">
<head>
<meta property="og:description" content="${sampleDescription}" />
<link rel="icon" href="${sampleIcon}" />
Expand All @@ -63,6 +65,8 @@ describe('Get Metadata Tests', function() {
assert.equal(metadata.type, sampleType, `Unable to find ${sampleType} in ${sampleHtml}`);
assert.equal(metadata.url, sampleUrl, `Unable to find ${sampleUrl} in ${sampleHtml}`);
assert.equal(metadata.provider, sampleProviderName, `Unable to find ${sampleProviderName} in ${sampleHtml}`);
assert.equal(metadata.language, sampleLanguage, `Unable to find ${sampleLanguage} in ${sampleHtml}`);

});

it('uses absolute URLs when url parameter passed in', () => {
Expand Down Expand Up @@ -164,6 +168,21 @@ describe('Get Metadata Tests', function() {

});

it('finds language in metadata', () => {
const html = `
<html>
<head>
<meta name="language" content="en-CA" />
</head>
</html>
`;

const doc = stringToDom(html);
const metadata = getMetadata(doc, sampleUrl, metadataRuleSets);

assert.equal(metadata.language, sampleLanguage, `Unable to find ${sampleLanguage} in ${html}`);
});

it('allows custom rules', () => {
const doc = stringToDom(sampleHtml);
const rules = {
Expand Down

0 comments on commit 66397fb

Please sign in to comment.