Can Pagedown turn “\n” into <br /> , just like MarkdownSharp's Autonewlines option?

StackOverflow https://stackoverflow.com/questions/11388451

سؤال

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!

هل كانت مفيدة؟

المحلول

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();

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top