JSFiddle - React, Tailwind, and code Playground

by Salmin Skenderovic

JavaScript

var rows = window.getSelection().toString().split("\n");


var addDashes = true;
var instructionNr = 0;

var formattedText = rows.filter(row => row !== "").map(row => {

  if (row.toLowerCase().includes("ingredient")) {
    return '## ' + row + ' ##\n';
  }


  if (row.toLowerCase().includes("instruction") || row.toLowerCase().includes("direction")) {
    addDashes = false;
    return '\n## ' + row + ' ##\n';
  }

  if (addDashes) {
    return '- ' + row;
  } else {
    instructionNr++;
    return instructionNr + '. ' + row + '\n';
  }

}).join("\n");


var text = document.createElement("textarea");
text.value = formattedText;
text.setAttribute("contenteditable", true);

document.body.appendChild(text);

text.select();
text.setSelectionRange(0, 99999);

document.execCommand("copy");