Iterate over source page with cheerio and performing logic
<p>I want to be able to perform some logic across all the HTML in the source page using <code>cheerio</code>. I'm not sure how to do this as I can't seem to find a method that can do this in the <a href="https://github.com/cheeriojs/cheerio" rel="nofollow">cheerio library</a>. I think the closest method is the <code>.each()</code> <a href="https://github.com/cheeriojs/cheerio#each-functionindex-element-" rel="nofollow">method</a>. However, it seems like I need to have a tag to match with first. I want to perform logic to EVERY piece of HTML.</p>

<p>This is what I have so far:</p>

<pre><code>    let $ = cheerio.load(pageSource);
</code></pre>

<p>This is the function I want to apply to every tag and its contents:</p>

<pre><code>    summarizeContent(content) {
        let contentLength = content.length;
        let middle = " ....... ";
        if ((contentLength&gt; this.contentSummarizeMinLength) &amp;&amp; (contentLength &lt; this.contentSummarizeMaxLength)) {
            let chunkIndex = 0;
            const increment = 5;
            while (contentLength &gt; this.contentSummarizeMinLength) {
                chunkIndex += increment;
                contentLength -= 2 * increment;
            }
            content = content.substring(0, chunkIndex) + middle + content.substr(-chunkIndex);
        } else if (contentLength &gt;= this.contentSummarizeMaxLength) {
            const chunk = 20;
            content = content.substring(0, chunk) + middle + content.substr(-chunk);
        }
        return content;
    }
</code></pre>

<p>So after the HTML transform, I should have an output like this if this was my input:</p>

<pre><code>&lt;ul id="fruits"&gt;
    &lt;li class="apple"&gt;AppleAppleAppleAppleAppleAppleAppleAppleApple&lt;/li&gt;
    &lt;li class="orange"&gt;OrangeOrangeOrangeOrangeOrangeOrangeOrange&lt;/li&gt;
    &lt;li class="pear"&gt;PearPearPearPearPearPearPearPearPearPearPearPear&lt;/li
&lt;/ul&gt;
</code></pre>

<p>output:</p>

<pre><code>&lt;ul id="fruits"&gt;
    &lt;li class="apple"&gt;AppleApple ...... AppleApple&lt;/li&gt;
    &lt;li class="orange"&gt;Orange ...... Orange&lt;/li&gt;
    &lt;li class="pear"&gt;PearPear ...... PearPear&lt;/li
&lt;/ul&gt;
</code></pre>

<p>The content within the <code>&lt;li&gt;</code> tags don't matter. I just want to be able to apply a function to across some content between HTML tags. Usage of Cheerio is not required, it was the first library I found that I should would do the job.</p>