diff --git a/README.md b/README.md index 1c123fd..b03f9ff 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ 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 @@ -44,6 +45,7 @@ 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)); ``` diff --git a/__tests__/index.spec.js b/__tests__/index.spec.js index 940bce2..33fe68c 100644 --- a/__tests__/index.spec.js +++ b/__tests__/index.spec.js @@ -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'); diff --git a/index.js b/index.js index 7842b1f..30b468a 100644 --- a/index.js +++ b/index.js @@ -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)