Akshayram1 commited on
Commit
810b0ce
·
verified ·
1 Parent(s): cfdc918

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -28
app.py CHANGED
@@ -12,34 +12,36 @@ def md_to_text(md_text):
12
  return soup.get_text()
13
 
14
  def copy_button(text_to_copy, button_id):
15
- # More reliable HTML/JS implementation of copy button
16
- components.html(
17
- f"""
18
- <script>
19
- function copyText{button_id}() {{
20
- const text = `{text_to_copy.replace('`', '\\`').replace("'", "\\'")}`;
21
- navigator.clipboard.writeText(text).then(function() {{
22
- console.log('Text copied to clipboard');
23
- document.getElementById('copy-button-{button_id}').innerHTML = 'Copied!';
24
- setTimeout(function() {{
25
- document.getElementById('copy-button-{button_id}').innerHTML = 'Copy';
26
- }}, 2000);
27
- }})
28
- .catch(function(err) {{
29
- console.error('Failed to copy text: ', err);
30
- }});
31
- }}
32
- </script>
33
- <button id="copy-button-{button_id}" onclick="copyText{button_id}()"
34
- style="background-color: #4CAF50; color: white; border: none;
35
- padding: 5px 10px; text-align: center; text-decoration: none;
36
- display: inline-block; font-size: 14px; margin: 4px 2px;
37
- cursor: pointer; border-radius: 4px;">
38
- Copy
39
- </button>
40
- """,
41
- height=50,
42
- )
 
 
43
 
44
  st.title("Markdown to Plain Text Converter")
45
  st.write("Paste your Markdown content below and convert it to plain text!")
 
12
  return soup.get_text()
13
 
14
  def copy_button(text_to_copy, button_id):
15
+ # Creating a safer version of the text for JavaScript
16
+ # This avoids the backslash issue in f-strings
17
+ escaped_text = text_to_copy.replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r')
18
+
19
+ html_code = """
20
+ <script>
21
+ function copyText""" + button_id + """() {
22
+ const text = \"""" + escaped_text + """\";
23
+ navigator.clipboard.writeText(text).then(function() {
24
+ console.log('Text copied to clipboard');
25
+ document.getElementById('copy-button-""" + button_id + """').innerHTML = 'Copied!';
26
+ setTimeout(function() {
27
+ document.getElementById('copy-button-""" + button_id + """').innerHTML = 'Copy';
28
+ }, 2000);
29
+ })
30
+ .catch(function(err) {
31
+ console.error('Failed to copy text: ', err);
32
+ });
33
+ }
34
+ </script>
35
+ <button id="copy-button-""" + button_id + """" onclick="copyText""" + button_id + """()"
36
+ style="background-color: #4CAF50; color: white; border: none;
37
+ padding: 5px 10px; text-align: center; text-decoration: none;
38
+ display: inline-block; font-size: 14px; margin: 4px 2px;
39
+ cursor: pointer; border-radius: 4px;">
40
+ Copy
41
+ </button>
42
+ """
43
+
44
+ components.html(html_code, height=50)
45
 
46
  st.title("Markdown to Plain Text Converter")
47
  st.write("Paste your Markdown content below and convert it to plain text!")