Skip to content

Commit

Permalink
fix html not included in pre code block
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Jun 13, 2021
1 parent ecad610 commit d5f4e55
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 11 deletions.
12 changes: 3 additions & 9 deletions commonmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/JohannesKaufmann/html-to-markdown/escape"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)

var multipleSpacesR = regexp.MustCompile(` +`)
Expand Down Expand Up @@ -268,12 +267,7 @@ var commonmark = []Rule{
{
Filter: []string{"code"},
Replacement: func(_ string, selec *goquery.Selection, opt *Options) *string {
code, err := selec.Html()
if err != nil {
return nil
}
// We don't want the html encoded characters to be displayed as is.
code = html.UnescapeString(code)
code := getHTML(selec)

// Newlines in the text aren't great, since this is inline code and not a code block.
// Newlines will be stripped anyway in the browser, but it won't be recognized as code
Expand Down Expand Up @@ -309,9 +303,9 @@ var commonmark = []Rule{
language := codeElement.AttrOr("class", "")
language = strings.Replace(language, "language-", "", 1)

code := codeElement.Text()
code := getHTML(codeElement)
if codeElement.Length() == 0 {
code = selec.Text()
code = getHTML(selec)
}

fenceChar, _ := utf8.DecodeRuneInString(opt.Fence)
Expand Down
1 change: 1 addition & 0 deletions from.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func NewConverter(domain string, enableCommonmark bool, options *Options) *Conve

conv.before = append(conv.before, func(selec *goquery.Selection) {
selec.Find("a[href]").Each(func(i int, s *goquery.Selection) {
// TODO: don't hardcode "data-index" and rename it to avoid accidental conflicts
s.SetAttr("data-index", strconv.Itoa(i+1))
})
})
Expand Down
12 changes: 12 additions & 0 deletions testdata/TestCommonmark/pre_code/goldmark.golden
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ totally ~~~~~~ normal
~ code

</code></pre>
<pre><code>
The &lt;img&gt; tag is used to embed an image.

The &lt;img/&gt; tag is used to embed an image.

</code></pre>
<pre><code>
&lt;a href=&quot;#Blabla&quot; data-index=&quot;1&quot;&gt;
&lt;img src=&quot;http://bla.bla/img/img.svg&quot; style=&quot;height:auto&quot; width=&quot;200px&quot;/&gt;
&lt;/a&gt;

</code></pre>
17 changes: 16 additions & 1 deletion testdata/TestCommonmark/pre_code/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@
Some ~~~
totally ~~~~~~ normal
~ code
</code></pre>
</code></pre>


<!--code tag with pre and other tags inside-->
<pre><code>
The &lt;img&gt; tag is used to embed an image.

The <img> tag is used to embed an image.
</code></pre>

<!--TODO: make sure that "data-index" does not get added to the link-->
<pre><code>
<a href="#Blabla">
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
</a>
</code></pre>
16 changes: 16 additions & 0 deletions testdata/TestCommonmark/pre_code/output.fenced_backtick.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ Some ~~~
totally ~~~~~~ normal
~ code

```

```

The <img> tag is used to embed an image.

The <img/> tag is used to embed an image.

```

```

<a href="#Blabla" data-index="1">
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
</a>

```
18 changes: 17 additions & 1 deletion testdata/TestCommonmark/pre_code/output.fenced_tilde.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ Some ~~~
totally ~~~~~~ normal
~ code

~~~~~~~
~~~~~~~

~~~

The <img> tag is used to embed an image.

The <img/> tag is used to embed an image.

~~~

~~~

<a href="#Blabla" data-index="1">
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
</a>

~~~
16 changes: 16 additions & 0 deletions testdata/TestCommonmark/pre_code/output.indented.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ Some ~~~
totally ~~~~~~ normal
~ code

```

```

The <img> tag is used to embed an image.

The <img/> tag is used to embed an image.

```

```

<a href="#Blabla" data-index="1">
<img src="http://bla.bla/img/img.svg" style="height:auto" width="200px"/>
</a>

```
12 changes: 12 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,15 @@ func findMax(a []int) (max int) {
}
return max
}

// getHTML gets the HTML content and unescapes the encoded characters.
// Returns "" if there is an error.
func getHTML(selec *goquery.Selection) string {
content, err := selec.Html()
if err != nil {
return ""
}

// We don't want the html encoded characters to be displayed as is.
return html.UnescapeString(content)
}

0 comments on commit d5f4e55

Please sign in to comment.