To Markdown macro

Get help using and writing Nisus Writer Pro macros.
tomoyuki-kosaka
Posts: 41
Joined: 2009-02-17 17:44:12

To Markdown macro

Post by tomoyuki-kosaka »

Previously, I asked here about a macro for converting NWP file to Markdown format.
Just briefly, I made a not sophisticated one.
It is based on "Copy as HTML v3.3" macro by @loulesko.
And, @philip also helped me.

This macro worked for me as expect but I'm not sure about other files and situations.
It can handle, table, image, URL embedded file.
If someone use and find usefulness or improve points for this, it's very happy to me.
If you have some comment, please send it to me.
Anyway, I post this code.
Attachments
To Markdown.nwm
(36.08 KiB) Downloaded 2024 times
Groucho
Posts: 497
Joined: 2007-03-03 09:55:06
Location: Europe

Re: To Markdown macro

Post by Groucho »

Thank you very much, Tomoyuki!

Henry.
Axbax
Posts: 22
Joined: 2017-02-04 11:55:59

Re: To Markdown macro

Post by Axbax »

Hi,
I have been trying to make this work, but it only changes Heading 1 to # and removes style from ordered lists. The rest of the document is still styled. Am I doing something wrong (style names are default as required in the macro) or is there an update to this macro that I could try?
Thanks for your help!
Ken
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: To Markdown macro

Post by phspaelti »

Axbax wrote:Hi,
I have been trying to make this work, but it only changes Heading 1 to # and removes style from ordered lists. The rest of the document is still styled. Am I doing something wrong (style names are default as required in the macro) or is there an update to this macro that I could try?
It seems that Tomoyuki left some kind of unusual language formatting on parts of the macro. Since the macro uses styled find and replace in his macro, those statements won't work for most people.

Here is a version with extraneous formatting removed:
Attachments
To Markdown.nwm
(23.96 KiB) Downloaded 1948 times
philip
Axbax
Posts: 22
Joined: 2017-02-04 11:55:59

Re: To Markdown macro

Post by Axbax »

Excellent, thanks!
tomoyuki-kosaka
Posts: 41
Joined: 2009-02-17 17:44:12

Re: To Markdown macro

Post by tomoyuki-kosaka »

Sorry for the trouble, Axbax. I did not check this form a while.
And, thank you phspaelti! I appreciate your help!

Honestly, I'm not heavy user of this macro...
I'll try to use and improve this.
Axbax
Posts: 22
Joined: 2017-02-04 11:55:59

Re: To Markdown macro

Post by Axbax »

Hi, it's been a while since I used this and now that I needed it, it doesn't seem to work anymore. Any thoughts? Any updates?
Thanks in advance!
Ken
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: To Markdown macro

Post by martin »

What about the second macro (as posted by philip) doesn't work for you Ken? I just tried a quick test with a bit of formatting, headings, and links and the Markdown macro worked just fine.
tomoyuki-kosaka
Posts: 41
Joined: 2009-02-17 17:44:12

Re: To Markdown macro

Post by tomoyuki-kosaka »

Probably, the macro should include "Paragraph styles" for exchange the "Headings".
I also remake macro as third one.
Please try this.
Attachments
To Markdown.nwm
(39.92 KiB) Downloaded 1775 times
Jtummins
Posts: 8
Joined: 2015-01-04 08:53:55

Re: To Markdown macro

Post by Jtummins »

I downloaded your "To Markdown" macro, and everything seems to work except for the Blockquote. The default style in NWP is "Block Quote" instead of "Blockquote". I don't know if that name change makes any difference in function.

The line that makes the translation from RTF Block Quote to Markdown block quote seems to be:

Code: Select all

Find and Replace '(?:^.+$)', '> \0', 'EuW'
For whatever reason, it does not see the Block Quote style.

Do you (or anyone) have a recommendation or solution?
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: To Markdown macro

Post by martin »

Jtummins wrote: 2020-09-03 08:14:46 I downloaded your "To Markdown" macro, and everything seems to work except for the Blockquote. The default style in NWP is "Block Quote" instead of "Blockquote". I don't know if that name change makes any difference in function.
That's actually not the problem. Despite the macro comments talking about the "Blockquote" style, the macro actually searches for a style called "Block Quote", which is the default style name.

The problem is that the formatting applied to the Find and Replace command that searches for the Block Quote style accidentally also includes some language formatting. Because of that the macro will only find text using the Block Quote style if it also matches the custom language.

The solution is to remove the custom language formatting from that Find and Replace command. I'm attaching an updated version of tomoyuki-kosaka's macro that includes this fix.
Attachments
To Markdown.nwm
(39.33 KiB) Downloaded 998 times
Nick Sloan
Posts: 22
Joined: 2004-08-01 02:35:18

Re: To Markdown macro

Post by Nick Sloan »

Thank you for this very useful macro tomoyuki and martin.

How difficult would it be to adapt it to work on selected text only, and to leave the selection active on completion?
I have made some attempt to do this myself, but I am hitting various brick walls.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: To Markdown macro

Post by martin »

It is possible to adapt the markdown macro to only convert content in the selection.

To do the conversion inline in the current document is awkward. It's easy enough to change a single conversion operation to operate only in the selection (e.g. add pound signs to headings). The sticky issue is that at each point in the macro you need to be able to reselect the same content, but accounting for any changes in text length. It's definitely possible to do this bookkeeping, but it's tedious to code. You have to keep track of the original TextSelection and continually adjust its length to account for characters that were added or removed by each single operation.

The quicker solution is to simply extract the selected content to a new document, convert that to markdown, and then insert it back into the original document. I'm attaching a To Markdown in Selection macro that does exactly that. I hope it helps!
Attachments
To Markdown in Selection.nwm.zip
(6.43 KiB) Downloaded 450 times
Nick Sloan
Posts: 22
Joined: 2004-08-01 02:35:18

Re: To Markdown macro

Post by Nick Sloan »

Thank you martin, that's fantastic. It does exactly what I need, including leaving the converted text selected.

Thanks too for the explanation about the difficulty of working with selections. Makes perfect sense.
All the best,
Nick
Nick Sloan
Posts: 22
Joined: 2004-08-01 02:35:18

Re: To Markdown macro

Post by Nick Sloan »

Hi Martin, it seems that your macro is leaving spaces between the leading asterisk/underscore and the words converted into italic and bold which defeats the MD styling. (I have only just noticed this because I have spent most of the day compiling it into a much longer KM macro and solving other problems.) Also, is there any reason why I could not substitute single asterisks for underscores for the italic styling?
Many thanks again, Nick
Post Reply