-
Notifications
You must be signed in to change notification settings - Fork 894
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
<br> inside <code> is not turned into \n correctly #355
Comments
The default code span rule is supposed to be as less opinionated as possible, while producing valid Markdown. Please see CommonMark spec 6.3. Let's walk through the assumptions first:
Strictly speaking, it is not. It is turned into
Update: I first wrote the answer for code spans, but the key things are the same for code blocks. Depends on your use case:
So interpreting eventual HTML within code is definitely something that should be left to users' custom rules. Your rule can always choose to output an embedded HTML Now specifically for
Two more remarks:
Does this answer your question? |
@martincizek Thanks a lot for your very detailed answer. It helps a lot. 👍 |
[fix] Code Link Wrap converting (mixmark-io/turndown#355)
Here's what I wound up doing to handle cases like this--I'd be curious to hear if there's a better, more robust approach. For starters, I've found that multiline const cheerio = require("cheerio"); // ^1.0.0-rc.12
const TurndownService = require("turndown"); // ^7.1.2
const html = `<code>
xxxx
<br>
yyyy
</code>`;
const $ = cheerio.load(html);
// wrap with `<pre>` if necessary
$("code").replaceWith((_, e) =>
`<pre><code>${$(e).html().trim()}</code></pre>`
);
// if <br> in your source code is on one line, e.g.
// `<code>xxxx<br>yyyy</code>`, you can use:
//$("pre code br").replaceWith("\n");
const turndownService = new TurndownService({
codeBlockStyle: "fenced"
});
console.log(($.html()));
console.log("_".repeat(40));
console.log(turndownService.turndown($.html())); |
If the Only a subset of what can be expressed in HTML can be converted to Markdown. If you are outside of what can be expressed in Markdown, you need to write some opinionated preprocessor like you did when you wrapped the code in the |
Sample Html:
However in result markdown,
<br>
is just dismissed and not turned into\n
.I checked
rules.fencedCodeBlock
, I have a question about commonmark-rules.js#L114. Incontent
param,<br>
is turned into\n
. But in L114, we usenode.firstChild.textContent
instead ofcontent
. Any specific reason for this logic? Is it safe to usecode = content
.Thanks
The text was updated successfully, but these errors were encountered: