Skip to content
This repository has been archived by the owner on Oct 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #118 from aarongustafson/image-transform-enhancements
Browse files Browse the repository at this point in the history
Image transform enhancements
  • Loading branch information
Andy-set-studio authored Apr 28, 2020
2 parents 2e9897c + 7d33f3c commit 960a133
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hylia",
"version": "0.6.0",
"version": "0.7.0",
"description": "A simple Eleventy starter kit to help you have a blog of your own",
"main": "index.js",
"dependencies": {
Expand All @@ -10,6 +10,7 @@
"@tbranyen/jsdom": "^13.0.0",
"concurrently": "^4.1.2",
"html-minifier": "^4.0.0",
"image-size": "^0.8.3",
"json-to-scss": "^1.5.0",
"sass": "^1.26.3",
"semver": "^6.3.0",
Expand Down
5 changes: 5 additions & 0 deletions src/scss/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ hr {
margin: get-size(900) auto;
}

// For when metric attributes are added to img elements
img {
height: auto;
}

:focus {
outline: 1px solid var(--color-theme-primary-glare);
outline-offset: 0.25rem;
Expand Down
12 changes: 11 additions & 1 deletion src/transforms/parse-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const jsdom = require('@tbranyen/jsdom');
const {JSDOM} = jsdom;
const minify = require('../utils/minify.js');
const slugify = require('slugify');
const getSize = require('image-size');

module.exports = function(value, outputPath) {
if (outputPath.endsWith('.html')) {
Expand All @@ -10,7 +11,7 @@ module.exports = function(value, outputPath) {
});

const document = DOM.window.document;
const articleImages = [...document.querySelectorAll('main article img')];
const articleImages = [...document.querySelectorAll('main article img, .intro img')];
const articleHeadings = [
...document.querySelectorAll('main article h2, main article h3')
];
Expand All @@ -20,6 +21,15 @@ module.exports = function(value, outputPath) {
articleImages.forEach(image => {
image.setAttribute('loading', 'lazy');

const file = image.getAttribute('src');

if (file.indexOf('http') < 0) {
const dimensions = getSize('src' + file);

image.setAttribute('width', dimensions.width);
image.setAttribute('height', dimensions.height);;
}

// If an image has a title it means that the user added a caption
// so replace the image with a figure containing that image and a caption
if (image.hasAttribute('title')) {
Expand Down

0 comments on commit 960a133

Please sign in to comment.