-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to render Markdown inside <div> blocks #488
Comments
Weird you're right! I guess marked skips certain elements. You could loop through the elements that have your class, grab the markdown, process the markdown, then replace the contents! Wouldn't be that difficult with jQuery and you wouldn't need to modify marked. |
I'm using a static site generator and don't want to run JS on the client side at render time. |
FYI, Markdown specifically skips processing Markdown within block-level HTML; please refer to http://daringfireball.net/projects/markdown/syntax#html |
but some times you want to have a nice styled panel and use in it some markdown like below
and it is not entirely true that markdown doesn't work in html block as the markdown emphasis work normally. Looking on above sample, if I would but "test" in between of asterisk "test" that would be rendered to bold. Also the code block above is rendered like it would be a one line code. So something works there. And that would be super cool to have it fully working. |
anyone know the line to hack in the js to make this work? |
Reliable Markdown parsing inside HTML is a must. But this will require to remove "indented block = code block" rule entirely, as nested HTML tags are always indented... As GFM style is so much better I think that removal will only improve everything. It was always a fail in original Markdown design to treat indented text as code. |
I love Markdown, but if things like support for markdown in between divs will not be available, markdown will start loosing his position in favour of https://github.com/asciidoctor/asciidoctor.js |
I know this is a bit of a specific case, but I have been trying to get round this for my (jekyll) github pages. My requirement was to wrap a markdown-ed paragraph in a div so I could style it as a panel. In the end I just created an empty, classed div in front of the paragraph and used a sibling selector I realise this is an unholy and specific workaround, but I thought I would share. |
This feels like quite a common use case - it would be nice if there was some built in liquid template that could do this for you, although we'll need to ensure that markdown is subsequently parsed and outputted correctly within that liquid block. |
Some markdown parsers/implementations allow this:
This would be rendered as
It would be great if marked would support this, too. |
just stumbled upon this div case Interestingly, it seems as the following indeed do render correctly:
However others don't do so in my case:
|
+1 Basically, there's a crazy regex in block.html that simply skips over block elements entirely, where a "block element" is anything, like <foo>, that isn't an inline element. I have this requirement too. I have some <div>s that I process in a special way -- mostly to add css dynamically. But I'd like the content to be processed like any other md. Proving some kind of flag on the div would work, or I don't mind writing something to help out the lexer if I knew what hooks are in the library. |
I don't know if I am doing it the right way, but here is how I got it work: const marked = require('marked');
const renderer = new marked.Renderer();
const cheerio = require('cheerio');
renderer.html = function(html) {
// marked has to be the first attribute
const $ = cheerio.load(html);
const markedDivs = $('div[marked]');
if (markedDivs.length > 0) {
// loop through all div tags with marked attribute
for (var i = markedDivs.length - 1; i > -1; i--) {
marked(markedDivs.eq(i).html(), function(err, html) {
markedDivs.eq(i).html(html);
});
}
html = $('body').html();
}
return html;
};
renderer.paragraph = function(text) {
let isHtml = /<[a-z][\s\S]*>/i.test(text);
if (isHtml) return text + '\n';
else return '<p>' + text + '</p>\n';
};
module.exports = renderer; |
i don't know if i understand this request correctly, but after having checked parsedown, i've found that this is already supported: i've activated markdown extra in grav and the markdown in a |
@aoloe Your test uses inline style, which was already established to work. Have you tried a heading or list with parsedown-extra? @joshbruce I'm confused about the issue consolidation. This remains a limitation in marked, right? Should further discussion happen here or in #985? |
+1 I'm missing this feature. I think that the CommonMark HTML Blocks specification woudl be quite easy to implement. A raw HTML block basically starts with an HTML tag and ends with an empty line. The following Markdown would be parsed as follows:
|
@r0the I find the whitespace handling in HTML blocks to be surprising. It prevents using newlines to organize the HTML content in longer blocks. You also can't indent naturally:
Not only must the markdown be left-aligned, but the closing tags get treated as a codeblock due to the indentation. (example) That said, it's probably easier to implement than the markdown=1 attribute. |
FWIW the implemented solution is to insert new lines around the content that you want to get parsed, as in this comment: #488 (comment) |
Put a blank line around the html elements <div class="hide">
# h1
</div> |
Hi,
First: thanks for writing this fantastic library!
My issue: I'm using Markdown in documentation for a project, and would like to use <div> tags inside of the Markdown to specify that certain sections should be hidden (or otherwise stylized).
For example:
Unfortunately, the Markdown content inside the <div> does not get rendered -- it's copied verbatim into the output.
Is there any way to configure marked.js to render Markdown inside of <div> tags? If not: can you point me in the right direction for how to add such an option? Or: can you recommend some alternate strategy for hiding (or toggling visibility of) sections of content?
Thanks!
The text was updated successfully, but these errors were encountered: