Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: special character attributes in body mess up markup #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,10 @@ function objectToAttributes(obj) {
}, "");
}

/**
* Checks for `<body>` tag in a string.
*
* @param {string} content The content
* @return {boolean} `True` if body tag, `False` otherwise.
*/
function hasBodyTag(content) {
const hasBody = /<\s*body(\w|\s|=|"|-)*>/gm;
return hasBody.test(content);
}

module.exports = {
isUrl,
isAllowedExtension,
parseStringToNumbers,
generateWidths,
objectToAttributes,
hasBodyTag,
};
5 changes: 2 additions & 3 deletions lib/img2picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
generateWidths,
parseStringToNumbers,
objectToAttributes,
hasBodyTag,
} = require("./helpers.js");

/**
Expand Down Expand Up @@ -210,7 +209,7 @@ async function generateImage(attrs, options) {
*/
async function replaceImages(content, options) {
const { extensions, fetchRemote } = options;
const $ = cheerio.load(content);
const $ = cheerio.load(content, null, false);
let images = $("img")
.not("picture img") // Ignore images wrapped in <picture>
.not("[data-img2picture-ignore]") // Ignore excluded images
Expand Down Expand Up @@ -243,7 +242,7 @@ async function replaceImages(content, options) {
$(images[i]).replaceWith(picture);
});

return hasBodyTag(content) ? $.html() : $("body").html();
return $.html();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
"license": "MIT",
"dependencies": {
"@11ty/eleventy-img": "^2.0.1",
"cheerio": "^1.0.0-rc.10",
"cheerio": "^1.0.0-rc.11",
"debug": "^4.3.4"
},
"devDependencies": {
"ava": "^3.15.0",
"eslint": "^8.15.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-xo-space": "^0.32.0",
"eslint-config-xo-space": "^0.33.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^8.0.1",
"image-size": "^1.0.1",
"lint-staged": "^12.4.1",
"lint-staged": "^13.0.0",
"nyc": "^15.1.0",
"prettier": "^2.6.2",
"rimraf": "^3.0.2"
Expand Down