gatsby-remark-relative-images
Version:
Convert image src(s) in markdown to be relative to their node's parent directory. This will help gatsby-remark-images match images outside the node folder. For example, use with NetlifyCMS.
43 lines (42 loc) • 1.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.onCreateNode = void 0;
const path_1 = __importDefault(require("path"));
const lodash_1 = require("lodash");
const traverse_1 = __importDefault(require("traverse"));
const _1 = require(".");
const utils_1 = require("./utils");
const onCreateNode = ({ node, getNodesByType }, pluginOptions) => {
const options = (0, lodash_1.defaults)(pluginOptions, _1.defaultPluginOptions);
if (node.fileAbsolutePath && node.internal.type === `MarkdownRemark` || node.internal.type === `Mdx`) {
const files = getNodesByType(`File`);
const directory = path_1.default.dirname(node.fileAbsolutePath);
// Deeply iterate through frontmatter data for absolute paths
(0, traverse_1.default)(node.frontmatter).forEach(function (value) {
if (!(0, lodash_1.isString)(value))
return;
if (!path_1.default.isAbsolute(value) || !path_1.default.extname(value))
return;
const paths = this.path.reduce((acc, current) => {
acc.push(acc.length > 0 ? [acc, current].join('.') : current);
return acc;
}, []);
let shouldTransform = options.include.length < 1;
if (options.include.some((a) => paths.includes(a))) {
shouldTransform = true;
}
if (options.exclude.some((a) => paths.includes(a))) {
shouldTransform = false;
}
if (!shouldTransform)
return;
const file = (0, _1.findMatchingFile)(value, files, options);
const newValue = (0, utils_1.slash)(path_1.default.relative(directory, file.absolutePath));
this.update(newValue);
});
}
};
exports.onCreateNode = onCreateNode;