axe312ger / metalsmith-adaptive-images

DEPRECATED: A plugin for Metalsmith to create adaptive images via srcset and styles properties
9 stars 1 forks source link

Plugin replaces all Cyrillic characters with html entities #42

Closed peremenov closed 6 years ago

peremenov commented 6 years ago

I'm using configuration like this:

const images = AdaptiveImages({
  imageWidths: [1440, 960, 480],
  imageSizes: ['(min-width: 960px) 960px', '100vw'],
  defaultSize: 960,
  namingPattern: '{dir}{name}@{size}{ext}', // foo/bar-200.jpg,...
  srcsetPattern: '{url} {size}w', // foo/bar-200.jpg 200w,...
  htmlFileGlob: '**/*.html',
  htmlImageSelector: 'img',
});

...

  .use(templates({
    templateKey: 'layout',
  }))
  .use(images.replaceImages)

Charset of the source files utf-8. As result instead of Зоркий I've got Зоркий

axe312ger commented 6 years ago

This is related to the internal usage of cheerio to replace the pictures and the configuration I pass: https://github.com/axe312ger/metalsmith-adaptive-images/blob/master/src/index.js#L104-L106

I had to do this to get valid HTML back then in the project I developed this for.

If anybody has a replacement for cheerio which will NOT alter the html code besides the image replacement, I'd be super happy to merge this is. A simple regex is IMHO not reliable enough.

axe312ger commented 6 years ago

Can you maybe try to remove the xmlMode: true and see if it is working properly for you? That might be a quick fix.

peremenov commented 6 years ago

As described here non-safe solution could be like this:

file.contents = new Buffer($.html({ decodeEntities: false }));

It works fine for me.

axe312ger commented 6 years ago

Yeah we can't do that :/

var cheerio = require("cheerio")

const $ = cheerio.load('<div>&#x3C;script&#x3E;alert(&#x27;xss&#x27;)&#x3C;/script&#x3E;</div>')

console.log($.html())
// "<div>&lt;script&gt;alert(&apos;xss&apos;)&lt;/script&gt;</div>"

console.log($.html({decodeEntities: false}))
// "<div><script>alert('xss')</script></div>"
peremenov commented 6 years ago

Thank you anyways