Skip to content

Commit

Permalink
Merge pull request #88 from tomoyukikashiro/feat/optimizer
Browse files Browse the repository at this point in the history
fix(toolbox): Added function to optimize html
  • Loading branch information
t-jindai authored Oct 14, 2019
2 parents d006762 + af04bce commit 5c44147
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ name | default value | note
cwd|`.`|image / styles base path
gaConfigPath| |`amp-analytics` config json path for [google analytics](https://www.ampproject.org/docs/analytics/analytics-vendors)
serviceWorker| |attributes of [amp-install-serviceworker](https://www.ampproject.org/docs/reference/components/amp-install-serviceworker) <br/> e.g. `src`, `data-iframe-src`

optimize|false| if true, this module will optimize the html by using [@ampproject/toolbox-optimizer](https://github.com/ampproject/amp-toolbox/tree/master/packages/optimizer)

## Functions

Expand All @@ -76,6 +76,7 @@ This library ...
- replace all a tag links which destination is original site with absolute url which starts with http[s]
- original url comes from canonical's href attribute
- e.g. `<a href="/test">` is replaced with `<a href="https//original-url.com/test">`
- optimize html by using [@ampproject/toolbox-optimizer](https://github.com/ampproject/amp-toolbox/tree/master/packages/optimizer)

## Preparation

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

const cheerio = require('cheerio')
const amp = require('./lib/amp')
const css = require('./lib/css')
Expand All @@ -11,6 +12,7 @@ const iframe = require('./lib/iframe')
const boilerplate = require('./lib/boilerplate')
const serviceworker = require('./lib/serviceWorker')
const link = require('./lib/link')
const toolbox = require('./lib/toolbox')

const html2amp = async (html, options = {}) => {
let $ = cheerio.load(html)
Expand All @@ -26,7 +28,8 @@ const html2amp = async (html, options = {}) => {
$ = link($)
$ = serviceworker($, options)
$ = boilerplate($, options)
return $.html()
html = await toolbox.optimizer($.html(), options)
return html
}

module.exports = html2amp
8 changes: 8 additions & 0 deletions lib/toolbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const AmpOptimizer = require('@ampproject/toolbox-optimizer')

const ampOptimizer = AmpOptimizer.create()

exports.optimizer = (html, options) => {
if (!options.optimize) return Promise.resolve(html)
return ampOptimizer.transformHtml(html)
}
88 changes: 77 additions & 11 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"homepage": "https://github.com/tomoyukikashiro/html2amp#readme",
"dependencies": {
"@ampproject/toolbox-optimizer": "^1.1.2-alpha.0",
"axios": "^0.19.0",
"cheerio": "^1.0.0-rc.2",
"clean-css": "^4.2.1",
Expand Down
Loading

0 comments on commit 5c44147

Please # to comment.