AO3: Comment Formatting and Preview

Adds buttons to insert HTML formatting, and shows a live preview box of what the comment will look like

  1. // ==UserScript==
  2. // @name AO3: Comment Formatting and Preview
  3. // @namespace https://greasyfork.org/en/users/906106-escctrl
  4. // @version 5.0
  5. // @description Adds buttons to insert HTML formatting, and shows a live preview box of what the comment will look like
  6. // @author escctrl
  7. // @license MIT
  8. // @match *://*.archiveofourown.org/tags/*/comments*
  9. // @match *://*.archiveofourown.org/tags/*/wrangle?*
  10. // @match *://*.archiveofourown.org/users/*/inbox*
  11. // @match *://*.archiveofourown.org/users/*/bookmarks*
  12. // @match *://*.archiveofourown.org/tags/*/bookmarks*
  13. // @match *://*.archiveofourown.org/works/*
  14. // @match *://*.archiveofourown.org/chapters/*
  15. // @match *://archiveofourown.org/collections/*/works/*
  16. // @match *://archiveofourown.org/collections/*/bookmarks*
  17. // @match *://*.archiveofourown.org/comments/*
  18. // @match *://*.archiveofourown.org/comments?*
  19. // @match *://*.archiveofourown.org/admin_posts/*
  20. // @exclude *://archiveofourown.org/works/search*
  21. // @grant none
  22. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js
  23. // @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js
  24. // @require https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js
  25. // @require https://update.greasyfork.org/scripts/491888/1355841/Light%20or%20Dark.js
  26. // ==/UserScript==
  27.  
  28. /* global jQuery, lightOrDark */
  29.  
  30. (function($) {
  31. 'use strict';
  32.  
  33. /*********************************************************
  34. GUI CONFIGURATION
  35. *********************************************************/
  36.  
  37. // load storage on page startup
  38. var standardmap = new Map(JSON.parse(localStorage.getItem('cmtfmtstandard'))); // only a key: true/false list
  39. var custommap = new Map(JSON.parse(localStorage.getItem('cmtfmtcustom'))); // all content we need from user to display & insert what they want
  40.  
  41. // if the background is dark, use the dark UI theme to match
  42. let dialogtheme = lightOrDark($('body').css('background-color')) == "dark" ? "ui-darkness" : "base";
  43.  
  44. // the config dialog container
  45. let cfg = document.createElement('div');
  46. cfg.id = 'cmtFmtDialog';
  47.  
  48. // adding the jQuery stylesheet to style the dialog, and fixing the interferance of AO3's styling
  49. $("head").append(`<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/${dialogtheme}/jquery-ui.css">`)
  50. .prepend(`<script src="https://use.fontawesome.com/ed555db3cc.js" />`)
  51. .append(`<style tyle="text/css">#${cfg.id}, .ui-dialog .ui-dialog-buttonpane button {font-size: revert; line-height: 1.286;}
  52. #${cfg.id} form {box-shadow: revert; cursor:auto;}
  53. #${cfg.id} #custombutton a {cursor:pointer;}
  54. #${cfg.id} legend {font-size: inherit; height: auto; width: auto; opacity: inherit;}
  55. #${cfg.id} fieldset {background: revert; box-shadow: revert;}
  56. #${cfg.id} input[type='text'] { position: relative; top: 1px; padding: .4em; width: 3em; }
  57. #${cfg.id} ul { padding-left: 2em; }
  58. #${cfg.id} ul li { list-style: circle; }
  59. #${cfg.id} #stdbutton label { font-family: FontAwesome, sans-serif; }
  60. #${cfg.id} #custombutton div button { width: 0.5em; }
  61. #${cfg.id} #custombutton div input:nth-of-type(1) { width: 2em; }
  62. #${cfg.id} #custombutton div input:nth-of-type(2) { width: 6em; }
  63. #${cfg.id} #custombutton div input:nth-of-type(3) { width: 10em; }
  64. #${cfg.id} #custombutton div input:nth-of-type(4) { width: 10em; }
  65. </style>`);
  66.  
  67. // the available standard buttons, display & insert stuff
  68. let config_std = new Map([
  69. ["bold", { icon: "&#xf032;", text: "Bold", ins_pre: "<b>", ins_app: "</b>" }],
  70. ["italic", { icon: "&#xf033;", text: "Italic", ins_pre: "<em>", ins_app: "</em>" }],
  71. ["underline", { icon: "&#xf0cd;", text: "Underline", ins_pre: "<u>", ins_app: "</u>" }],
  72. ["strike", { icon: "&#xf0cc;", text: "Strikethrough", ins_pre: "<s>", ins_app: "</s>" }],
  73. ["link", { icon: "&#xf0c1;", text: "Link", ins_pre: "<a href=\"\">", ins_app: "</a>" }],
  74. ["image", { icon: "&#xf03e;", text: "Image", ins_pre: "<img src=\"", ins_app: "\" />" }],
  75. ["quote", { icon: "&#xf10d;", text: "Quote", ins_pre: "<blockquote>", ins_app: "</blockquote>" }],
  76. ["paragraph", { icon: "&#xf1dd;", text: "Paragraph", ins_pre: "<p>", ins_app: "</p>" }],
  77. ["listnum", { icon: "&#xf0cb;", text: "Numbered List", ins_pre: "<ol><li>", ins_app: "</li></ol>" }],
  78. ["listbull", { icon: "&#xf0ca;", text: "Bullet List", ins_pre: "<ul><li>", ins_app: "</li></ul>" }],
  79. ["listitem", { icon: "&#xf192;", text: "List Item", ins_pre: "<li>", ins_app: "</li>" }],
  80. ]);
  81.  
  82. // build GUI for chosen enable/disable of standard buttons
  83. let standardbuttons = '';
  84. config_std.forEach((val, key) => {
  85. standardbuttons += `<label for="${key}" title="${val.text}">${val.icon}</label><input type="checkbox" name="${key}" id="${key}" ${(standardmap.get(key)==="true" || standardmap.size == 0) ? 'checked="checked"' : ""}>`;
  86. });
  87.  
  88. // reformat the stored custom buttons to match the standard
  89. let config_custom = new Map();
  90. custommap.forEach((val, key) => {
  91. val = JSON.parse(val); // turn the string into an array of 4x2 each
  92. let newval = {}; // turn the array into an object
  93. val.forEach((v) => {
  94. newval[v[0]] = v[1];
  95. });
  96. config_custom.set(key, newval);
  97. });
  98.  
  99. // build GUI for stored custom buttons
  100. let custombuttons = '';
  101. config_custom.forEach((val) => {
  102. custombuttons += `<div><button class="remove">-</button><input type="text" name="icon" value="${val.icon}"><input type="text" name="text" value="${val.text}">
  103. <input type="text" name="ins_pre" value="${val.ins_pre}"><input type="text" name="ins_app" value="${val.ins_app}"></div>`;
  104. });
  105.  
  106. // template for a blank row to add a custom button (is cloned before inserting into DOM)
  107. let newcustombutton = `<div><button class="remove">-</button><input type="text" name="icon" placeholder="Icon"><input type="text" name="text" placeholder="Title">
  108. <input type="text" name="ins_pre" placeholder="Insert Before"><input type="text" name="ins_app" placeholder="Insert After"></div>`;
  109.  
  110. $(cfg).html(`<form>
  111. <fieldset id='stdbutton'>
  112. <legend>Standard text formatting</legend>
  113. <p>Select the buttons you'd like to see as options on the button bar.</p>
  114. ${standardbuttons}
  115. </fieldset>
  116. <fieldset id='custombutton'>
  117. <legend>Custom HTML or text</legend>
  118. <p>Define custom buttons, which will insert HTML and/or text.</p>
  119. <ul><li>In the first field, choose <a href="https://fontawesome.com/v4/icons/">the Icon</a> you want on the button.<br />
  120. Copy its 4-letter Unicode (for example "f004" for the heart) into this field.</li>
  121. <li>If you leave the Icon field empty, the Title from the second field is shown on the button instead. The Title also appears as mouseover text.</li>
  122. <li>Put the text you want inserted around the cursor position into the Insert Before and Insert After fields.</li></ul>
  123. ${custombuttons}
  124. <div><button class="add">+</button></div>
  125. </fieldset>
  126. <p>Any changes only apply after reloading the page.</p>
  127. </form>`);
  128.  
  129. // attach it to the DOM so that selections work (but only if #main exists, else it might be a Retry Later error page)
  130. if ($("#main").length == 1) $("body").append(cfg);
  131.  
  132. // turn checkboxes and radiobuttons into pretty buttons
  133. $( "#cmtFmtDialog input[type='checkbox'], #cmtFmtDialog input[type='radio']" ).checkboxradio({ icon: false });
  134.  
  135. // optimizing the size of the GUI in case it's a mobile device
  136. let dialogwidth = parseInt($("body").css("width")); // parseInt ignores letters (px)
  137. dialogwidth = dialogwidth > 550 ? 550 : dialogwidth * 0.9;
  138.  
  139. // initialize the dialog (but don't open it)
  140. $( "#cmtFmtDialog" ).dialog({
  141. appendTo: "#main",
  142. modal: true,
  143. title: 'Comment Formatting Buttons',
  144. draggable: true,
  145. resizable: false,
  146. autoOpen: false,
  147. width: dialogwidth,
  148. position: {my:"center", at: "center top"},
  149. buttons: {
  150. Reset: deleteConfig,
  151. Save: storeConfig,
  152. Cancel: function() { $( "#cmtFmtDialog" ).dialog( "close" ); }
  153. }
  154. });
  155.  
  156. // event triggers if form is submitted with the <enter> key
  157. $( "#cmtFmtDialog form" ).on("submit", (e) => {
  158. e.preventDefault();
  159. storeConfig();
  160. });
  161.  
  162. // putting event triggers on buttons that will delete custom rows
  163. function evRemoveRow(el) {
  164. $(el).on("click", (e) => {
  165. e.cancelBubble = true;
  166. e.preventDefault();
  167. $(e.target).parent().remove(); // delete whole div
  168. });
  169. }
  170. // run it immediately on the stored custom buttons
  171. evRemoveRow($( "#cmtFmtDialog button.remove" ));
  172.  
  173. // putting event trigger on button that will add blank custom rows
  174. $( "#cmtFmtDialog button.add" ).on("click", (e) => {
  175. e.cancelBubble = true;
  176. e.preventDefault();
  177. // add a new blank row and attach the remove event again
  178. $(e.target).parent().before( $(newcustombutton).clone() );
  179. evRemoveRow($( "#cmtFmtDialog button.remove:last-of-type" ));
  180. });
  181.  
  182. function deleteConfig() {
  183. // deselects all buttons, empties all fields in the form
  184. $('#cmtFmtDialog form').trigger("reset");
  185. $('#cmtFmtDialog button.remove').trigger("click");
  186.  
  187. // deletes the localStorage
  188. localStorage.removeItem('cmtfmtstandard');
  189. localStorage.removeItem('cmtfmtcustom');
  190.  
  191. $( "#cmtFmtDialog" ).dialog( "close" );
  192. }
  193.  
  194. function storeConfig() {
  195. // build a Map() for enabled standard buttons => button -> true/false
  196. let storestd = new Map();
  197. $( "#cmtFmtDialog #stdbutton [name]" ).each(function() { storestd.set( $(this).prop('name'), String($(this).prop('checked')) ); });
  198. localStorage.setItem('cmtfmtstandard', JSON.stringify(Array.from(storestd.entries())));
  199.  
  200. // build a Map() for the custom buttons => custom# -> { icon: X, text: X, ins_pre: X, ins_app: X }
  201. let storecustom = new Map();
  202. $( "#cmtFmtDialog #custombutton div:has(input)" ).each((i, div) => {
  203. let parts = new Map();
  204. $(div).find('[name]').each(function() { parts.set( $(this).prop('name'), $(this).prop('value') ); });
  205. storecustom.set('custom'+i, JSON.stringify(Array.from(parts.entries())));
  206. });
  207. localStorage.setItem('cmtfmtcustom', JSON.stringify(Array.from(storecustom.entries())));
  208.  
  209. $( "#cmtFmtDialog" ).dialog( "close" );
  210. }
  211.  
  212. /* CREATING THE LINK TO OPEN THE CONFIGURATION DIALOG */
  213.  
  214. // if no other script has created it yet, write out a "Userscripts" option to the main navigation
  215. if ($('#scriptconfig').length == 0) {
  216. $('#header').find('nav[aria-label="Site"] li.dropdown').last()
  217. .after(`<li class="dropdown" id="scriptconfig">
  218. <a class="dropdown-toggle" href="/" data-toggle="dropdown" data-target="#">Userscripts</a>
  219. <ul class="menu dropdown-menu"></ul></li>`);
  220. }
  221. // then add this script's config option to navigation dropdown
  222. $('#scriptconfig .dropdown-menu').append(`<li><a href="javascript:void(0);" id="opencfg_cmtfmt">Comment Formatting Buttons</a></li>`);
  223.  
  224. // on click, open the configuration dialog
  225. $("#opencfg_cmtfmt").on("click", function(e) {
  226. $( "#cmtFmtDialog" ).dialog('open');
  227. });
  228.  
  229. /*********************************************************
  230. COMMENT BAR AND PREVIEW FUNCTIONALITY
  231. *********************************************************/
  232.  
  233. // merge the enabled standard and custom buttons into one list
  234. let config = new Map();
  235. config_std.forEach((val, key) => { if (standardmap.get(key)==="true" || standardmap.size == 0) config.set(key, val); });
  236. config_custom.forEach((val, key) => {
  237. if (val.icon !== "") val.icon = `&#x${val.icon};`; // add what Font Awesome needs to display properly
  238. config.set(key, val);
  239. });
  240.  
  241. $("head").append(`<style type="text/css"> ul.comment-format { font-family: FontAwesome, sans-serif; float: left; }
  242. ul.comment-format a { cursor: default; }
  243. ul.comment-format .fontawe { font-family: FontAwesome, sans-serif; }
  244. div.comment-preview.userstuff { border: 1px inset #f0f0f0; min-height: 1em; padding: 0.2em 1em; line-height: 1.5; } </style>`);
  245.  
  246. // collate the button bar
  247. let buttonBar = document.createElement('ul');
  248. $(buttonBar).addClass('actions comment-format');
  249. for (let c of config) {
  250. let li = document.createElement('li');
  251. li.title = c[1].text;
  252. li.innerHTML = `<a class="${c[0]}">${ (c[1].icon === "") ? c[1].text : c[1].icon}</a>`;
  253. if (c[1].icon !== "") $(li).addClass("fontawe");
  254. $(buttonBar).append(li);
  255. }
  256. // delegated event handlers for button clicks and update of the comment preview
  257. $('body').on('click', 'ul.comment-format a', function(e) {
  258. e.preventDefault();
  259. insert_format(e.target);
  260. });
  261. $('body').on('input', 'textarea.comment-preview', function(e) {
  262. update_preview(e.target);
  263. });
  264.  
  265. // preview box template (will be cloned when inserting into DOM)
  266. let preview = `<div class='comment-preview userstuff' title='Comment Preview (approximate)'></div>`;
  267.  
  268. // click event function called with the button <a> that was clicked (so we know which textarea to insert it to)
  269. function insert_format(elm) {
  270. let area = $(elm).parent().parent().next('textarea')[0]; // the textarea element we're dealing with
  271. let text = $(area).val(); // the original content of the comment box
  272. let cursor_start = area.selectionStart, cursor_end = area.selectionEnd; // any highlighted text
  273. let fmt = config.get(elm.className); // grab the formatting HTML corresponding to the clicked button
  274.  
  275. // set the comment box text with the new content, and focus back on it
  276. $(area).val(
  277. text.slice(0, cursor_start) + // text from before cursor position or highlight
  278. fmt.ins_pre + text.slice(cursor_start, cursor_end) + fmt.ins_app + // wrap any highlighted text in the formatting HTML
  279. text.slice(cursor_end) // text from after cursor position or highlight
  280. ).focus();
  281.  
  282. // set the cursor position to the same value so we don't highlight anymore
  283. let cursor_new =
  284. // if we only inserted format HTML, set it between the halves so you can enter the text to format
  285. (cursor_start == cursor_end) ? cursor_start + fmt.ins_pre.length :
  286. // if we highlighted, and this is a link (so the link text is already done), set the cursor into the href=""
  287. (elm.className == "link") ? cursor_start + fmt.ins_pre.length - 2 :
  288. // otherwise always set it at the end of the inserted text i.e. the same distance from the end as originally
  289. $(area).val().length - (text.length - cursor_end);
  290. area.selectionStart = area.selectionEnd = cursor_new;
  291.  
  292. // manually trigger the value-has-changed event so the preview updates (not calling update_preview directly as it would fail on Sticky Comment Box)
  293. $(area).trigger('input');
  294. }
  295.  
  296. // function called when anything changes (input event trigger) in the textarea
  297. function update_preview(elm) {
  298. let prevbox = $(elm).siblings('div.comment-preview')[0];
  299. prevbox.innerHTML = parse_preview($(elm).val());
  300. }
  301.  
  302. // adding the button bar & preview box for the New/Edit Work/Chapter form
  303. if ($('#work_summary, #work_notes, #work_endnotes, #chapter_summary, #chapter_notes, #chapter_endnotes').filter('textarea').length > 0) {
  304. $('#work_summary, #work_notes, #work_endnotes, #chapter_summary, #chapter_notes, #chapter_endnotes').filter('textarea')
  305. .before($(buttonBar).clone())
  306. .after($(preview).clone())
  307. .addClass('comment-preview')
  308. .each(function() { update_preview(this); }); // update the preview for reloaded pages with cached comment text
  309. }
  310.  
  311. // if we're not posting/editing a work, we are probably viewing a page that allows commenting: add for visible comment boxes and handle the Sticky Comment Box
  312. else if ($('#main').find('textarea[id^="comment_content_for"]').length > 0) {
  313. // adding the button bar & preview box for any visible comment area (clone with events!)
  314. $('textarea[id^="comment_content_for"]')
  315. .before($(buttonBar).clone())
  316. .after($(preview).clone())
  317. .addClass('comment-preview')
  318. .each(function() { update_preview(this); }); // update the preview for reloaded pages with cached comment text
  319.  
  320. // Support for Sticky Comment Box!
  321. // if this script executes first, we may have to wait for the Sticky Comment Box to appear in the DOM
  322. if ($('#float_cmt_dlg').length == 0) {
  323. const observer = new MutationObserver(function(mutList, obs) {
  324. for (const mut of mutList) { for (const node of mut.addedNodes) {
  325. // check if the added node is our comment box
  326. if (node.id == 'float_cmt_dlg') {
  327. obs.disconnect(); // stop listening immediately, we have what we needed
  328. // add the buttonbar to the Sticky Comment Box (it doesn't get a preview field to save space)
  329. $('#float_cmt_userinput textarea').before($(buttonBar).clone().css('font-size', '80%'));
  330. }
  331. }}
  332. });
  333.  
  334. // listening to as few changes as possible: only direct children of <body>
  335. observer.observe($('body').get(0), { attributes: false, childList: true, subtree: false });
  336.  
  337. // failsafe: stop listening after 5 seconds (in case the other script isn't running)
  338. // this will always execute even if the box was already found and the observer disconnected previously
  339. let timeout = setTimeout(() => {
  340. observer.disconnect();
  341. }, 5 * 1000);
  342. }
  343. // when the Sticky Comment Box script executed first and the textarea is already there, we immediately add the button bar
  344. else $('#float_cmt_userinput textarea').before($(buttonBar).clone().css('font-size', '80%'));
  345. }
  346.  
  347. // and if we're doing comments on either a work or the inbox, we need to wait for dynamic replies
  348. if($('#feedback, #reply-to-comment, #main.comments-show').length > 0) {
  349. // adding the bar for any dynamically loaded comment areas: inbox replies, work/tag replies, editing existing comments
  350. const waitforreply = new MutationObserver(function(mutList, obs) {
  351. for (const mut of mutList) { for (const node of mut.addedNodes) {
  352. // check if the added node is our comment box
  353. if (node.nodeType == 1 && node.id.startsWith('comment_form_for')) {
  354. $(node).find('textarea')
  355. .before($(buttonBar).clone())
  356. .after($(preview).clone())
  357. .addClass('comment-preview')
  358. .each(function() { update_preview(this); }); // update the preview with the existing comment text
  359. }
  360. }}
  361. });
  362.  
  363. // listening to the places where Ao3 adds the HTML in for the reply box
  364. waitforreply.observe($('#feedback, #reply-to-comment, #main.comments-show').get(0), { attributes: false, childList: true, subtree: true });
  365. }
  366.  
  367. // if we're in a bin, we have to handle the View & Post Comment box (only if there are /comments buttons)
  368. if ($('#wrangulator').find('a[href$="/comments"]').length > 0) {
  369. // if this script executes first, we may have to wait for the View & Post Comment box to appear in the DOM
  370. if ($('#peekTopLevelCmt').length == 0) {
  371. let waitfmt = new MutationObserver(function(mutList, obs) {
  372. for (let mut of mutList) { for (let node of mut.addedNodes) {
  373. // check if the added node is our comment box
  374. if (node.id === 'peekTopLevelCmt') {
  375. obs.disconnect(); // stop listening immediately, we have what we needed
  376. // add the buttonbar to the View & Post Comment box (it doesn't get a preview field to save space)
  377. $('#peekTopLevelCmt textarea').before($(buttonBar).clone());
  378. }
  379. }}
  380. });
  381.  
  382. // listening to as few changes as possible: only direct children of #main
  383. waitfmt.observe($('#main').get(0), { attributes: false, childList: true, subtree: false });
  384.  
  385. // failsafe: stop listening after 5 seconds (in case the other script isn't running)
  386. // this will always execute even if the box was already found and the observer disconnected previously
  387. let timeout = setTimeout(() => {
  388. waitfmt.disconnect();
  389. }, 5 * 1000);
  390. }
  391. // when the View & Post Comment box script executed first and the textarea is already there, we immediately add the button bar
  392. else $('#peekTopLevelCmt textarea').before($(buttonBar).clone());
  393. }
  394.  
  395. // on bookmarks, there's either an Edit button to manage my own bookmark, or a Save button to bookmark that work
  396. if ($('div[id^="bookmark_form_placement_for_"]').length > 0) {
  397. const waitforbkmk = new MutationObserver(function(mutList, obs) {
  398. for (const mut of mutList) { for (const node of mut.addedNodes) {
  399. // check if the added node is our comment box
  400. if (node.nodeType == 1 && node.id === 'bookmark-form') {
  401. $(node).find('textarea')
  402. .before($(buttonBar).clone())
  403. .after($(preview).clone())
  404. .addClass('comment-preview')
  405. .each(function() { update_preview(this); }); // update the preview with the existing comment text
  406. }
  407. }}
  408. });
  409.  
  410. // listening to the places where Ao3 adds the HTML for the add/edit bookmark box
  411. // unfortunately the only way to listen to multiple elements is to loop through the list, but then we don't need to listen to the whole tree (:
  412. $('div[id^="bookmark_form_placement_for_"]').each(function() {
  413. waitforbkmk.observe($(this).get(0), { attributes: false, childList: true, subtree: false });
  414. });
  415. }
  416. // when viewing a work, the bookmark form is already loaded, just not visible
  417. if ($('#main').find('textarea#bookmark_notes').length > 0) {
  418. // adding the button bar & preview box for any visible comment area (clone with events!)
  419. $('textarea#bookmark_notes')
  420. .before($(buttonBar).clone())
  421. .after($(preview).clone())
  422. .addClass('comment-preview')
  423. .each(function() { update_preview(this); }); // update the preview for reloaded pages with cached comment text
  424. }
  425.  
  426. function parse_preview(content) {
  427. // if the comment box is still empty, show a simple placeholder
  428. if (content == "") return "<p><i>preview</i></p>";
  429.  
  430. // if there is comment text, turn double linebreaks into paragraphs and single linebreaks into <br>
  431. // linebreak compatibility
  432. const lbr = (content.indexOf("\r\n") > -1) ? "\r\n" :
  433. (content.indexOf("\r") > -1) ? "\r" : "\n";
  434.  
  435. // remove obvious issues: whitespaces between <li>'s, a <br> plus linebreak (while editing)
  436. content = content.replace(/<\/li>\W+<li>/ig, '</li><li>');
  437. content = content.replace(/<br \/>(\r\n|\r|\n)/ig, '<br />');
  438.  
  439. content = content.split(`${lbr}${lbr}`); // split content at each two linebreaks in a row
  440. const regexLine = new RegExp(`${lbr}`, "g");
  441. content.forEach((v, i) => {
  442. v = v.replace(regexLine, "<br />"); // a single linebreak is replaced by a <br>
  443. content[i] = "<p>"+v.trim()+"</p>"; // two linebreaks are wrapped in a <p>
  444. });
  445. return content.join(lbr);
  446. }
  447.  
  448. })(jQuery);