Tinderbox Forum

Write in rich text, export to Markdown (with [[Wikilinks]] )

Tinderbox makes exporting to html easy. But if you want to write in rich text (not directly in Markdown)I’ve found it tough to export to Markdown that preserves most formatting, external web links, and internal links in the [[ ]] “wikilinks” format recognized by 1Writer, DEVONthink, Obsidian, and it seems a growing number of other apps.

Here is an AppleScript-wrapped solution that may be of interest. Similar can be done within Tinderbox via HTMLExportCommand though I’ve found the setup fussier.

Install Pandoc (at pandoc.org) if you don’t already have it, and at a minimum add the default HTML templates via File > Built-in Templates > HTML in your Tinderbox document.

There’s little other setup required within Tinderbox. But you need to prefix any note-to-note links with a character of your choice because Tinderbox exports the same format for internal links as for external ones. I chose @ for the prefix.

Text links entered as [[ Ziplinks should work if prefixed. Or you can enter the name of the other note, select that name and go to Note > Link to ... in the menu. External web links currently need to be entered manually via Note > Make Web link if you want them to export. (They can’t just be dragged in as suggestion the screenshot.)

Copy-paste the script below into Script Editor (at Applications > Utilities). Make sure you have a folder in Finder ready to receive the export and that it doesn’t contain files you don’t want to be overwritten. Then select notes in Tinderbox, click the ‘run’ button in Script Editor, and follow the prompt.

If all goes well the chosen folder will be filled with .md files with contents that look like the result on the right below:

The exported files can then be moved to a folder that 1Writer on the iPad is accessing, or that Obsidian uses as a vault, or that DEVONthink is indexing, or all of the above at the same time. The internal “wikilinks” should still work!

Script that exports multiple selected notes to a folder:
-- Have an export folder ready. Select Tinderbox notes and run. 
-- Assumes Pandoc is installed at /usr/local/bin/. See https://pandoc.org/installing.html
-- NB: overwrites any like-named .md files in that folder

set prefix to "@" -- character(s) used to distinguish internal links from external ones

set pandocCmd to "/usr/local/bin/pandoc -f html -t markdown_mmd" -- html to MultiMarkdown
set sedCmd to "sed -E 's/" & prefix & "(\\[.+\\]).+\\)/[\\1]/g;t'" -- grab anchor, surround by [[ ]]
set cmdStr to pandocCmd & " | " & sedCmd -- assemble the "pipe"

set theFolder to (choose folder with prompt "Choose a folder to receive the exported MD files")

tell front document of application "Tinderbox 8"
	repeat with aNote in selections
		tell aNote
			set theFilePath to POSIX path of theFolder & (value of attribute "Name") & ".md" -- name file after note
			set theHTML to evaluate with "exportedString(this,$HTMLExportTemplate)"
			set theMMD to do shell script "echo " & quoted form of theHTML & " | " & cmdStr
			do shell script "touch " & quoted form of theFilePath -- create file if doesn't exist
			do shell script "echo " & quoted form of theMMD & "> " & quoted form of theFilePath -- write to file
		end tell
	end repeat
end tell
Script that sends Markdown from one note to the clipboard:
set prefix to "@" -- character(s) used to distinguish internal links from external ones

tell front document of application "Tinderbox 8"
	tell selection 1
		--      get the HTML from Tinderbox
		set theHTML to evaluate with "exportedString(this,$HTMLExportTemplate)"
		--      instruct Pandoc to convert from HTML to (Multi)Markdown
		set pandocCmd to "/usr/local/bin/pandoc -f html -t markdown_mmd"
		--       AS-escaped regex to grab anchor text from prefixed Markdown links only and surround by [[ ]]
		set sedCmd to "sed -E 's/" & prefix & "(\\[.+\\]).+\\)/[\\1]/g;t'"
		--       assemble the "pipe" for the command line script
		set cmdStr to "echo " & quoted form of theHTML & " | " & pandocCmd & " | " & sedCmd
		--       run the script through the shell
		set theMMD to do shell script cmdStr
	end tell
end tell
set the clipboard to theMMD
return theMMD -- to view in Script Editor Result pane

These scripts can be placed in the Scripts menu for convenient access and to hide ugly code. Script Editor > Preferences > Show Script menu.

Thoughts welcome on how well this works on other computers. This has been quite a journey. Perhaps in time Markdown export from Tinderbox that preserves links will be as easy as html export!

Edit: shortened and improved multiple selected notes script

5 Likes

Here’s a similar solution to the above that doesn’t use AppleScript, though you still have to install Pandoc from pandoc.org/installing.html.

It’s faster than AppleScript (a possible consideration if frequently exporting many hundreds of notes at a time). But it requires more setup within each Tinderbox document. With the external AppleScript you can (usually) just select notes in any document that has at least the built-in HTML template assigned (File > Built-in-Templates) and run it without worrying about getting the settings right in multiple other attributes that you might not want to change anyway because sometimes you also need to export as html.

I got something like this to work:


HTMLExportFileNameSpacer needs a space (not visible in the screenshot) rather than the default _.

HTMLExportCommand is:

/usr/local/bin/pandoc -f html -t markdown_mmd | sed -E 's/@(\[.+\]).+\)/[\1]/g;t'

The regex that extracts the anchor text from internal links and surrounds them with [[ ]], thus turning them into “wikilinks” that many other apps now recognize, has a couple fewer \ to ‘escape’ characters than does the AppleScript.

Once all this is set up then to export (Multi)Markdown instead of HTML you go, naturally, to File > Export > As HTML!