RSS Guard filter: Mark news item as important

A filter for RSS Guard that will mark a news item as important, if it finds any text that matches specific regular expression patterns. Separate patterns can be defined to match only news item titles against, or only item bodies, or both.

  • In addition, the title of an important news item will be prefixed with a visual length indicator of the text contained in its text body: ranging from [▁] for a short text body with fewer than 800 characters, to [█] if the body text of the news item exceeds 2400 characters.
  • Also, the news item title will be prefixed with the [matched text], enclosed in square brackets, directly after the length marker. This serves as a quick information about what exactly triggered the filter.

The filter checks separate pattern lists for the title only, the body only, or both:

  • Add anything to anywhereRegExps that should be matched no matter whether in the title or the text body of the news item. In the example below, if the text »Nature« is found in either the title or the text body, the news item will be marked important. Lower case or upper case won’t matter, so »nature« will also trigger the filter. However, »Yellowstone« will only trigger it if it’s found exactly like that.
  • Add anything to titleRegExps that should be matched in the title of the news item only. In the example below, if »bird« is found, the news item will be marked important. Lower case or upper case won’t matter, so »Bird« will also trigger the filter.
  • Add anything to bodyRegExps that should be matched in the text body of the news item only. In the example below, if »wolf« is found, the news item will be marked important. Lower case and upper case do matter here.

The patterns are regular expressions, so you may add more complicated patterns once you’re familiar with that.

To copy this filter into RSS Guard, first hover with your mouse over the listing below and press »Open code in new window«. Copying the colored and formatted listing below, as-is, won’t work.
function filterMessage() {
  /* Configure here: */

  anywhereRegExps = [ /(Nature)/i, /(Yellowstone)/ ];
  titleRegExps = [ /(bird)/i ];
  bodyRegExps = [ /(wolf)/ ];


  /* Ignore from here, unless you're a developer: */

  function mark(prefix) {
    trimmedPrefix = prefix.trim();
    titleLabel = '[ ' + trimmedPrefix + ' ]';

    if (!msg.title.includes(titleLabel)) {
      msg.isImportant = true;

      contentLength = '[ █ ]';
      if (msg.contents.length < 800) {
        contentLength = '[ ▁ ]';
      } else if (msg.contents.length < 2400) {
        contentLength = '[ ▄ ]';
      }

      if (msg.title.match(/^\[ [▁▄█] \]/)) {
        msg.title = msg.title.replace(
            /^(\[ [▁▄█] \])(.*)/, '$1 ' + titleLabel + '$2');
      } else {
        msg.title = contentLength + ' ' + titleLabel + ' ' + msg.title;
      }
    }
  }

  for (i = 0; i < anywhereRegExps.length; i++) {
    found = msg.title.match(anywhereRegExps[i]);

    if (found) {
      mark(found[0]);
    } else {
      found = msg.contents.match(anywhereRegExps[i]);
      if (found) {
        mark(found[0]);
      }
    }
  }

  for (i = 0; i < titleRegExps.length; i++) {
    found = msg.title.match(titleRegExps[i]);

    if (found) {
      mark(found[0]);
    }
  }

  for (i = 0; i < bodyRegExps.length; i++) {
    found = msg.title.match(bodyRegExps[i]);

    if (found) {
      mark(found[0]);
    }
  }

  return MessageObject.Accept;
}

Image Credits:
Papirus icon for RSS Guard (modified) | GNU General Public License, version 3

Licensing:
This content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
For your attributions to us please use the word »tuxwise«, and the link https://tuxwise.net.