Page 1 of 1
Support for Markdown
Posted: 2010-12-09 06:10:13
by Greiggy
I'm using Markdown increasingly on certain jobs which require speed and simple formatting of headings, lists, quotes etc. Markdown enables fast text entry without leaving the keyboard. Is there a way I could combine Markdown with NWP to get formatted output?
OK, I can do it using styles and still rely on this a lot -- it's just that Markdown is that much faster.
Re: Support for Markdown
Posted: 2010-12-09 11:17:12
by martin
Here's a macro will convert a good chunk of markdown in the front document to proper formatting:
Code: Select all
# This macro converts various inline plain "markdown" to real formatting.
# It uses the standard Nisus Writer style set. If your styles have different
# names, edit the highlighted style names in the macro as needed.
$doc = Document.active
# Convert lines prefixed by octothorpes to heading
$level = 1
$tag = Cast to String '#'
$prefix = ''
While $level < 7
$prefix &= $tag
$style = $doc.styleWithName("Heading $level")
# find all headings for this level
Replace All "^$prefix +(.*)", '\1', 'E'
$style.apply
$level += 1
End
# Lines starting with angle brackets should be block quotes
Replace All '^>\s*(.+)', '\1', 'E'
Menu 'Paragraph Style:Block Quote'
# unordered and numbered lists
Replace All '^[*\\-+] (.*)', '\1', 'E'
Menu 'Lists:Bullet List'
Replace All '^\d+\. (.*)', '\1', 'E'
Menu 'Lists:Number List'
# italics and strong
Replace All '_(.+?)_', '\1', 'E'
Menu 'Character Style:Emphatic'
Replace All '\*(\S.*?)\*', '\1', 'E'
Menu 'Character Style:Strong'
It relies upon your document style names, and assumes the standard set exists (ie: Heading 1, Heading 2 ... Block Quote, Emphatic, Strong). If your document lacks these styles the macro will give you an error.
If there's some aspect of markdown you use that the macro doesn't cover, please let me know. You might also see
this thread, in which Kino has provided a variety of macros that help with markdown.
Re: Support for Markdown
Posted: 2011-06-19 01:46:24
by iljajj
Hi, the macro doesn't seem to work flawlessly in NWP 2; the Markdown text"
Let’s see whether **Markdown** works on this thing, *shall we*?
Is rendered into:
Let’s see whether *Markdown* works on this thing, shall we?
It appears that everything inbetween asterisks is boldized. Using single asterisks for bold and underscores for italics appears to work, but this isn't playing by the rules entirely. Also, it would mean doing a massive search and replace on my markdown files. Would it be possible to adapt the macro?
Re: Support for Markdown
Posted: 2011-06-24 13:14:30
by martin
Here's an updated Markdown macro that handles a variety of things better, including the emphatic/bold markings. The new macro creates a copy of the current document before applying formatting.
Re: Support for Markdown
Posted: 2011-06-27 23:20:20
by iljajj
This seems to work perfectly. Thanks for uploading it!
Re: Support for Markdown
Posted: 2015-05-11 18:56:21
by skyblue
This is wonderful, thank you.
I'm trying to extend this macro to add support for page breaks– if I want to use ***** in Markdown and convert it to a page break in Nisus, how could I go about that?
I don't think that this is standard Markdown usage, but I need something to easily create page breaks in print...
I know little about regular expressions, I got as far as renaming the comment block and changing the repetition of *s to 5...
Code: Select all
# page breaks
If Replace All '^([*\-]\s?){5,}$', ' ', 'EU'
$lineParaStyle = ' '
$style = GetTargetDocumentStyle( $lineParaStyle, 'paragraphStyle' )
$style.apply
End
Thanks!
Re: Support for Markdown
Posted: 2015-05-11 20:29:10
by phspaelti
skyblue wrote:This is wonderful, thank you.
I'm trying to extend this macro to add support for page breaks– if I want to use ***** in Markdown and convert it to a page break in Nisus, how could I go about that?
I don't think that this is standard Markdown usage, but I need something to easily create page breaks in print...
I know little about regular expressions, I got as far as renaming the comment block and changing the repetition of *s to 5...
Code: Select all
# page breaks
If Replace All '^([*\-]\s?){5,}$', ' ', 'EU'
$lineParaStyle = ' '
$style = GetTargetDocumentStyle( $lineParaStyle, 'paragraphStyle' )
$style.apply
End
No, that would not be good. The bit of macro you started to modify actually replaces all types of patterns containing repeated sequences of stars and hyphens, even allowing spaces, into horizontal lines. At current a sequence of 5 stars would also be replaced by a horizontal line.
Also I assume you just want a page break and don't need any special formatting applied to the page break. In that case you could just write:
Code: Select all
# page breaks
Replace All '^\*{5}$', '\f', 'E'
However this line will have to be placed in the macro
before the line that does the horizontal lines, or you will never get to see its effect.
Re: Support for Markdown
Posted: 2022-01-18 11:10:32
by martin
NOTE: A new version of this macro has been posted as
Markdown Preview in the macro repository. That macro includes some tweaks to be more permissible with Markdown variants, e.g. MultiMarkdown syntax.
Re: Support for Markdown
Posted: 2022-01-19 19:06:10
by anupam
This is awesome! Seriously. I did not know (or even considered) that using Markdown to write the draft and then use a macro to convert to styled-text is an option. Now that I do, my word-processing just gained some superpowers.
Just goes to prove why I love Nisus. This should be an A-list feature listed on the front-page.