Bionic Reading

Quick and dirty implementation of the Bionic Reading API

Simple Implementation

// bionic.ts
function transform(content: string) {
  const words = content.split(' ');
  const htmlItems = words.map((word) => {
    const len = Math.ceil(word.length * 0.3);
    return `<b>${word.slice(0, len)}</b>${word.slice(len)}`;
  });

  return htmlItems.join(' ');
}

Using with cheerio

// bionic-nodejs.ts
import cheerio from 'cheerio';

function transform(sourceHtml: string) {
  const $ = cheerio.load(sourceHtml, {
    normalizeWhitespace: true,
    decodeEntities: true,
  });

  $('p').each((i, elem) => {
    const htmlItems: string[] = [];
    $(elem)
      .text()
      .split(/\s+/)
      .forEach((word) => {
        const len = Math.ceil(word.length * 0.3);

        htmlItems.push(`<b>${word.slice(0, len)}</b>${word.slice(len)}`);
      });

    const htmlOutput = htmlItems.join(' ');

    $(elem).html(htmlOutput);
  });

  return $.html();
}
Sign up to read news articles, Twitter threads & newsletters on your Kindle
© 2023 Podzim LLC

* Amazon, Kindle, Send to Kindle and all related logos are trademarks of Amazon.com, Inc. or its affiliates.

* This website was not created or endorsed by Amazon.