UNPKG

gatsby-remark-widows

Version:
32 lines (25 loc) 890 B
"use strict"; var visit = require("unist-util-visit"); module.exports = function (_ref) { var markdownAST = _ref.markdownAST; var pluginOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var options = pluginOptions; options.minLength = options.minLength || 4; options.maxLength = options.maxLength; visit(markdownAST, "text", function (node) { var processedText = node.value; var words = processedText.split(' '); var skip = false; if (options.maxLength && words.length > options.maxLength) { skip = true; } if (options.minLength && words.length < options.minLength) { skip = true; } // Fix possible widows if text is more than 4 words if (!skip) { processedText = processedText.replace(/\s+([\S]*)(\s*)$/gm, "\xA0$1$2"); } node.value = processedText; }); return markdownAST; };