Skip to content

Commit

Permalink
Break out transforms; replace htmlmin with beautify
Browse files Browse the repository at this point in the history
  • Loading branch information
MWDelaney committed Nov 4, 2024
1 parent 097df44 commit ddfbf85
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 40 deletions.
40 changes: 10 additions & 30 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* - `src/config/watchtargets.js`
* - `src/config/templateLanguages.js`
* - `src/config/filters.js`
* - `src/config/transforms.js`
*/

/**
Expand Down Expand Up @@ -51,12 +52,15 @@ import filters from './src/config/filters.js';
*/
import build from './src/config/build.js';

/**
* Import transforms from /src/config/transforms.js
*/
import transforms from './src/config/transforms.js';

/**
* Any additional requirements can be added here
*/
import fs from 'fs';
import chalk from 'chalk';
import htmlmin from 'html-minifier';

/**
* Eleventy configuration
Expand Down Expand Up @@ -182,36 +186,12 @@ export default function(eleventyConfig) {
build(eleventyConfig);

/**
* Minify HTML output
*/
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
// Eleventy 1.0+: use this.inputPath and this.outputPath instead
if (this.outputPath && this.outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}

return content;
* Add transforms from /src/config/transforms.js
*/
Object.keys(transforms).forEach((transformName) => {
transforms[transformName](eleventyConfig);
});

/**
* Minify XML output
*/
eleventyConfig.addTransform("xmlmin", function (content, outputPath) {
// Eleventy 1.0+: use this.inputPath and this.outputPath instead
if (this.outputPath && this.outputPath.endsWith(".xml")) {
let minified = htmlmin.minify(content, {
collapseWhitespace: true
});
return minified;
}

return content;
});

/**
* Configure dev server
Expand Down
6 changes: 3 additions & 3 deletions README.ZeroPoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Create and edit your site **right from this readme file!**.

Or edit your entire site in a web-based editor by [clicking here](https://github.dev/MWDelaney/ZeroPoint/).
🪐 Or edit your entire site in a web-based editor by [clicking here](https://github.dev/MWDelaney/ZeroPoint/).

<details>
<summary><strong>📝 Creating and editing pages</strong></summary>
Expand Down Expand Up @@ -92,7 +92,7 @@ Once you set up deployment, any time you commit to your repository's `main` bran
Ready to go deeper? Here's how ZeroPoint is laid out:

```sh
example.com # → Root of your ZeroPoint-based project
example.com # → Root of your project
├── src/ # → Source directory
│ ├── assets/ # → Site assets
│ │ ├── fonts/
Expand Down Expand Up @@ -120,7 +120,7 @@ example.com # → Root of your ZeroPoint-based project
│ └── site.json # → Site branding configuration
├── .eleventy.js # → Core Eleventy config file
├── netlify.toml # → Netlify deployment and plugin configuration (optional)
├── README.ZeroPoint.md #ZeroPoint readme
├── README.ZeroPoint.md #Template repository readme
└── README.md
```

Expand Down
2 changes: 1 addition & 1 deletion src/config/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
/**
* Pages
*/
pages: function (eleventyConfig) {
pages: async function (eleventyConfig) {
// Get all `.md` files in the `src/pages` directory
eleventyConfig.addCollection("pages", function(collectionApi) {
return collectionApi.getFilteredByGlob("src/content/pages/**/*.md");
Expand Down
2 changes: 1 addition & 1 deletion src/config/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import markdownIt from "markdown-it";

export default {
// Markdown filter
markdown: function (eleventyConfig) {
markdown: async function (eleventyConfig) {
let options = {
html: true,
breaks: true,
Expand Down
4 changes: 2 additions & 2 deletions src/config/passthroughs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
/**
* Copy images to the `public` directory
*/
images: function () {
images: async function () {
let config = { 'src/assets/images': 'assets/images' }

// Return the config to .eleventy.js
Expand All @@ -18,7 +18,7 @@ export default {
/**
* Copy fonts to the `public` directory
*/
fonts: function () {
fonts: async function () {
let config = { 'src/assets/fonts': 'assets/fonts' }

// Return the config to .eleventy.js
Expand Down
2 changes: 1 addition & 1 deletion src/config/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
* Metagen plugin
* https://github.com/tannerdolby/eleventy-plugin-metagen
*/
metagen: function (eleventyConfig) {
metagen: async function (eleventyConfig) {
// Add plugin to eleventyConfig
eleventyConfig.addPlugin(metagenPlugin);
},
Expand Down
4 changes: 2 additions & 2 deletions src/config/shortcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export default {
* By Stephanie Eckles
* https://11ty.rocks/eleventyjs/dates/
*/
year: function (eleventyConfig) {
year: async function (eleventyConfig) {
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
},

/**
* Add image shortcode (requires image plugin)
* https://www.11ty.dev/docs/plugins/image/
*/
image: function (eleventyConfig) {
image: async function (eleventyConfig) {
async function imageShortcode(src, alt = "", className = "", style = "", sizes = "") {
let options = {
widths: [null],
Expand Down
39 changes: 39 additions & 0 deletions src/config/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Add Eleventy transforms here
* https://www.11ty.dev/docs/transforms/
*/
import beautify from "js-beautify";

export default {

/**
* Beautify HTML and XML output
*/
beautify: async function (eleventyConfig) {
eleventyConfig.addTransform("beautify", async function (content) {
let types = [
".html",
".xml",
];

if (this.page.outputPath) {
for (let type of types) {
if (this.page.outputPath.endsWith(type)) {
return beautify.html(content, {
indent_size: (process.env.ELEVENTY_ENV === "production" ? 0 : 2),
indent_char: " ",
max_preserve_newlines: 1,
preserve_newlines: false,
indent_inner_html: true,
end_with_newline: true,
wrap_line_length: 120,
extra_liners: ["head", "body", "/html"],
});
}
}
}

return content;
});
}
}

0 comments on commit ddfbf85

Please sign in to comment.