Sha256: 710e18859ec139684ea47770d5ae9844185246318e3f3a8264e1069c38e96eac

Contents?: true

Size: 1.77 KB

Versions: 22

Compression:

Stored size: 1.77 KB

Contents

var ume = (function() {
  function normalise(text) {
    return text.replace(/\s+/g, " ").trim();
  }

  function escapeEntities(text) {
    return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  }

  const BOLD_ITALIC_REGEX = /(?:(\W|^)(\*)|(\*)(\W|$)|(\W|^)(_)|(_)(\W|$))/;

  function parseInlines(text) {
    const parts = text.split(BOLD_ITALIC_REGEX);

    if(parts.filter(function(part) { return part == "*"; }).length % 2 != 0) return text;
    if(parts.filter(function(part) { return part == "_"; }).length % 2 != 0) return text;

    var inBold = false;
    var inItalic = false;
    var html = [];

    for(const part of parts) {
      if(part == "*") {
        inBold = !inBold;
        html.push(inBold ? "<b>" : "</b>"); //Note that in_bold has been inverted, so this is inverted as well
      }
      else if(part == "_") {
        inItalic = !inItalic;
        html.push(inItalic ? "<i>" : "</i>"); //Note that in_italic has been inverted, so this is inverted as well
      }
      else {
        html.push(part);
      }
    }

    return html.join("");
  }

  function parseHeading(text) {
    const parts = text.match(/^(#+) (.*)$/);
    return "<h" + parts[1].length + ">" + parseInlines(parts[2]) + "</h" + parts[1].length + ">";
  }

  function parseParagraph(text) {
    return "<p>" + parseInlines(text) + "</p>";
  }

  return function(text) {
    const nodes = text.split(/(\r?\n){2,}/g);
    var html = [];

    for(const node of nodes) {
      const cleanedNode = escapeEntities(normalise(node));

      if(cleanedNode.startsWith("#")) html.push(parseHeading(cleanedNode));
      else html.push(parseParagraph(cleanedNode));
    }

    return html.join("");
  }
})();

if(typeof module === "object" && typeof module.exports === "object") module.exports = ume;

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
servel-0.33.0 app/js/ume.js
servel-0.32.0 app/js/ume.js
servel-0.31.0 app/js/ume.js
servel-0.30.0 app/js/ume.js
servel-0.29.0 app/js/ume.js
servel-0.28.0 app/js/ume.js
servel-0.27.0 app/js/ume.js
servel-0.26.0 app/js/ume.js
servel-0.25.0 app/js/ume.js
servel-0.24.0 app/js/ume.js
servel-0.23.0 app/js/ume.js
servel-0.22.0 app/js/ume.js
servel-0.21.0 app/js/ume.js
servel-0.20.0 lib/servel/templates/js/ume.js
servel-0.19.0 lib/servel/templates/js/ume.js
servel-0.18.0 lib/servel/templates/js/ume.js
servel-0.17.0 lib/servel/templates/js/ume.js
servel-0.16.0 lib/servel/templates/js/ume.js
servel-0.15.0 lib/servel/templates/js/ume.js
servel-0.14.0 lib/servel/templates/js/ume.js