Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions commands/conversions/rich-text-clipboard-to-markdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# Install via homebrew: `brew install pandoc`

# @raycast.title Rich Text to Markdown
# @raycast.author Adam Zethraeus
# @raycast.authorURL https://github.com/adam-zethraeus
# @raycast.description Convert rich text clipboard data to GitHub Flavored Markdown using Pandoc
# @raycast.author Ali Rohde
# @raycast.authorURL https://github.com/alibrohde
# @raycast.description Convert rich text clipboard data (preserving hyperlinked text as [label](url)) to GitHub Flavored Markdown using Pandoc. Tries the HTML pasteboard flavor first, falls back to RTF.
#
# @raycast.icon 📝
#
Expand All @@ -20,4 +20,19 @@ if ! command -v pandoc &> /dev/null; then
fi

export LC_CTYPE=UTF-8

# Prefer HTML: most modern web sources (browsers, Gmail, Google Docs, Notion,
# Linear, etc.) place higher-fidelity HTML on the pasteboard than RTF, and it
# preserves hyperlinked text as real anchor tags that pandoc renders as
# [label](url) in markdown.
html=$(osascript -e 'try' -e 'the clipboard as «class HTML»' -e 'on error' -e 'return ""' -e 'end try' 2>/dev/null \
| perl -ne 'chomp; next unless s/^«data HTML//; s/»$//; print pack("H*", $_)')
Comment on lines +24 to +29
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned about that because if the editor works with both, and the person prefers to have Markdown instead of RTF, the editor will kind of force the user to make use of RTF. Right?

The case I am thinking of here is Google Docs, where we can have blocks of code and want to paste Markdown inside a code block. I am not sure if it will respect that.

Perhaps it is better to have a second Script Command to convert to RTF instead of doing two things with a single Script Command.

What do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this — I think there might be a small misread of what the script does (and that's on me for not making it clearer). Both branches produce markdown as the final output: pandoc … --to=gfm | pbcopy runs on either path. The HTML-vs-RTF choice only controls which input flavor on the pasteboard we read from before converting.

The reason HTML is preferred: when you copy from Gmail, Google Docs, Notion, Linear, etc., the pasteboard holds both flavors, but only the HTML version preserves hyperlinks as real <a href="…"> anchor tags. Pandoc turns those into [label](url) in markdown. The RTF flavor on the same sources often flattens links to plain text, so users get markdown with the link text but no URL. RTF stays as a fallback for sources that only expose rich text (some older native apps).

So for the Google Docs code-block case — pasting markdown into a ``` block — the output is markdown regardless of which branch fires. The user always gets markdown on the clipboard; no second script command needed.

Happy to update the @raycast.description to make the input-vs-output distinction more obvious if that would help. Let me know!


if [ -n "$html" ]; then
printf '%s' "$html" | pandoc --from=html --to=gfm | pbcopy
exit 0
fi

# Fall back to RTF for sources that only expose rich text (some native apps,
# older editors). This is the original behavior of the script.
osascript -e 'the clipboard as «class RTF »' | perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | pandoc --from=rtf --to=gfm | pbcopy