UNPKG

remark-typopo

Version:

remark plugin to apply microtypography fixes using Typopo

43 lines 1.42 kB
// src/index.ts import typopo from "typopo"; import { visit } from "unist-util-visit"; var { fixTypos } = typopo; var DEFAULT_SETTINGS = { locale: "en-us", allowInCodeBlocks: false, allowInInlineCode: false }; function isVerbatimNode(node) { if (!node || typeof node !== "object") return false; const type = node.type; return type === "code" || type === "inlineCode" || type === "math" || type === "inlineMath"; } var remarkTypopo = (options = {}) => { const settings = { ...DEFAULT_SETTINGS, ...options }; const skip = (type) => type === "code" && !settings.allowInCodeBlocks || type === "inlineCode" && !settings.allowInInlineCode || type === "math" || type === "inlineMath"; return (tree) => { visit(tree, "text", (node, _i, parent) => { if (!node.value?.trim() || parent && skip(parent.type)) return; node.value = applyTypopo(node.value, settings.locale); }); visit(tree, isVerbatimNode, (node) => { if (skip(node.type)) return; node.value = applyTypopo(node.value, settings.locale); }); }; }; var applyTypopo = (text, locale) => { const [ , // Skip full match (index 0) leading = "", content = "", trailing = "" ] = text.match(/^(\s*)(.*?)(\s*)$/s) ?? []; return leading + fixTypos(content, locale, {}) + trailing; }; var index_default = remarkTypopo; export { index_default as default }; //# sourceMappingURL=index.js.map