Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created August 23, 2020 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/a70c8f1280a253aa3847df7244e267f8 to your computer and use it in GitHub Desktop.
Save ahmadawais/a70c8f1280a253aa3847df7244e267f8 to your computer and use it in GitHub Desktop.
remark-widow
const visit = require('unist-util-visit')
module.exports = () => (tree, file) => {
visit(tree, 'heading', node => {
console.log('node: ', node);
visit(node, 'text', textNode => {
console.log('textNode: ', textNode);
const text = textNode.value ? textNode.value.trim() : ''
// const textNoWidows = text.replace(/\s+([\S]*)(\s*)$/gm, "\xA0$1$2");
// const textNoWidows = text.replace(/\s([^\s]*)(\s+)?$/, ' $1');
const textNoWidows = text.replace(/\s([^\s]*)(\s+)?$/, '\xA0$1');
console.log('textNoWidows: ', textNoWidows);
// const textNoWidows = text.replace(/\s([^\s]*)(\s+)?$/, ' $1');
textNode.value = textNoWidows
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment