Skip to content

Instantly share code, notes, and snippets.

@Its-Just-Nans
Last active November 22, 2021 16:05
Show Gist options
  • Save Its-Just-Nans/aaab58eff030a0c726676405176c374e to your computer and use it in GitHub Desktop.
Save Its-Just-Nans/aaab58eff030a0c726676405176c374e to your computer and use it in GitHub Desktop.
ShowDownJS problem with &
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/showdown/dist/showdown.min.js"></script>
</head>
<body>
<div id="converted"></div>
<pre id="toConvert" style="display:none">
Here some text with & taht is converted
```c
#include <stdio.h>
int main(){
int number = 7;
int *pointerTOnumber = &number;
}
```
</pre>
<script>
let converter = new showdown.Converter();
let html = converter.makeHtml(document.getElementById('toConvert').innerHTML);
document.getElementById('converted').innerHTML = html;
</script>
</body>
</html>
@Its-Just-Nans
Copy link
Author

The result of this program is :

#include <stdio.h>

int main(){
    int number = 7;
    int *pointerTOnumber = &amp;number;
}

@aote777
Copy link

aote777 commented Nov 22, 2021

我也遇到这种情况了,请问你解决这个问题了吗?
I also encountered this situation. Have you solved this problem?

@Its-Just-Nans
Copy link
Author

It's because of the pre tag wich transforms the & symbol to an &amp;

To counter this, just use showdown in a traditionnal tag like <div> !

@aote777
Copy link

aote777 commented Nov 22, 2021

It's because of the pre tag wich transforms the & symbol to an &amp;

To counter this, just use showdown in a traditionnal tag like <div> !

那官网里那个textarea里的是可以正常显示的,他们是手动转换了吗?
The textarea on the official website can be displayed normally. Did they switch manually?

@aote777
Copy link

aote777 commented Nov 22, 2021

https://github.com/aote777/anki-md-template/blob/master/demo.html
这是我写的demo 可以直接运行。
This is the demo I wrote, which can be run directly.

@Its-Just-Nans
Copy link
Author

It's because of the pre tag wich transforms the & symbol to an &amp;
To counter this, just use showdown in a traditionnal tag like <div> !

The textarea on the official website can be displayed normally. Did they switch manually

Yes you can use <textarea>, it's only the <pre> wich isn't working

@aote777
Copy link

aote777 commented Nov 22, 2021

Thank you for your answer. But I got the innerHTML value directly, and the character entity was also escaped。
var text = textarea.innerHTML
I don't understand

@aote777
Copy link

aote777 commented Nov 22, 2021

I've found a solution. Thank you very much 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment