diff --git a/README.md b/README.md index bc243ab..6a83f2d 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ This library ... - original url comes from canonical's href attribute - e.g. `` is replaced with `` - NOTE: If the canonical url in your html is not absolute url this function would be passed +- remove all ``, `` - optimize html by using [@ampproject/toolbox-optimizer](https://github.com/ampproject/amp-toolbox/tree/master/packages/optimizer) ## Preparation @@ -131,5 +132,5 @@ This library ... To make your html valid AMP your html also should ... - have canonical meta tag to regular HTML -- not have any problem if all scripts are removed -- not have any problem if all `!imporant` syntax are removed in css +- Don't have any problem if all scripts are removed +- Don't have any problem if all `!imporant` syntax are removed in css diff --git a/index.js b/index.js index cf01dc4..ecff88f 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,8 @@ const ga = require('./lib/googleanalytics') const iframe = require('./lib/iframe') const boilerplate = require('./lib/boilerplate') const serviceworker = require('./lib/serviceWorker') -const link = require('./lib/link') +const link = require('./lib/link/canonical') +const preload = require('./lib/link/preload') const toolbox = require('./lib/toolbox') const html = require('./lib/html') @@ -27,6 +28,7 @@ const html2amp = async (htmlString, options = {}) => { $ = ga($, options) $ = iframe($, options) $ = link($) + $ = preload($) $ = serviceworker($, options) $ = boilerplate($, options) htmlString = html($, options) diff --git a/lib/link.js b/lib/link/canonical.js similarity index 91% rename from lib/link.js rename to lib/link/canonical.js index 54c4e3c..cda5eb7 100644 --- a/lib/link.js +++ b/lib/link/canonical.js @@ -14,11 +14,11 @@ const replaceOriginal = ($, elems, url) => { }) } -const link = ($) => { +const canonical = ($) => { const canonicalUrl = $('link[rel=canonical]').attr('href') if (!canonicalUrl) return $ replaceOriginal($, $('a[href]'), canonicalUrl) return $ } -module.exports = link +module.exports = canonical diff --git a/lib/link/preload.js b/lib/link/preload.js new file mode 100644 index 0000000..b35f797 --- /dev/null +++ b/lib/link/preload.js @@ -0,0 +1,7 @@ +const preload = ($) => { + $('link[rel="preload"][as="script"]').remove() + $('link[rel="preload"][as="fetch"]').remove() + return $ +} + +module.exports = preload diff --git a/test/link.test.js b/test/link/canonical.test.js similarity index 82% rename from test/link.test.js rename to test/link/canonical.test.js index d442fbb..f347d32 100644 --- a/test/link.test.js +++ b/test/link/canonical.test.js @@ -1,15 +1,15 @@ const cheerio = require('cheerio') -const assert = require('./assert') -const link = require('../lib/link') -const htmlFactory = require('./html') +const assert = require('../assert') +const canonical = require('../../lib/link/canonical') +const htmlFactory = require('../html') -describe('link', function () { +describe('canonical', function () { const head = '' describe('if there is a link which href starts with /', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) @@ -17,7 +17,7 @@ describe('link', function () { describe('if there is a link which href starts with ./test', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) @@ -25,7 +25,7 @@ describe('link', function () { describe('if there is a link which href starts with ../test', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) @@ -33,7 +33,7 @@ describe('link', function () { describe('if there is a link which href starts with `test`', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) @@ -41,7 +41,7 @@ describe('link', function () { describe('if there is a link which href starts with http', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) @@ -49,7 +49,7 @@ describe('link', function () { describe('if there is a link which href starts with //', function () { const html = htmlFactory({ head, body: '' }) it('should be replaced with origin in canonical', function () { - const $ = link(cheerio.load(html)) + const $ = canonical(cheerio.load(html)) assert($, htmlFactory({ head, body: '' })) }) }) diff --git a/test/link/preload.test.js b/test/link/preload.test.js new file mode 100644 index 0000000..2be33f2 --- /dev/null +++ b/test/link/preload.test.js @@ -0,0 +1,28 @@ +const cheerio = require('cheerio') +const assert = require('../assert') +const canonical = require('../../lib/link/preload') +const htmlFactory = require('../html') + +describe('preload', function () { + describe('If there is link tag for preloading script', function () { + const html = htmlFactory({ head: '' }) + it('should be removed', function () { + const $ = canonical(cheerio.load(html)) + assert($, htmlFactory({ head: '' })) + }) + }) + describe('If there is link tag for preloading fetch resource', function () { + const html = htmlFactory({ head: '' }) + it('should be removed', function () { + const $ = canonical(cheerio.load(html)) + assert($, htmlFactory({ head: '' })) + }) + }) + describe('If there is link tag for preloading style', function () { + const html = htmlFactory({ head: '' }) + it('shouldn\'t be removed', function () { + const $ = canonical(cheerio.load(html)) + assert($, htmlFactory({ head: '' })) + }) + }) +})