Created
August 3, 2026 22:49
-
-
Save rahimnathwani/210b1f9cb6ce731a30432227772d6f85 to your computer and use it in GitHub Desktop.
md2clip and clip2md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| md2clip() { | |
| local tmpfile=$(mktemp /tmp/md2clip_XXXXXX.html) | |
| pandoc -f markdown -t html "$1" \ | |
| | sed -E \ | |
| -e 's|<li><p( [^>]*)?>|<li>|g; s|</p></li>|</li>|g' \ | |
| -e 's|<(ul\|ol)>|<\1 style="margin:0.25em 0 0.75em 0;">|g' \ | |
| -e 's|<li>|<li style="margin:0 0 0.15em 0;">|g' \ | |
| -e 's|<p>|<p style="margin:0;">|g' \ | |
| > "$tmpfile" | |
| osascript -l JavaScript -e ' | |
| ObjC.import("AppKit"); | |
| var html = $.NSString.alloc.initWithDataEncoding( | |
| $.NSFileManager.defaultManager.contentsAtPath("'"$tmpfile"'"), | |
| $.NSUTF8StringEncoding | |
| ).js; | |
| var pb = $.NSPasteboard.generalPasteboard; | |
| pb.clearContents; | |
| pb.setStringForType(html, "public.html"); | |
| ' | |
| rm "$tmpfile" | |
| } | |
| clip2md() { | |
| osascript -l JavaScript -e ' | |
| ObjC.import("AppKit"); | |
| var pb = $.NSPasteboard.generalPasteboard; | |
| var html = pb.stringForType("public.html"); | |
| if (html.isNil()) { | |
| html = pb.stringForType("public.utf8-plain-text"); | |
| } | |
| if (html.isNil()) { | |
| $.NSFileHandle.fileHandleWithStandardError.writeData( | |
| $.NSString.alloc.initWithUTF8String("clip2md: clipboard is empty\n").dataUsingEncoding($.NSUTF8StringEncoding) | |
| ); | |
| $.exit(1); | |
| } | |
| html.js; | |
| ' | pandoc -f html -t gfm-raw_html | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment