"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.topicTeaser = exports.postSummary = exports.parseRaw = exports.parsePost = void 0;

var _validator = _interopRequireDefault(require("validator"));

var _templates = _interopRequireDefault(require("./templates"));

var _parse = _interopRequireWildcard(require("./parse"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const eventRX = new RegExp((0, _parse.tagTemplate)('event', '[\\s\\S]*'));
const invalidRX = new RegExp(`(${(0, _parse.tagTemplate)('event-invalid', '[\\s\\S]*')})`);

const parseRaw = async content => {
  const input = content.replace(/\[description\]([\s\S]*)\[\/description\]/, '[description]<p>$1</p>[/description]');
  const event = (0, _parse.default)(input);

  if (!event) {
    return input.replace(invalidRX, '<div class="hide">$1</div>');
  }

  event.name = _validator.default.escape(event.name);
  const eventText = await (0, _templates.default)({
    event
  }); // must use function to avoid `$` being treated specially

  const text = input.replace(eventRX, () => eventText);
  return text;
};

exports.parseRaw = parseRaw;

const parsePost = async data => {
  const {
    postData
  } = data;
  postData.content = await parseRaw(postData.content);
  return data;
};

exports.parsePost = parsePost;

const reescape = str => _validator.default.escape(_validator.default.unescape(str));

const rawPattern = /\[event\]\s*\[name\](.*)\[\/name\][\s\S]*\[\/event\]/;
const wrapperPattern = /<!-- plugin-calendar-event-wrapper:start \|\|\|(.*?)\|\|\| -->[\s\S]*<!-- plugin-calendar-event-wrapper:end -->/;

const shortEvent = (full, name) => `<p>| [[calendar:event_title]]: ${reescape(name)} |</p>`;

const postSummary = (data, callback) => {
  data.posts.forEach(post => {
    if (post && post.content) {
      // eslint-disable-next-line no-param-reassign
      post.content = post.content.replace(rawPattern, shortEvent).replace(wrapperPattern, shortEvent);
    }
  });
  callback(null, data);
};

exports.postSummary = postSummary;

const topicTeaser = (data, callback) => {
  data.teasers.forEach(post => {
    if (post && post.content) {
      // eslint-disable-next-line no-param-reassign
      post.content = post.content.replace(wrapperPattern, shortEvent);
    }
  });
  callback(null, data);
};

exports.topicTeaser = topicTeaser;
//# sourceMappingURL=parseFilters.js.map