gatsby-remark-image-attributes
Version:
Creates HTML image markup with style and data-* attributes from [`mdAST Image`](https://github.com/syntax-tree/mdast#image) nodes with attributes in their title.
53 lines (52 loc) • 2.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const unist_util_visit_1 = __importDefault(require("unist-util-visit"));
const attribute_image_1 = __importDefault(require("./attribute-image"));
const figure_1 = __importDefault(require("./attribute-image/figure"));
const wrapped_1 = __importDefault(require("./attribute-image/wrapped"));
const options = {
styleAttributes: [],
dataAttributes: false
};
const logMsg = (strings, ...expressions) => {
const message = strings.reduce((msg, str, idx) => `${msg}${str}${expressions[idx] || ''}`, '');
return `[gatsby-remark-image-attributes] ${message}`;
};
const applyOptions = ({ styleAttributes, dataAttributes = false }, reporter) => {
if (styleAttributes) {
reporter.warn(logMsg `The styleAttributes option is deprecated. The plugin uses all CSS properties.`);
}
options.dataAttributes = dataAttributes === true;
};
exports.default = ({ markdownAST, reporter }, userOptions) => {
applyOptions(userOptions, reporter);
const attributeImages = {
figure: [],
root: [],
wrapped: []
};
unist_util_visit_1.default(markdownAST, 'image', (node) => {
const aimg = new attribute_image_1.default(node);
if (aimg.attributes.length) {
attributeImages.root.push(aimg);
}
});
unist_util_visit_1.default(markdownAST, 'html', (node) => {
if (figure_1.default.test(node)) {
attributeImages.figure.push(new figure_1.default(node));
return true;
}
if (wrapped_1.default.test(node)) {
attributeImages.wrapped.push(new wrapped_1.default(node));
return true;
}
});
return Promise.all([
new Promise(resolve => resolve(attributeImages.figure.map(attributeImage => attributeImage.mdastNode))),
new Promise(resolve => resolve(attributeImages.root.map(attributeImage => attributeImage.mdastNode))),
new Promise(resolve => resolve(attributeImages.wrapped.map(attributeImage => attributeImage.mdastNode)))
]);
};