Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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!")
|