Open mfulton26 opened 4 years ago
My img is
<img data-src="https://mmbiz.qpic.cn/mmbiz_png/xxxxxxxxxxx/640?wx_fmt=png"
data-type="png"
data-w="2560"
style="display: block;margin: 0 auto;max-width: 100%;">
So my code is:
turndownService.addRule('img', {
filter: "img",
replacement: function (content, node, options) {
return "![](" + node.getAttribute('data-src') + ")";
}
})
Small advancement on the above to get the largest src from a srcset (I needed this for transferring assets from one CMS to another):
turndownService.addRule("img", {
filter: "img",
replacement: function (_content, node, _options) {
const srcset = node.getAttribute("srcset") || "";
const srcs = Object.fromEntries(
srcset.split(",").map((src) => src.trim().split(" ").reverse())
);
const largest =
srcs[`${Math.max(...Object.keys(srcs).map((src) => parseInt(src)))}w`];
return "![](" + largest + ")";
},
});
some sites set an invalid
src
attribute on<img>
elements (sadly) and rely on the browser's support ofsrcset
it would be wonderful if Turndown could use the smallest or largest image specified in the
srcset
instead of the value insrc
example
input
output
actual
expected/desired