Skip to content

Commit

Permalink
Merge pull request #50 from SG5/language-support
Browse files Browse the repository at this point in the history
add language to options, fix #46
  • Loading branch information
Oscar Franco authored Oct 31, 2019
2 parents 0ebe026 + 66bea57 commit ece71e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ Additionally you can pass an options object which should add more functionality
| Property Name | Result |
| ------------- |:-------------:|
| imagesPropertyType (**optional**) (ex: 'og') | Fetches images only with the specified property, `meta[property='${imagesPropertyType}:image']` |
| language (**optional**) (ex: 'de', 'en-US') | Fetch content with specific language |


```javascript
LinkPreview.getPreview(
'https://www.youtube.com/watch?v=MejbOFk7H6c',
{
imagesPropertyType: 'og', // fetches only open-graph images
language: 'fr-CA', // fetches site for French language
})
.then(data => console.debug(data));
```
Expand Down
8 changes: 8 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('link preview', () => {
expect(linkInfo.contentType.toLowerCase()).to.be.equal('text/html; charset=utf-8');
});

it('should make request with different languages', async () => {
let linkInfo = await LinkPreview.getPreview('https://www.hsbc.ca/', {language: 'fr'});
expect(linkInfo.title).to.be.equal('Particuliers | HSBC Canada');

linkInfo = await LinkPreview.getPreview('https://www.hsbc.ca/');
expect(linkInfo.title).to.be.equal('HSBC Personal Banking | HSBC Canada');
});

it('should handle audio urls', async () => {
const linkInfo = await LinkPreview.getPreview('https://ondemand.npr.org/anon.npr-mp3/npr/atc/2007/12/20071231_atc_13.mp3');

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ exports.getPreview = function (text, options) {
});

if (detectedUrl) {
fetch(detectedUrl)
var fetchOptions = {}
if (options && options.language) {
fetchOptions.headers = {
'Accept-Language': options.language
}
}
fetch(detectedUrl, fetchOptions)
.then(function (response) {

// get final URL (after any redirects)
Expand Down

0 comments on commit ece71e4

Please sign in to comment.