Skip to content

Instantly share code, notes, and snippets.

@diogotito
Last active February 4, 2022 15:03
Show Gist options
  • Save diogotito/028003d49c84f09f5f232f8d76aa378d to your computer and use it in GitHub Desktop.
Save diogotito/028003d49c84f09f5f232f8d76aa378d to your computer and use it in GitHub Desktop.
A PowerShell Custom Tool to paste HTML fragments into Zim
$ofs = "`n"
$clipboard = Get-Clipboard -TextFormatType Html
$html_fragment = ("$clipboard" -split '<!--StartFragment-->|<!--EndFragment-->')[1]
$html_fragment | pandoc --from html --to zimwiki

This script uses Pandoc!
Make sure you have the pandoc.exe binary available in your %PATH%

Pasting formatted (HTML) text in Zim on Windows

Fluff I like Zim. It is the first note-taking application where I finally gained the habit of writing and managing my technical and personal notes in a more or less organized way. There are, however, a number of major flaws in Zim when I compare it with other fancier note-taking tools like Obsidian, OneNote, etc. The number one flaw, to me, is the inability to paste formatted text from other apps and preserve some of their formatting (bold, italic, hyperlinks, • Bullet lists, headings, code snippets, etc.).

Luckily, Pandoc supports Zim's peculiar wiki syntax as an output format and Windows' PowerShell has a Get-Clipboard cmdlet with the ability to retrieve the clipboard content in HTML format.

This is where this script comes in handy:

How to use

Download and extract the html-to-zim.ps1 file. Then, inside Zim:

  1. On the menu bar, click ToolsCustom Tools
  2. Click the [+] button on the right
  3. Fill in the "Edit Custom Tool" form like this:
Edit Custom Tool
Name: Paste from HTML
Description: Gets the clipboard contents as HTML and converts it to
Zim's syntax with Pandoc before inserting it in the note
Command: powershell C:\Path\To\html-to-zim.ps1
🔲 Command does not modify data
☑️ Output should replace current selection
☑️ Show in toolbar

On Linux

it's a bit easier. The command can be this one-liner:

sh -c "xclip -selection CLIPBOARD -target text/html -out | pandoc --from html --to zim"

No script needed! (although it's technically possible to make a PowerShell one-liner from the html-to-zim.ps1 script) Maybe go pick your favourite icon theme's internet-web-browser.svg file for flavour.

Extra: Assign this Custom Tool to a custom key binding

  1. Open the Preferences window (Edit → Preferences)
  2. Go to the Key bindings tab
  3. There is an action for the Custom Tool you just created. It's probably the 2nd or 3rd one in the list and the name looks like <Actions>/custom_tools/paste from html-usercreated
  4. Double click the Key Binding cell (it probably says "Disabled")
  5. Press Ctrl+Shift+V
  6. Click OK

Now test this new Custom Tool: Copy some formatted text from Word or the web, then jump back to Zim and try pasting it with either Tools → Paste from HTML or Ctrl+Shift+V.
The clipboard was converted to Zim's wiki syntax and inserted in the cursor's position!
Now press Ctrl+R for Zim to redisplay it as formatted text.

Happy Zimming!

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