Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@abovedave
Copy link
Contributor

@abovedave abovedave commented Oct 6, 2017

Removes the previously undocumented page.settings.beautify option & closes #154 (related to #162).

Adds the ability to manipulate the raw output of the template engine, before the page is cached and served. Similar to the Wordpress feature, it is useful for linting (thinking RSS feeds in particular), minifying and formatting. They are loaded in order of definition, global first, and the output is passed from one to the next so you can chain functions.

Example usage: minifying HTML.

config.json

"globalPostProcessors": [
    "minify-html"
]

The default location in paths.processors is workspace/processors

/workspace/processors/minify-html.js (using html-minifier)

const minify = require('html-minifier').minify

module.exports = (data, output) => {
  return minify(output, {
  	collapseWhitespace: true,
  	minifyCSS: true,
  	minifyJS: true,
  	removeRedundantAttributes: true,
  	useShortDoctype: true,
  	removeAttributeQuotes: true
  })
}

Note:

  • the page json data object is also passed
  • in addition it can be defined or disabled at page level in the page.settings

Add a processor, in addition to the global

"settings": {
  "postProcessors": ["remove-swear-words"]
}

Disable for given page

"settings": {
  "postProcessors": false
}

Upgrading from page.settings.beautify

Install package

npm install js-beautify --save

New file workspace/processors/beautify-html.js

const beautifyHtml = require('js-beautify').html

module.exports = (data, output) => {
  return beautifyHtml(output)
}

Old page.json

{
  "page": {
    "name": "Sitemap page",
    "description": "Sitemap",
    "language": "en"
  },
  "settings": {
    "cache": true,
    "beautify": true
  },
  ...
}

New page.json

{
  "page": {
    "name": "Sitemap page",
    "description": "Sitemap",
    "language": "en"
  },
  "settings": {
    "cache": true,
    "postProcessors": [
      "beautify-html"
     ]
  },
  ...
}

@abovedave abovedave changed the title Feature: post processors 📬 Feature: post processors Oct 6, 2017
Copy link
Contributor

@jimlambie jimlambie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@jimlambie
Copy link
Contributor

This is a breaking change because it removes beautifying from core?

@abovedave
Copy link
Contributor Author

Yes exactly, that setting will no longer have any effect...not sure how to handle it since it was undocumented anyway? You could only turn it on page-by-page in the settings block.

AFAIK we're not using it in any client work either.

@jimlambie
Copy link
Contributor

Perhaps we should make some enquiries then - if it's not used we can simply make it a new feature and release as a minor version

@abovedave
Copy link
Contributor Author

Added upgrade notes from settings.beautify

Copy link
Contributor

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool! Just added some minor comments.

.then(output => {
let err = null
// Send the templated page
let templateData = Object.assign(this.data, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a const?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, to avoid mutating this.data, you could do Object.assign({}, this.data, (...)).


// Post-process the output
if (config.get('globalPostProcessors') || this.page.postProcessors) {
let postProcessors = config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a const?

keepWhitespace: this.page.keepWhitespace
})
.then(output => {
let err = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable doesn't seem to ever be changed. You're returning an error on L60 but it comes from L59. Could we remove this initialisation and just do return done(null, output) on L65?

@abovedave
Copy link
Contributor Author

Great suggestions, thanks @eduardoboucas!

@jimlambie
Copy link
Contributor

This still has to be a major release, right? If we're not bundling in a beautify processor and honouring the existing page setting for beautify, then it's a breaking change. This would make it Web 5.0.

@abovedave
Copy link
Contributor Author

Yep makes sense 👍

@eduardoboucas
Copy link
Contributor

Agree 💯 %.

@jimlambie
Copy link
Contributor

I'm a bit wary of another major release so soon after 4.0, is all. Feels like we're following the FB Messenger app approach - they're up to version 133 or something. Mind you, their release notes are awful. "Performance enhancements".

@eduardoboucas
Copy link
Contributor

That's understandable. If this is not an urgent requirement, perhaps we can hit pause and bundle it with other breaking changes for the next major release? ⏸

@abovedave
Copy link
Contributor Author

Could we prepare a 5.0 branch perhaps? Just conscience that this branch will get stale and drift from head.

@eduardoboucas
Copy link
Contributor

Good idea. We could start a release/5.0 branch – I think this is the convention we agreed on, right @jimlambie?

@abovedave abovedave changed the base branch from develop to release/5.0 October 12, 2017 12:42
@abovedave abovedave changed the base branch from release/5.0 to develop October 13, 2017 09:02
@abovedave abovedave merged commit 60e7a6a into develop Oct 13, 2017
@abovedave abovedave deleted the feature/postProcessors branch December 6, 2017 15:29
@abovedave abovedave mentioned this pull request Apr 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expand on beautify output options

4 participants