1

I'm wanting to write my first Jekyll plug-in. I've recently come across the flesch-kincaid algorithm for determining general reading level. What I'm thinking is to write something that adds a data attribute to each <p> tag or any element marked with [data-read-level].

Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words )

What category does this fall under though? I'm thinking generator myself. Is there maybe some better way to show the results rather than an attribute?

I've written a somewhat working JS based version here, it needs work probably and only works with English right now.

JS Version:

// Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words )

let fkExamine = function() {
    window.addEventListener('DOMContentLoaded', init);

    const fkConstA = 206.835;
    const fkConstB = 1.015;
    const fkConstC = 84.6;

    function init() {
      console.log('--      fkExamine.js initiated');
      let parseArray = [].slice.call(document.querySelectorAll('p, [data-read-level]'));
      parseArray.map(function(parseItem) {
        let txtContent = parseItem.textContent;
        let totalWords = getTotalWords(txtContent);
        let totalSentences = getTotalSentences(txtContent);
        let totalSyllables = getTotalSyllables(txtContent);

        let fkScore = fkConstA - fkConstB * (totalWords / totalSentences) - fkConstC * (totalSyllables / totalWords);
        parseItem.setAttribute('fk-score', fkScore);
      });
    }

    function getTotalWords(input) {
      const words = input.split(' ');
      return words.filter(word => word !== '').length;
    }

    function getTotalSentences(input) {
      return input.match(/[\w|\)][.?!](\s|$)/g).length;
    }

    function getTotalSyllables(input) {
      let syllables = 0;
      let words = input.split(' ');
      words = words.filter(word => word !== '');

      for(let i = 0; i < words.length; i++) {
        words[i].toLowerCase();
        words[i].replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
        words[i].replace(/^y/, '');
        syllables += words[i].match(/[aeiouy]{1,2}/g).length;
      }
      return syllables;
    }
}();
Moxxi
  • 39
  • 7
  • 1
    Interesting but complicated. The examples on the Jekyll page are not really good. I think (as you want to target single elements in the site) you may need to build a [Tag plugin](https://jekyllrb.com/docs/plugins/tags/). Interested in other opinions though. – Christian Nov 06 '22 at 22:56
  • 2
    You could make it a filter, similar to how [this plugin](https://github.com/gjtorikian/jekyll-time-to-read) calculates reading time. I'm not sure if making this information available on a paragraph level would make sense to begin with, but rather on the post level maybe? – Benjamin W. Nov 07 '22 at 01:16
  • 1
    Shouldn't this rather be tagged [tag:ruby] instead of [tag:javascript]? – Benjamin W. Nov 07 '22 at 01:17
  • 1
    Filter sounds perfect actually thanks for that, I tagged JS mainly because I was writing this is JS first; but it seems very appropriate for Jekyll blogs. – Moxxi Nov 07 '22 at 02:05

0 Answers0