Can Images in a Local Markdown File Be Converted into Internal Items During Import?

Is it possible to import a local Markdown file containing images and convert the images into internal items at the same time, just like when clipping from the web or inserting locally?

A markdown file never contains images, only links to them.
One can write a script that tries to (!) import the images from wherever they are stored and fix the MD references when the MD is imported.
That will work only if the images are accessible at import time and if the references to them can be resolved.

Actually it’s possible using data: URLs, converting to Markdown or clipping as Markdown might use this depending on the preferences.

2 Likes

Correct. But since these files are huge and difficult to work with, containing all the image data as ASCII, they’re probably rare.
And if they user data URLs, there’s no need to convert anything on import.

1 Like

That exactly happens when I export to html and then convert to md. And the linked img become

![](data:image/png;base64,iVBORw0

what I don’t understand.

But in such case the link is

![](%E4%BC%9A%E8%AE%AE%E8%AE%B0%E5%BD%95/CleanShot

if the image folder is not totally english name.

That’s an embedded image. It’s part of the MD file, not external. @cgrunenberg referred to that.

URLs may contain only ASCII characters. So, umlauts, CJK etc must be URL-encoded to be usable in an URL.

May I ask why you’re wanting to embed the image data? Are you intending to share the Markdown document but don’t want the assets to be separate?

I can give a typical scenario. I recently organized some notes generated in Bear and imported them into the inbox of DevonThink. After exporting them as Markdown files, the images are placed in a folder with the same name. When I moved the md file to another database, I had to simultaneously move the image folder and maintain the folder’s dependency relationship, which was very troublesome. When I imported from other sources such as Craft or md files sent to me by others, I also encountered similar problems. It becomes even more complicated if the image folder is named differently.

Documents with embedded images always works better if one uses a document format intended to hold text & images. Word DOC, DOCX, Pages, etc. all designed to do that. Markdown not designed or intended to hold imbedded images.

1 Like

Doc, pages, pdf, and other similar formats were designed before the invention of the iPhone and iPad, they are not well-suited to handle varying monitor sizes ranging from 6 inches to 32 inches. Furthermore, markdown and textbundle are the only two formats that provide an easy way to create links between different items. However, it is worth noting that TextBundle is currently not supported for editing on Devon.

I don’t understand how this is troublesome. The Markdown document and its assets are placed in a group, nicely self-contained. So why are you not moving the entire group?

And note, this is not anything new or a behavior we designed. Structurally, web pages have been designed this way for decades.

2 Likes

That’s neither here nor there – they were designed for print, not for screen. So, the age of these apps doesn’t matter here – their forte is output on fixed-size media.

If you want your images to be included in your document(s), you have to settle on a format. MD can do that, but it’s not nice to work with in such files.

There is no silver bullet (and we’ve been over that several times, as a search in the forum will tell you).

1 Like

I just received a form in email from my bank in an attachment in DOCX format with both text in the letter an their logo as an image. My iPhone displays it quite well as a full page screen. Even can zoom in and out with a two finger swipe. So I would say, for me, DOCX suits the iPhone display pretty good. IMHO.

A form, in DOCX, from your BANK!!!
That stuff really winds me up.
The rascal in me would delete random words from the non-form part of the doc and add extra info in other places and send it back to see if they’re even paying attention.
The person who’s been in the room with a lawyer or two would say that you should probably send it back asking for a proper form or something like that.

I know there’s no perfect way to handle secure documents and forms and all methods have a hole in them somewhere but that’s miles away from choosing the exact wrong way to do it which is with anything to do with an editable word processor format.

1 Like

the form was empty of any personal details. i will print to paper, sign, and deliver to bank.

chill.

I understand that it’s customary, but when migrating MD files, the need to consider their folder structure can be disruptive and unnecessarily complex. In the past, I’ve dealt with this by first uploading to an image hosting service and then importing locally, but it was still cumbersome and unstable. I hope to achieve a seamless process, focusing only on the problem itself without the need to repeatedly consider the underlying operations. By the way, do you have plan to support for editing textbundle files?

I’m not sure how moving a group containing a document and its assets is disruptive or complex. It’s practically no different nor harder than moving an RTFD file.

Textbundles have not been widely adopted, so I don’t know if editing them will be supported. If so, it’s unlikely to happen before the next major version.

1 Like

Seems like a tempest because the OP insists on doing things one way, then is disturbed by the outcome. (A scenario that’s the root of a vast number of postings here.)

Bear offers 10 export types. For simplicity, just export the Bear files as RTF. If they have images, then Bear will make RTFDs. DEVONthink has lived well with RTF/RTFDs since creation.

3 Likes

Actually I write a automator script with help of gpt-4

on run {input, parameters}
	
	tell application "Finder"
		set targetFolder to POSIX file "/Users/kongxi/Yoink/" as alias
		set filePaths to every file in targetFolder
		
		--set filePaths to {}
		repeat with filePath in filePaths
			--	set end of filePaths to POSIX file (targetFolder & fileName)
			
			--set folderPath to my getFileFolderPOSIXPath(filePath)
			
			--set folderPath to my convertFileAliasToDirectoryAlias(filePath)
			
			set fileName to name of filePath
			set folderName to my removeExtensionFromFileName(fileName)
			set folderFullName to "/Users/kongxi/Yoink/" & folderName
			
			tell application "System Events"
				if exists disk item folderFullName then
					set theFolder to POSIX file folderFullName as alias
					
					set markdownFile1 to filePath as alias
					set imagesFolder1 to theFolder as alias
					set theFiles1 to every file of imagesFolder1
					
					my sendtodv(markdownFile1, imagesFolder1, theFiles1)
					tell application "Finder"
						move file markdownFile1 to trash
						move folder imagesFolder1 to trash
					end tell
				else
					set markdownFile1 to filePath as alias
					my sendmdtodv(markdownFile1)
					tell application "Finder"
						move file markdownFile1 to trash
					end tell
				end if
			end tell
			
			--log markdownFile1
		end repeat
		--log fileNames
	end tell
	
end run

on sendtodv(markdownFile, imagesFolder, theFiles)
	tell application id "DNtp"
		set theGroup to get record at "/Asset"
		
		set mdContent to read markdownFile as «class utf8»
		-- set mdContent to mdContentOrigin as «class utf8»
		
		repeat with theFile in theFiles
			
			
			--set theRecord to import (theFile as string) to theGroup
			tell application "System Events"
				set filePath3 to POSIX path of theFile
			end tell
			set theRecord to import filePath3 to theGroup
			
			
			set devonthinkLink to reference URL of theRecord as «class utf8» -- 确认有正确输出
			
			-- 以下代码块需要根据你的Markdown文件格式进行调整
			--set originalLink to POSIX path of (theFile as string) as «class utf8»
			tell application "System Events"
				set filePath3 to POSIX path of theFile
			end tell
			set originalLink to POSIX path of filePath3 as «class utf8»
			
			set resultPath to my extractLastFolderAndFile(originalLink) as «class utf8»
			set replacePath to my encodeURL(resultPath) as «class utf8»
			
			set mdContent to my replaceText(mdContent, replacePath, devonthinkLink) -- as «class utf8»
		end repeat
		
		-- set mdContent to ""
		set eof of markdownFile to 0
		write mdContent to markdownFile as «class utf8»
		--log mdContent
		
		-- 将更新后的Markdown文件导入到DEVONthink的inbox
		import (markdownFile as string) to incoming group
	end tell
end sendtodv

on sendmdtodv(markdownFile)
	tell application id "DNtp"
		set theGroup to get record at "/Asset"
		import (markdownFile as string) to incoming group
	end tell
end sendmdtodv


on replaceText(originalText, searchText, replacementText)
	set AppleScript's text item delimiters to searchText
	set textItems to text items of originalText
	set AppleScript's text item delimiters to replacementText
	set newText to textItems as string
	set AppleScript's text item delimiters to {""} -- Reset delimiters to default
	return newText
end replaceText

-- 解码URL的函数
on decodeURL(encodedURL)
	set decodedText to do shell script "python3 -c 'import sys, urllib.parse as ul; print(ul.unquote(\"" & encodedURL & "\"))'"
	return decodedText
end decodeURL

on extractLastFolderAndFile(fullPath)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"} -- 保存旧的文本项分隔符,并设置新的分隔符为"/"
	
	set pathComponents to text items of fullPath -- 分割路径
	set lastFolder to item -2 of pathComponents -- 获取倒数第二项,即最后一个文件夹的名称
	set fileName to last item of pathComponents -- 获取最后一项,即文件名
	
	set newPath to lastFolder & "/" & fileName -- 组合新路径
	set AppleScript's text item delimiters to oldDelims -- 恢复原来的文本项分隔符
	
	return newPath -- 返回结果
end extractLastFolderAndFile

on encodeURL(unencodedURL)
	set encodedText to do shell script "python3 -c 'import sys, urllib.parse as ul; print(ul.quote(\"" & unencodedURL & "\"))'"
	return encodedText
end encodeURL

-- 子函数:去除文件名的扩展名
on removeExtensionFromFileName(fullFileName)
	set AppleScript's text item delimiters to "."
	set fileNameList to text items of fullFileName
	set AppleScript's text item delimiters to ""
	
	-- 如果文件名包含多个点,我们需要重新组合除了最后一部分以外的所有文本项
	if (count of fileNameList) > 1 then
		set fileNameWithoutExtension to items 1 thru -2 of fileNameList as string
		-- 连接除了最后一项的所有项
		set lastItem to last item of fileNameList
		if lastItem is not "" then
			set fileNameWithoutExtension to fileNameWithoutExtension & ""
		end if
	else
		set fileNameWithoutExtension to fullFileName
	end if
	
	return fileNameWithoutExtension
end removeExtensionFromFileName

And it’ll monitor a specific folder to import the md to inbox and images to inbox/asset, while all the attachment links are replaced with devonthink link.

1 Like