Skip to content

Commit

Permalink
feature(CannonicalURL) #22 Add support for cannonical URL (#27)
Browse files Browse the repository at this point in the history
* feature(CannonicalURL) #22 Add support for cannonical URL as a new parameter

* feature(CannonicalURL) #22 Update readme with documentation

Co-authored-by: Ruslan Kazakov <[email protected]>
  • Loading branch information
blanxi and rkazakov authored Jul 1, 2022
1 parent f8959f7 commit e813f1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ npm install ampify
```js
const ampify = require('ampify');
const html = '<YOUR_HTML_CONTENT>';
const amp = ampify(html, {cwd: 'amp'});
const amp = ampify(html, {cwd: 'amp'}, 'https://<your-canoncial-url>');
console.log(amp); // Content of AMP HTML
```

Expand All @@ -36,6 +36,9 @@ console.log(amp); // Content of AMP HTML

### round

## Canonical URL
With the third parameter you can set the Cannonical URL, should be a full valid URL, please refer to [page discoverable](https://www.ampproject.org/docs/fundamentals/discovery)

### Enable images dimensions rounding

- Type: `String`
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cheerio = require('cheerio');
const sizeOf = require('image-size');
const CleanCss = require('clean-css');

module.exports = async (html, options) => {
module.exports = (html, options, canonicalURL) => {
const tags = {
amp: ['img', 'video'],
};
Expand Down Expand Up @@ -87,6 +87,10 @@ module.exports = async (html, options) => {
$('head meta[charset="UTF-8"]').remove();
$('head').prepend('<meta charset="utf-8">');

if (canonicalURL) {
$('head').append(`<link rel="canonical" href="${canonicalURL}">`);
}

/* google analytics */
$('script').each((index, element) => {
const src = $(element).attr('src');
Expand Down
4 changes: 2 additions & 2 deletions test/assert.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ampify = require('../');

module.exports = async (input, output, options) => {
const processed = await ampify(input, options);
module.exports = (input, output, options, canonicalURL) => {
const processed = ampify(input, options, canonicalURL);
expect(processed).toBe(output);
};
9 changes: 9 additions & 0 deletions test/header.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e813f1a

Please sign in to comment.