turndown icon indicating copy to clipboard operation
turndown copied to clipboard

Unexpected result of <a> elem wrapping <h1> elem

Open ArcticLampyrid opened this issue 8 years ago • 5 comments
trafficstars

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 |

ArcticLampyrid avatar May 24 '17 04:05 ArcticLampyrid

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 |'

academyofzhuang avatar Aug 04 '17 03:08 academyofzhuang

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 avatar Mar 04 '18 21:03 SilentImp

@SilentImp , yes, you are right. I was wrong. Thank you for explaining it.

academyofzhuang avatar Mar 05 '18 01:03 academyofzhuang

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>

domchristie avatar Mar 06 '18 10:03 domchristie

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

ArcticLampyrid avatar Mar 09 '18 11:03 ArcticLampyrid