Skip to content

Instantly share code, notes, and snippets.

@garcon
Created October 24, 2025 07:27
Show Gist options
  • Select an option

  • Save garcon/2658d8ca5b71f6e265c0a81f14ab62bf to your computer and use it in GitHub Desktop.

Select an option

Save garcon/2658d8ca5b71f6e265c0a81f14ab62bf to your computer and use it in GitHub Desktop.
Replace hyphen/minus (-) in text with sequence of soft hyphen and non-breaking hyphen to let hyphenated words repeat the hyphen at the beginning of a new line when wrapped..
javascript:!function(){let e=window.getSelection();if(!e.rangeCount)return;let t=e.getRangeAt(0),n=e.toString();if(!n)return;let r=e=>/^[\p{L}\p{N}]$/u.test(e),l=n.replace(/-/g,(e,t)=>{let l=function e(t,n){let l=0;for(let a=n-1;a>=0;a--){let o=t[a];if(/\s/.test(o)||"-"===o)break;r(o)&&l++}return l}(n,t),a=function e(t,n){let l=0;for(let a=n+1;a<t.length;a++){let o=t[a];if(/\s/.test(o)||"-"===o)break;r(o)&&l++}return l}(n,t);return l>1&&a>1?%22\u00ad\u2011%22:%22\u2011%22}),a=document.activeElement,o=a&&a.tagName&&a.tagName.toLowerCase();if(%22textarea%22===o||%22input%22===o&&%22number%22==typeof%20a.selectionStart){let%20s=a.selectionStart,i=a.selectionEnd;a.setRangeText(l,s,i,%22select%22)}else{let%20f=document.createTextNode(l);t.deleteContents(),t.insertNode(f),e.removeAllRanges();let%20g=document.createRange();g.setStartBefore(f),g.setEndAfter(f),e.addRange(g)}}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment