turndown
turndown copied to clipboard
Unexpected result of <a> elem wrapping <h1> elem
Input:
<a href="http://www.baidu.com"><h1>asd</h1></a>
Output:
[
# asd
](http://www.baidu.com)
I think it should be:
# [asd](http://www.baidu.com)
Input:
<table>
<tbody>
<tr>
<td><p>a1</p></td>
<td><p>b1</p></td>
</tr>
<tr>
<td><p>a2</p></td>
<td><p>b2</p></td>
</tr>
</tbody>
</table>
Output:
|
a1
|
b1
|
|
a2
|
b2
|
I think it should be:
| a1 | b1 |
| a2 | b2 |
case 1: your input html is wrong, should be in the shape of
<h1><a href="">title</a></h1>.
Notice a tag is inside h1 tag, not the other way around
case 2: you need to enable gfm option to convert table
const md = require('to-markdown')
const tb = `
<table>
<tbody>
<tr>
<td><p>a1</p></td>
<td><p>b1</p></td>
</tr>
<tr>
<td><p>a2</p></td>
<td><p>b2</p></td>
</tr>
</tbody>
</table>
`
md(tb, { gfm: true })
// '| \n\na1\n\n | \n\nb1\n\n |\n| \n\na2\n\n | \n\nb2\n\n |'
your input html is wrong, should be in the shape of
@academyofzhuang actually no. Anchor will behave like block level element if contain block level elements. And, for me at last, expected behavior same as topic caster have described.
@SilentImp , yes, you are right. I was wrong. Thank you for explaining it.
One of the difficulties is that it's not possible to create <a><h1></h1></a> from markdown:
# [asd](http://www.baidu.com)
converts to:
<h1><a href="http://www.baidu.com">asd</a></h1>
which is not:
<a href="http://www.baidu.com"><h1>asd</h1></a>
To implement this would require the library to consider many other cases where an <a> wraps block elements. For example, how should the library convert?:
<a href="#">
<h1>Hello world</h1>
<p>Foo bar</p>
<p>Baz</p>
</a>
The final rendering effects may be more important than the roundtrip.
<h1><a href="http://www.baidu.com">asd</a></h1> gives the same effects as <a href="http://www.baidu.com"><h1>asd</h1></a> if you don't view the source