import { AppLayout } from "components/AppLayout"; import { Button } from "components/Inputs/Button"; import { Markdawn } from "components/Markdown/Markdawn"; import { ContentPanel, ContentPanelWidthSizes, } from "components/Panels/ContentPanel"; import { Popup } from "components/Popup"; import { ToolTip } from "components/ToolTip"; import { AppStaticProps, getAppStaticProps } from "graphql/getAppStaticProps"; import { Immutable } from "helpers/types"; import { GetStaticPropsContext } from "next"; import { useCallback, useState } from "react"; import TurndownService from "turndown"; import { Icon } from "components/Ico"; import { TOC } from "components/Markdown/TOC"; interface Props extends AppStaticProps {} export default function Editor(props: Immutable): JSX.Element { const handleInput = useCallback((text: string) => { setMarkdown(text); }, []); const [markdown, setMarkdown] = useState(""); const [converterOpened, setConverterOpened] = useState(false); function wrap( wrapper: string, properties?: Record, addInnerNewLines?: boolean ) { transformationWrapper((value, selectionStart, selectionEnd) => { let prepend = wrapper; let append = wrapper; if (properties) { prepend = `<${wrapper}${Object.entries(properties).map( ([propertyName, propertyValue]) => ` ${propertyName}="${propertyValue}"` )}>`; append = ``; } if (addInnerNewLines) { prepend = `${prepend}\n`; append = `\n${append}`; } let newValue = ""; newValue += value.slice(0, selectionStart); newValue += prepend; newValue += value.slice(selectionStart, selectionEnd); newValue += append; newValue += value.slice(selectionEnd); return { prependLength: prepend.length, transformedValue: newValue }; }); } function toggleWrap( wrapper: string, properties?: Record, addInnerNewLines?: boolean ) { const textarea = document.querySelector( "#editorTextArea" ) as HTMLTextAreaElement; const { value, selectionStart, selectionEnd } = textarea; if ( value.slice(selectionStart - wrapper.length, selectionStart) === wrapper && value.slice(selectionEnd, selectionEnd + wrapper.length) === wrapper ) { unwrap(wrapper); } else { wrap(wrapper, properties, addInnerNewLines); } } function unwrap(wrapper: string) { transformationWrapper((value, selectionStart, selectionEnd) => { let newValue = ""; newValue += value.slice(0, selectionStart - wrapper.length); newValue += value.slice(selectionStart, selectionEnd); newValue += value.slice(wrapper.length + selectionEnd); return { prependLength: -wrapper.length, transformedValue: newValue }; }); } function preline(prepend: string) { transformationWrapper((value, selectionStart) => { const lastNewLine = value.slice(0, selectionStart).lastIndexOf("\n") + 1; let newValue = ""; newValue += value.slice(0, lastNewLine); newValue += prepend; newValue += value.slice(lastNewLine); return { prependLength: prepend.length, transformedValue: newValue }; }); } function insert(prepend: string) { transformationWrapper((value, selectionStart) => { let newValue = ""; newValue += value.slice(0, selectionStart); newValue += prepend; newValue += value.slice(selectionStart); return { prependLength: prepend.length, transformedValue: newValue }; }); } function appendDoc(append: string) { transformationWrapper((value) => { const newValue = value + append; return { prependLength: 0, transformedValue: newValue }; }); } function transformationWrapper( transformation: ( value: string, selectionStart: number, selectedEnd: number ) => { prependLength: number; transformedValue: string } ) { const textarea = document.querySelector( "#editorTextArea" ) as HTMLTextAreaElement; const { value, selectionStart, selectionEnd } = textarea; const { prependLength, transformedValue } = transformation( value, selectionStart, selectionEnd ); textarea.value = transformedValue; handleInput(textarea.value); textarea.focus(); textarea.selectionStart = selectionStart + prependLength; textarea.selectionEnd = selectionEnd + prependLength; } const contentPanel = (

Convert HTML to markdown

Copy and paste any HTML content (content from web pages) here.{" "}
The text will immediatly be converted to valid Markdown.
You can then copy the converted text and paste it anywhere you want in the editor