Skip to content

Commit

Permalink
Add exclusions CSS selectors
Browse files Browse the repository at this point in the history
This is needed if there are selector that match in elements that should
be excluded from the index.
  • Loading branch information
nitriques committed Mar 29, 2018
1 parent 247295f commit 366718e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ CSS selector for the title of the page.

CSS selector for the "key" property. You can add custom keys as you wish.

### exclusions: Object

An object containing CSS selectors to find elements that must not be indexed.
Those CSS selectors are matched for each node and are check against all their parents to make
sure non of its parent are excluded.

#### exclusions.text: String

CSS selector of excluded elements for the text of the page.

#### exclusions[key]: String

CSS selector of excluded elements for "key" property. The key must match the one used in selectors[key].

#### formatters: Object

An object containing formatter string. Their values are removed from the original result obtained
Expand Down
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ config.selectors = _.map(config.selectors, (selector, key) => {
return {
key,
attributes: selector.attributes,
selector: selector.selector
selector: selector.selector,
exclude: config.exclusions && config.exclusions[key]
};
});

Expand Down
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"image": "meta[property=\"og:image\"]",
"description": "meta[name=\"description\"]",
"text": "h1, h2, h3, h4, h5, h6, p, li"
},
"exclusions": {

},
"formatters": {
"title": "-"
Expand Down
6 changes: 5 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ const parse = (record, data, config) => {
const key = selector.key;
if (record[key] === undefined) {
record[key] = [];
const nodes = $(selector.selector);
// Fetch all and filter exclusions
const nodes = $(selector.selector).filter((i, node) => {
return !selector.exclude || $(node).closest(selector.exclude).length === 0;
});

// Populate the record
_.each(nodes, (node) => recursiveFindValue(node, record[key], selector.attributes));

Expand Down
13 changes: 13 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ test('Custom selector parse', (t) => {
t.end();
});

test('Selector exclusion parse', (t) => {
const rec = {};
const c = _.clone(config);
const data = `<html><body>
<a>test</a>
<footer><a class="no-ok">not-ok</a></footer>
</body></html>`;
c.selectors.push({key: 'links', selector: 'a', exclude: 'footer'});
parse(rec, data, c);
t.equal(rec.links, 'test');
t.end();
});

test('JSON formatter', (t) => {
const rec = {};
const c = _.clone(config);
Expand Down

0 comments on commit 366718e

Please sign in to comment.