Work on Markdown files in DTP...

I have been trying to figure out a workflow for markdown within DTP. A few of the complaints I have seen have been related to how ugly the preview of the file is.

I previously posted a script to allow you to change the style sheets for markdown documents to something less fugly than the built-in CSS.

The script didn’t scratch the itch completely so I have now written a couple of scripts which allow me to work fully within DTP and use Marked for export (Print to PDF in DTP with markdown sucks).

This script allows you to keep your style sheets in the database and link them into the markdown document. It should be obvious from the property names where things go:

property styleGroupName : "Stylesheets"
property favouriteStyle : "Firates"
property linkPosition : "bottom"

set cr to ASCII character 13
set lf to ASCII character 10

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to subject as text
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

tell application id "DNtp"
	repeat with styleGroup in records of current database
		if the name of styleGroup is styleGroupName then
			exit repeat
		end if
	end repeat
	set styleSheets to {}
	set uuids to {}
	repeat with styleSheet in children of styleGroup
		if the name of styleSheet ends with ".css" then
			set styleSheetName to my replaceText(".css", "", name of styleSheet)
			set styleSheets to styleSheets & styleSheetName
			set uuids to uuids & {styleSheetName & ":" & uuid of styleSheet}
		end if
	end repeat
	set favouriteStyle to choose from list styleSheets with prompt "Select your favourite style sheet:" default items favouriteStyle
	repeat with link in uuids
		if link contains favouriteStyle & ":" then
			exit repeat
		end if
	end repeat
	uuids
	set linkText to "<link rel=\"stylesheet\" href=\"x-devonthink-item://" & my replaceText((favouriteStyle & ":" as text), "", link as text) & "\">"
	set theSelection to the selection as list
	repeat with thisItem in theSelection
		if the kind of thisItem is "Markdown" then
			set thePath to the path of thisItem
			set theText to do shell script "perl -pe 's|<link rel=.stylesheet.*?>||' " & quoted form of thePath & " | sed -e :a -e '/[^[:blank:]]/,$!d; /^[[:space:]]*$/{ $d; N; ba' -e '}'"
			set theText to my replaceText(cr, lf, theText)
			if linkPosition is "bottom" then
				set theText to theText & lf & lf & linkText & lf
			else
				set theText to linkText & lf & lf & theText
			end if
			set the plain text of thisItem to theText
		end if
	end repeat
end tell

This second script changes the the x-devonthink-item:// links to file:// links and opens the file in Marked (or the default markdown processor) for easy exporting:

property removeStylesheet : "yes"

set cr to ASCII character 13
set lf to ASCII character 10

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to subject as text
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

tell application id "DNtp"
	set theSelection to the selection as list
	repeat with thisItem in theSelection
		if the kind of thisItem is "Markdown" then
			set thePath to the path of thisItem
			if removeStylesheet is "yes" then
				set theText to do shell script "perl -pe 's|<link rel=.stylesheet.*?>||' " & quoted form of thePath & " | sed -e :a -e '/[^[:blank:]]/,$!d; /^[[:space:]]*$/{ $d; N; ba' -e '}'"
			else
				set theText to do shell script "cat " & quoted form of thePath & " | sed -e :a -e '/[^[:blank:]]/,$!d; /^[[:space:]]*$/{ $d; N; ba' -e '}'"
			end if
			set theText to my replaceText(cr, lf, theText)
			set {otid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "x-devonthink-item"}
			set theTextItems to text items of theText
			set AppleScript's text item delimiters to otid
			set uuids to {}
			repeat with aChunk in theTextItems
				if aChunk starts with "://" then
					set {otid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
					set uuids to uuids & (words 1 through 5 of aChunk as text)
					set AppleScript's text item delimiters to otid
				end if
			end repeat
			repeat with aUuid in uuids
				set doc to get record with uuid aUuid
				set docPath to path of doc
				set theText to my replaceText(aUuid, docPath, theText)
			end repeat
			set theText to my replaceText("href=\"x-devonthink-item", "href=\"file", theText)
			set theText to my replaceText("x-devonthink-item://", "", theText)
			set tmpFileName to POSIX path of (path to temporary items from user domain) & name of thisItem
			try
				close access tmpFileName
			end try
			set tmpFile to open for access tmpFileName with write permission
			set eof of tmpFile to 0
			write theText as «class utf8» to tmpFile starting at eof
			close access tmpFile
			do shell script "open " & quoted form of tmpFileName
		end if
	end repeat
end tell

Have fun!

1 Like

Thanks.

PS, remember: Beauty is in the eye of the beholder.

I obviously don’t know when to stop…

This script will insert an image link from the database:

property imageGroupName : "Images"

set cr to ASCII character 13
set lf to ASCII character 10

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to subject as text
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

tell application id "DNtp"
	repeat with imageGroup in records of current database
		if the name of imageGroup is imageGroupName then
			exit repeat
		end if
	end repeat
	set images to {}
	set uuids to {}
	repeat with anImage in children of imageGroup
		set imageName to name of anImage
		set images to images & imageName
		set uuids to uuids & {imageName & ":" & uuid of anImage}
	end repeat
	imageGroup
	set selectedImage to choose from list images with prompt "Select an image:"
	set imageCaption to text returned of (display dialog "Please type the caption for " & selectedImage default answer "")
	repeat with link in uuids
		if link contains selectedImage & ":" then
			exit repeat
		end if
	end repeat
	set linkText to "![" & imageCaption & "](x-devonthink-item://" & my replaceText((selectedImage & ":" as text), "", link as text) & ")"
	do shell script "echo " & quoted form of linkText & " | pbcopy"
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
end tell

I should mention that I’m using the internal links to allow me to edit and preview consistently within DTTG.

No problem. Ideas are for exchange and (polite) discussion.

If what you’re doing scratches your itch, and someone may find a solution or inspiration in your approach, it’s all good.

I personally use internal links in my Markdown docs as well.