Copy Markdown html without styles

How could I copy the HTML out of Markdown texts without the styles – only plain HTML tags?

Context: I write texts in markdown to give them a html structure and see it in DT – then I copy/paste the html to a non-markdown editor i.e. in a online editor.

Is this possible?

Does converting the MD to HTML in DT help? You can then see the HTML source and copy only what you need.

The CSS is included via link elements or inside style elements.

1 Like

@chrillek Yes, that’s it! Thanks very much!

You could also use a simple applescript to get the html code from a record.

This should work with a single markdown record/document.

tell application id "DNtp"
	set cr to content record of front think window
	set src to source of cr
	
	set the clipboard to src
end tell

If you wanted to work with the text before pasting it into the online editor, you could easily place the text into a text editor (eg textedit) or even a new, plain text record in DT. There are several things you could do, depending upon how you wanted to approach it.

	create record with {name:"newTemp", type:text, plain text:src}

I’m using DT2 but everything should work similarly with DT3.

2 Likes

@mockman This works excellent in DT3. Thanks a lot for this one!

For my use I copy it in BBedit and make everything in one single line (only the body tags) with search/replace. This is because the code editor in my e-learning tool adds paragraphs to every line.

Thanks again,
Carsten

Glad it works for you.

In all likelihood, you could do that bbedit stage within the script as well. This will remove all linefeeds and then add one back before the opening and closing body tags and then copy to the clipboard.

tell application id "DNtp"
	set cr to content record of front think window
	set src to source of cr
	
	set src to my split(src, linefeed, {}) -- remove all linefeeds
	set src to my split(src, "<body>", linefeed & "<body>") -- insert linefeed above <body>
	set src to my split(src, "</body>", linefeed & "</body>") -- insert linefeed above </body>
	
	set the clipboard to src
	
	-- optional
	create record with {name:"newTemp", type:text, plain text:src}
end tell

-- handler to find and replace in text
on split(rawSource, delim1, delim2)
	set AppleScript's text item delimiters to delim1
	set ti to text items of rawSource
	set AppleScript's text item delimiters to delim2
	set rawSource to ti as text
end split

I assumed above that you meant that you only wanted linefeeds above the <body> and </body> tags. If that’s not the case, let me know. You could also delete everything outside of the body tags, like this below. I added the code to put the result into a new bbedit window in case you wish to make other edits there but it is optional.

tell application id "DNtp"
	set cr to content record of front think window
	set src to source of cr
	
	set AppleScript's text item delimiters to {"<body", "</body>"} -- note that opening tag does not include `>`	
	set src to item 2 of (text items of src) as text -- section between body tags but including any parameters 
	set AppleScript's text item delimiters to ">" -- closing character of tags
	set src to (rest of text items of src) as text -- section after closing character of <body> tag
	
	set src to my split(src, linefeed, {}) -- remove all linefeeds
	set the clipboard to src
	
	-- optional
	create record with {name:"newTemp", type:text, plain text:src}
end tell

-- optional
tell application "BBEdit"
	activate
	make new window with properties {contents:src}
end tell

-- handler to find and replace in text
on split(rawSource, delim1, delim2)
	set AppleScript's text item delimiters to delim1
	set ti to text items of rawSource
	set AppleScript's text item delimiters to delim2
	set rawSource to ti as text
end split

Note that I added a bit regarding the <body> tag as it may have its own parameters, e.g. <body class="wholepage">.

1 Like

@mockman Now I finally could test it. This works perfect! Really great! That makes it really, really fast and easy.
Thank you so much!
Carsten