Author/share and social-counter removals delete tokens inside code blocks (`result.author` becomes `result.`)
Two loops in removeByContentPattern remove an element whose whole text is a label or a counter, but
neither skips code. Syntax highlighters put each token (GitHub, Pygments-style) or each line (Shiki-style)
in its own <span>, so code text is silently removed.
Seen on GitHub's rendering of this repo's own README: console.log(result.author); comes out as
console.log(result.);, in both fenced examples.
Repro (defuddle 0.19.6, linkedom):
import { parseHTML } from 'linkedom';
import { Defuddle } from 'defuddle/node';
const html = `<!doctype html><html><head><title>Reading results</title></head><body><article>
<h1>Reading results</h1>
<p>This guide shows how to read the object the parser returns. The parser takes a document, finds the
main content, removes clutter such as navigation and sidebars, and returns the cleaned content.</p>
<pre><span class="pl-c">// Parse the current document and read the metadata the parser found on the page</span>
<span class="pl-k">const</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">parser</span>.<span class="pl-en">parse</span>();
<span class="pl-smi">console</span>.<span class="pl-en">log</span>(<span class="pl-s1">result</span>.<span class="pl-c1">author</span>);
<span class="pl-s1">menu</span>.<span class="pl-en">share</span>(<span class="pl-s1">result</span>.<span class="pl-c1">url</span>);</pre>
<p>The result carries the title, the author, the publication date and the content. Each property is a
plain string, so it can be logged or stored without further conversion.</p>
<pre><code><span class="line">Written by</span>
<span class="line">Share</span>
<span class="line">12 Likes</span></code></pre>
<p><code>3 Comments</code></p>
<p>That is the whole result.</p>
</article></body></html>`;
const { document } = parseHTML(html);
const result = await Defuddle(document, 'https://example.com/guide', { markdown: true });
console.log(result.content);
Output, code blocks only:
// Parse the current document and read the metadata the parser found on the page
const result = parser.parse();
console.log(result.);
menu.(result.url);
Written by
Share
and the `3 Comments` paragraph is gone. With debug: true the removals are listed as
"author/share widget" (author, share) and "social engagement counter" (12 Likes, 3 Comments).
Cause, in src/removals/content-patterns.ts:
- The author/share widget loop (
SHARE_AUTHOR_LABEL, line 1249) matches aspanwhose text is exactlyauthor/share/follow. It climbs while the parent has 15 words or fewer and skips a container that holdspre/code, but when the<pre>itself has more than 15 words the container stays the span, which holds nothing, so the token is removed. When the<pre>is short and holds spans directly (no<code>), the climb can stop at the<pre>and remove the whole block. - The social counter loop (
SOCIAL_COUNTER_PATTERN, line 1271) has no content-element check at all, so a line span or an inline-code paragraph reading "12 Likes" near the end of the content is removed.
The same file's other text loops already guard against this with if (el.closest('pre, code')) continue;
(lines 520, 1008, 1171).
Same class as #167, which fixed removeBySelector with an ancestor check; these two loops in
removeByContentPattern were not covered by it.
Suggested fix: the same guard in both loops, plus querySelector for the counter loop, which has no
content-element check of its own:
if (el.closest('pre, code') || el.querySelector('pre, code')) continue;
after the SHARE_AUTHOR_LABEL.test and SOCIAL_COUNTER_PATTERN.test checks. With that applied to
0.19.6's compiled content-patterns.js, the repro above returns every code line unchanged. Run
through the browser bundle on eight assorted saved pages, the guard changed nothing else.
I reproduced this on current main with the repro from the description: the fixture comes out with console.log(result.);, menu.(result.url);, and the 12 Likes line and the inline-code 3 Comments paragraph are removed.
I'm taking this one. The fix adds a pre/code guard to both loops in removeByContentPattern, plus a fixture in tests/fixtures / tests/expected that fails before the fix. PR follows.