remarkjs / remark-rehype

plugin that turns markdown into HTML to support rehype
https://remark.js.org
MIT License
275 stars 18 forks source link

outer p tag optional #14

Closed sgpinkus closed 4 years ago

sgpinkus commented 4 years ago

Subject of the feature

Option to remove outer p tag.

Describe your issue here. rehype-remark will wrap HTML in a p tag. I want to not have strings wrapped in p tag optionally. I can't find an option for this.

var unified = require('unified');
var markdown = require('remark-parse');
var htmlStringify = require('rehype-stringify');
var remark2rehype = require('remark-rehype');

console.log(unified()
  .use(markdown)
  .use(remark2rehype)
  .use(htmlStringify)
  .processSync("hello").contents
);

Gives:

<p>hello</p>

Expected behaviour

Explained above.

Alternatives

...contents.replace(/^<p>/, '').replace(/<\/p>$/, '')
wooorm commented 4 years ago

Hi there @sgpinkus!

This is how Markdown works: you’ll get paragraphs, headings, and whatnot with Markdown when going to HTML.

If you’d rather “unwrap” paragraphs, you can write your own plugin (a rehype plugin is probably a good idea), that traverses the tree and replaces p elements with their children!

sgpinkus commented 4 years ago

Hey.

This is how Markdown works: you’ll get paragraphs, headings, and whatnot with Markdown when going to HTML.

Hard to see the point in it all otherwise. It's just the outer most p tag I didn't want. I pass text with absolutely no markdown markup I expected no HTML markup back. Supposing "hello" is valid HTML as much as <p>hello</p> is.

If you’d rather “unwrap” paragraphs, you can write your own plugin (a rehype plugin is probably a good idea)

Thanks for the suggestion. I will look into this.

wooorm commented 4 years ago

I understand. It’s just: the way it works, to have that outer <p> 🤷‍♂️ As in, you’ll have it with CommonMark, markdown-it, or here on GitHub (you can use browser dev tools to inspect):

hello

Yields:

hello