evilstreak / markdown-js

A Markdown parser for javascript
7.7k stars 863 forks source link

Disable wrapping everything is a `p` #156

Closed Petah closed 10 years ago

Petah commented 10 years ago

Is there anyway to disable wrapping everything in a p

jiananshi commented 10 years ago

do agree with this, it's annoying

funkytaco commented 10 years ago

Same here.... wrapping in a paragraph element makes this module unusable.

evilstreak commented 10 years ago

When you say wrapping everything in a <p>, do you mean the way that this …

The quick brown fox jumps
over the lazy dog.

Pack my box with five
dozen liquor jugs.

… gets turned into this …

<p>The quick brown fox jumps
over the lazy dog.</p>

<p>Pack my box with five
dozen liquor jugs.</p>

If so, that's just how Markdown works I'm afraid.

jiananshi commented 10 years ago

@evilstreak

If so, I suggest adding an option to get rid of it in the plugin. Usually it's annoying when I get data from my database and pass it to toHTML function, I have no idea to detect whether it contains markdown syntax. If it's not, then plain text with <p> tag surround will be sent to the backend and continue to frontend, bad things happens.

Currently I'm getting rid of it at the backend, remove the p tag after got it from database.

ashb commented 10 years ago

You can do this by not calling toHTML directly andinstead processing the parsed tree before turning it into HTML - I'll knock up a quick wiki page showing how to do that this evening.

jiananshi commented 10 years ago

@ashb That would be so nice, looking forward to it.

ashb commented 10 years ago

Here's a version.

tree = md.markdown.parse("The quick brown fox jumps\n*over* the lazy dog.\n\nPack my box with five\ndozen liquor jugs.\n")

This gives us a tree that looks like this

[ 'markdown',
  [ 'para',
    'The quick brown fox jumps\n',
    [ 'em', 'over' ],
    ' the lazy dog.' ],
  [ 'para',
    'Pack my box with five\ndozen liquor jugs.' ] ]

We can remove the top level paras and keep them separated like this:

tree.forEach( function(jsonml) { if (jsonml[0] == "para") { jsonml.splice(0,1) } } )

yeilding:

[ 'markdown',
  [ 'The quick brown fox jumps\n',
    [ 'em', 'over' ],
    ' the lazy dog.' ],
  [ 'Pack my box with five\ndozen liquor jugs.' ] ]

This should give you enough to go on I hope. If you have more precise requirements let us know!

beaugunderson commented 9 years ago

Rendering that ends up with the text in tags, so for example:

['markdown', ['Hello World']]

ends up as <Hello World>.

beaugunderson commented 9 years ago

Ah, I fixed this by using lodash's flatten operator and specifying true to the shallow argument to flatten the array by one level.

beaugunderson commented 9 years ago

Noting that lodash's flatten operator is shallow by default; the true argument makes it deep--not what you want here. :)