High-performance HTML pre-processor designed to be run in middleware, but also suitable for packaging up in a build task. Utilizes a streaming API based on htmlparser2. Useful when you need to apply basic markup modifications to HTML files at run-time and as a zero-config alternative to gulp and grunt plugins for optimizing release versions of HTML pages.
Makes use of standard-compliant
data-* attributes to annotate the markup indicating where custom processing should occur. These attributes are stripped out of the final output, so the HTML sent down to the browser leaves no traces behind.
Intended as a light-weight HTML decorator rather than a full-blown template engine such as Jade. However it's certainly possible for HTML generated by Jade, or another template language, to be piped through htmlprep.
Use in express route or middleware:
var htmlprep = ;res;fs;
Prepend prefix to asset URLs
Prepend a string to all
src and link
href attributes. Canonical use case is repointing assets to a CDN. If the
assetPathPrefix option is set to "//cdnhost.com/assets" then:
You can prevent this behavior for specific assets by providing a list of glob patterns to the
noPathPrefixPatterns option. The value of the each asset path, i.e.
/images/logo.gif is evaluated against each pattern using minimatch.
For example if you wanted all
.jpg files in the images directory to be left alone, you'd do this:
Omit HTML blocks or individual tags based on a custom build attribute. Useful for declaring which scripts or stylesheets should be present in release vs. debug mode.
When called with
Inject blocks of HTML at render time to the
<body> tags or to a named block specified with the
before navafter nav<!-- End of body -->
Declare a single
link with a glob pattern and automatically expand to individual elements for every matching file. File matches are relative to the directory specified in the
Appends the livereload script at the end of the body.
All the possible attributes that can be specifed in the
buildType- Remove all elements that have a
data-buildattribute whose value does not match the specified value.
liveReload- Specify if the localhost LiveReload script should be injected.
liveReloadPort- The port number for livereload.js, defaults to 35729.
assetPathPrefix- The string to prepend to all relative asset URLs. Works with
inject- Object of HTML blocks to inject over placeholders. The keys
headwill append to those respective tag names rather than a corresponding placeholder.
attrPrefix- Specify a custom prefix for data attributes. So instead of
data-build, you could use
cwd- The current working directory. Defaults to
User-Agent specific blocks
Exclude blocks of HTML where the request user-agent does not pass an expression test. Useful for including polyfill scripts conditionally or excluding content from mobile devices to lighten the HTML payload.
Licensed under the Apache License, Version 2.0.