import streamlit as st from markdown import markdown from bs4 import BeautifulSoup import streamlit.components.v1 as components # Set page config st.set_page_config( page_title="Markdown Converter", layout="wide", initial_sidebar_state="expanded", page_icon="📝" ) def md_to_text(md_text): html = markdown(md_text) soup = BeautifulSoup(html, features='html.parser') return soup.get_text() def copy_button(text_to_copy, button_id): # Creating a safer version of the text for JavaScript escaped_text = text_to_copy.replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') html_code = f""" """ components.html(html_code, height=50) st.title("Markdown to Plain Text Converter") st.write("Paste your Markdown content below and convert it to plain text!") # Using columns for better layout col1, col2 = st.columns(2) with col1: md_input = st.text_area( "Markdown Input", height=300, placeholder="Paste your Markdown here..." ) # Convert button if st.button("Convert"): converted_text = md_to_text(md_input) st.session_state.converted_text = converted_text # Display results with col2: if 'converted_text' in st.session_state: st.text_area( "", value=st.session_state.converted_text, height=300, key="output_area" ) # Add copy button if st.session_state.converted_text: copy_button(st.session_state.converted_text, "output") # Preview section with proper copy functionality with st.expander("Preview Original Markdown"): if md_input: st.markdown(md_input, unsafe_allow_html=True) # Convert to plain text for the copy button preview_plain_text = md_to_text(md_input) copy_button(preview_plain_text, "preview") else: st.write("No Markdown to preview") # How to run instructions st.sidebar.markdown(""" **How to use:** 1. Paste your Markdown in the left panel 2. Click 'Convert' 3. Use the 'Copy' button in the right panel 4. Toggle the preview to see original formatting 5. The preview's copy button copies the plain text version """) # Apply light theme using CSS st.markdown(""" """, unsafe_allow_html=True)