Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
  <h1>Convert Plain Text to HTML</h1>
  <form id="converter">
    <fieldset>
      <label for="plain-text">Enter plain text</label>
      <textarea id="input" name="plain-text" rows="6" onkeyup='convert(this.value);'></textarea>
    </fieldset>
    <section>
      <span>Converted HTML</span>
      <div id="output"></div>
    </section>
  </form>
</main>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

main {
  display: grid;
  row-gap: 1rem;
  place-items: center;
}

form {
  display: grid;
  gap: 1rem;
  justify-content: center;
  
  @media (width >= 700px) {
    grid-template-columns: 1fr 1fr;
  }
  
  label {
    display: block;
  }
  
  textarea, div {
    aspect-ratio: 2/1;
    border: 1px solid #000;
    overflow-y: scroll;
    max-block-size: 60vh;
    width: 80vw;
    
    @media (width >= 700px) {
      aspect-ratio: 1;
      width: 40vw;
    }
  }
  
  fieldset {
    border: none;
    padding: 0;
  }
  
  textarea {
    resize: none;
  }
  
  pre {
    white-space: pre-wrap;
  }
  
  img {
    max-inline-size: 100%;
  }
}
              
            
!

JS

              
                const output = document.getElementById("output");

// Source: https://www.w3docs.com/snippets/javascript/how-to-html-encode-a-string.html
function html_encode(input) {
  const textArea = document.createElement("textarea");
  textArea.innerText = input;
  return textArea.innerHTML.split("<br>").join("\n");
}

function convert_text_to_HTML(txt) {
  let out = "";
  const txt_array = txt.split("\n");
  const txt_array_length = txt_array.length;
  let non_blank_line_count = 0;

  for (let i = 0; i < txt_array_length; i++) {
    const line = txt_array[i];
    if (line === "") {
      continue;
    }
    non_blank_line_count++;
    if (non_blank_line_count === 1) {
      out += `<h1>${line}</h1>`;
    } else {
      out += `<p>${line}</p>`;
    }
  }
  return out;
}

function convert_images_and_links_to_HTML(string) {
  let urls_unique = [];
  let images_unique = [];
  const urls = string.match(/https*:\/\/[^\s<),]+[^\s<),.]/gim) ?? [];
  const imgs = string.match(/[^"'>\s]+\.(jpg|jpeg|gif|png|webp)/gim) ?? [];

  const urls_length = urls.length;
  const images_length = imgs.length;

  for (let i = 0; i < urls_length; i++) {
    const url = urls[i];
    if (!urls_unique.includes(url)) {
      urls_unique.push(url);
    }
  }

  for (let i = 0; i < images_length; i++) {
    const img = imgs[i];
    if (!images_unique.includes(img)) {
      images_unique.push(img);
    }
  }

  const urls_unique_length = urls_unique.length;
  const images_unique_length = images_unique.length;

  for (let i = 0; i < urls_unique_length; i++) {
    const url = urls_unique[i];
    if (images_unique_length === 0 || !images_unique.includes(url)) {
      const a_tag = `<a href="${url}" target="_blank">${url}</a>`;
      string = string.replace(url, a_tag);
    }
  }

  for (let i = 0; i < images_unique_length; i++) {
    const img = images_unique[i];
    const img_tag = `<img src="${img}" alt="">`;
    const img_link = `<a href="${img}" target="_blank">${img_tag}</a>`;
    string = string.replace(img, img_link);
  }
  return string;
}

function convert(input_string) {
  output.innerHTML = convert_images_and_links_to_HTML(
    convert_text_to_HTML(html_encode(input_string))
  );
}

              
            
!
999px

Console