Advertisement
petrogradphilosopher

curi.us reply/quote js + markdown link handling

Apr 9th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // initializeComment blanks out the comment title and the comment author, sets the comment
  2. // body to the given text, focuses the comment body, resizes the comment height to show
  3. // all of the given text, and puts the cursor at the end of the comment body.
  4. function initializeComment(text) {
  5.   document.getElementById("comment_jebra").value = ""; // Title.
  6.   document.getElementById("comment_comment_writer").value = ""; // Author.
  7.   commentBody = document.getElementById("comment_argle"); // Body.
  8.   window.location.href = "#post-comment"
  9.   commentBody.focus();
  10.   commentBody.value = text;
  11.   autogrow()
  12. }
  13.  
  14. function reply(commentNum) {
  15.   initializeComment("#" + commentNum + " ");
  16. }
  17.  
  18. function quote(commentNum) {
  19.   reply("")
  20.   var body = "";
  21.   // Find the outer element for the comment to be quoted and get its child of class
  22.   // "comment-body".  (There should be only one.) The innerText of this is what we want to
  23.   // quote.
  24.   var comment = document.getElementById(commentNum);
  25.   for (var i = 0; i < comment.childNodes.length; i++) {
  26.     if (comment.childNodes[i].className == "comment-body") {
  27.       text = innerTextWithMarkdownLinks(comment.childNodes[i]).replace(/^(?=.)/mg, "> ");
  28.       // Append a final pair of newlines if necessary.
  29.       if (text.slice(-2) != "\n\n") {
  30.         text = text + "\n\n";
  31.       }
  32.       initializeComment(text);
  33.       break;
  34.     }
  35.   }
  36. }
  37.  
  38. // innerTextWithMarkdownLinks returns a version of the innerText of elem
  39. // in which all links ("a" tags) – except for links of one of the types
  40. // below – have been replaced with markdown-style links. The types of
  41. // links which are are left as plain text are:
  42. // - auto-links to comment numbers (i.e., links whose innerText conists
  43. //   of "#" followed by one or more digits)
  44. // - raw http/https links (i.e., links whose innerText matches the
  45. //   regexp "^https?://"
  46. function innerTextWithMarkdownLinks(elem) {
  47.   var orig = elem.innerHTML;
  48.   var links = elem.getElementsByTagName("a");
  49.   for (var i = 0; i < links.length; i++) {
  50.     // Skip #XXXXX links.
  51.     if (links[i].innerText.match(/^#[0-9]+$/)) {
  52.       continue;
  53.     }
  54.     // Skip raw http/https links.
  55.     if (links[i].innerText.match(/^https?:[/][/]/)) {
  56.       continue;
  57.     }
  58.     // Temporarily change link to markdown-style link.
  59.     links[i].innerHTML = "[" + links[i].innerText + "](" + links[i].href + ")"
  60.    }
  61.   text = elem.innerText;
  62.   elem.innerHTML = orig; // Restore the original innerHTML.
  63.   return text;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement