4

I'm using markdown editor with Pagedown and MarkdownSharp.

There is an option "AutoNewlines" in MarkdownSharp.

I wonder how to do this in Pagedown (http://code.google.com/p/pagedown/).

Thank you!

Chance
  • 1,317
  • 2
  • 12
  • 18

2 Answers2

5

the first revision of this answer included a rather naive approach which didn't work in most cases.
i ported the AutoNewLines option from MarkDownSharp to PageDown you can download it here http://code.google.com/r/marcdrexel-pagedown/

it should behave exactly as in MarkDownSharp

usage sample:

var converter = Markdown.getSanitizingConverter();
converter.autoNewLine = true;
var editor = new Markdown.Editor(converter);
editor.run();
marc.d
  • 3,804
  • 5
  • 31
  • 46
  • Hi marc - if you get a minute, could you explain how you achieved this? I'm trying not to use a fork if I can avoid it: http://stackoverflow.com/questions/20027077/how-can-i-modify-pagedown-to-actually-render-newlines-like-markdownsharps-auton – SB2055 Nov 17 '13 at 04:10
  • 2
    @SB2055 its just a minimal change see https://code.google.com/r/marcdrexel-pagedown/source/diff?spec=svn89cf815be7922eae7f4e216d811a19e73b4a122a&r=89cf815be7922eae7f4e216d811a19e73b4a122a&format=side&path=/Markdown.Converter.js&old_path=/Markdown.Converter.js&old=3151a581819f39123b0b5fd1ca9e26cebdcaa873 – marc.d Nov 17 '13 at 09:15
2

This is now possible through the postSpanGamut hook provided by the Markdown Converter in Pagedown. This is the code I am using:

function nl2br(text) {

    // Replace new lines with <br/> tags to preserve formatting for users that are 
    // not used to markdown swallowing single line breaks.
    return text.replace(/\n/g, " <br>\n");
}

var converter = new Converter();
converter.hooks.chain("postSpanGamut", nl2br);

It is important to use the postSpanGamut hook rather than a more general preConversion hook as the postSpanGamut only runs on the contents of text blocks and there doesn't not mess up list formatting and other aspects of markdown.

More information in the docs.

MichaelJones
  • 1,336
  • 2
  • 12
  • 22
  • 1
    I doubt this works: "a\nb\nc\nd".replace(/[^(
    )]\n/g, "
    "); yields "
    b\n
    d" and I don't understand yet the voodoo why it does so.
    – Giszmo Aug 16 '14 at 04:57
  • ok, you dispose of anything that is before a newline and replace it by a "
    " unless the line already ends in a
    .
    – Giszmo Aug 16 '14 at 05:06
  • matching "not [some word]" is anything but easy with regex and so my solution for now is `text.replace(/([^(>)])\n/g, "$1
    \n");`
    – Giszmo Aug 16 '14 at 05:48
  • @Giszmo, thanks for bringing this up. You're right that the `[^]` is for not matching sets of characters rather than words. It worked in simple tests so I ran with it but I suspect it needs to be revisited. Python has `(?!...) syntax for not matching words but I suspect that is a Python extension. – MichaelJones Aug 16 '14 at 10:39
  • Actually, javascript has that too but it is for testing the words after matches not before them. We can match only `
    ` tags with `\n` afterwards, but not only `\n` with `
    ` before them.
    – MichaelJones Aug 16 '14 at 10:51
  • 1
    I've just updated the code to a simple `text.replace`. That seems to work and I've forgotten why I may have decided it didn't. If there is a failing example I'd be interested in explore further. – MichaelJones Aug 18 '14 at 16:16