Skip to content

Instantly share code, notes, and snippets.

@llinfeng
Created December 7, 2019 14:37
Show Gist options
  • Save llinfeng/fcf833f9b801e6c9ac903ee53338a89a to your computer and use it in GitHub Desktop.
Save llinfeng/fcf833f9b801e6c9ac903ee53338a89a to your computer and use it in GitHub Desktop.
An Autohotkey snippet that converts raw Markdown texts into "pastable" rich-text, for Gmail and even MS Word.
::togo::
; Debugging tips
; 1. Check if pandoc is installed. If not, the best way is to install through choco.
; choco install page:https://chocolatey.org/packages/pandoc
; 2. Check if `paste.exe` is available to PowerShell, same thing for `clip.exe` ==> these two are necessary Windows utilities/executable that were called with Ctrl+V and Ctrl+C
Run, PowerShell "paste.exe | pandoc -f markdown-smart --from=gfm -t HTML | Set-Clipboard -AsHtml ; echo 'Conversion done.' "
; Caveat: single quotes are pasted wrongly, as question marks.
Return
@llinfeng
Copy link
Author

llinfeng commented Dec 7, 2019

With Markdown texts copied to the clipboard, typing togo and press <space>-key will trigger the script to convert those pure texts into Rich texts. This helps when I was sharing my Markdown notes in Emails or Google Forums.

@pakaka
Copy link

pakaka commented Nov 23, 2024

thank you for that snippet, i have updated the code so it doesn't require paste.exe

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

::togo::
    ; Debugging tips
    ; 1. Check if pandoc is installed. If not, the best way is to install through choco.
    ; choco install page:https://chocolatey.org/packages/pandoc
    ; 2. Check if `paste.exe` is available to PowerShell, same thing for `clip.exe` ==> these two are necessary Windows utilities/executable that were called with Ctrl+V and Ctrl+C
    Run, PowerShell -NoExit "Get-Clipboard | pandoc -f markdown-smart --from=gfm -t HTML | Set-Clipboard -AsHtml; echo 'Conversion done.'"
    ; remove -NoExit  , this parameter is added for debugging purposes
Return

@pakaka
Copy link

pakaka commented Nov 23, 2024

version for ahk 2.0

#Requires AutoHotkey v2.0

;type `togo + space bar` to execute  
::togo::
{
    ; Debugging tips
    ; 1. Check if pandoc is installed. If not, the best way is to install through choco.
    ; choco install page:https://chocolatey.org/packages/pandoc
    ; 2. Check if `paste.exe` is available to PowerShell, same thing for `clip.exe` ==> these two are necessary Windows utilities/executable that were called with Ctrl+V and Ctrl+C
    RunWait "PowerShell -Command Get-Clipboard | pandoc -f markdown-smart --from=gfm -t HTML | Set-Clipboard -AsHtml; echo 'Conversion done.'"
    Send "^v"
}

@pakaka
Copy link

pakaka commented Nov 23, 2024

version for ahk v2 with fixed clipboard processing encoding issues

::togo::
{
    ; Save current clipboard content
    ClipSave := ClipboardAll

    ; PowerShell command to process clipboard content with pandoc
    Command := "
    (
        $clipboard = Get-Clipboard -Raw -TextFormatType UnicodeText;
        if (!$clipboard) { $clipboard = Get-Clipboard -Raw };
        $converted = $clipboard | pandoc -f markdown-smart --from=gfm -t html;
        Set-Clipboard -Text $converted -AsHtml;
        Write-Output 'Conversion done.'
    )"
    
    ; Run PowerShell command
    RunWait "PowerShell -NoProfile -Command " . Command

    ; Paste the processed clipboard content
    Send "^v"

    ; Restore the original clipboard content
    Clipboard := ClipSave
    ClipSave := ""
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment