Feature: Callback like `onRemoveNode` before a node is being removed

Open
#856 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript
Domain
web-dev

Research direction

Start with Readability.js, especially REGEXPS.unlikelyCandidates and _removeAndGetNext, and review the related context in issue #799. Trace where nodes are removed and determine the callback's expected behavior and public option shape. Done means the proposed removal hook is consistently handled without disrupting the existing readability algorithm.

Written by the indexing model from the issue text.

Description

feature-request

I have a similar use case #799 where a node is being removed because it the class name contains the header keyword which is matched by REGEXPS.unlikelyCandidates:

https://github.com/mozilla/readability/blob/d64951b44bed21ca0f9e0113afd0761b0e0f9d05/Readability.js#L122-L125

Of course I could fork and adapt the regex. However, I think it would be better if there was a generic and dynamic approach to influence the algorithm. For example a callback that is invoked every time a node is being removed by the algorithm, something like this:

var article = new Readability(document, {
    onRemoveNode: (node) => {
        // get all heading elements inside the node
        const headings = this._getAllNodesWithTag(node, ["h1", "h2", "h3", "h4", "h5", "h6"]).length;
        
        // remove node only if it doesn't contain any heading elements
        return headings.length === 0;
    }
});

This callback could be invoked directly from _removeAndGetNext:

https://github.com/mozilla/readability/blob/d64951b44bed21ca0f9e0113afd0761b0e0f9d05/Readability.js#L793-L797

If there is any interest in this, I'd willing to submit a PR.

Dominant language
JavaScript
Stars
11.5k
Forks
732
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mozilla/readability

All issues in mozilla/readability

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.