Skip to content

Commit

Permalink
fix: issues 837/838 (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister authored Aug 30, 2023
1 parent d94fa01 commit 94c7e93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/helix-shared-indexer/src/index-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,34 @@ const helpers = {
innerHTML: (elements) => elements.map((el) => el.children.map((child) => toHtml(child)).join('')),
match: (elements, re) => {
// todo: maybe base on function ?
const result = [];
const results = [];
const regex = new RegExp(re, 'g');

if (!Array.isArray(elements)) {
// eslint-disable-next-line no-param-reassign
elements = [elements];
}
let previousIndex = -1;
elements.forEach((el) => {
let m;
const content = typeof el === 'string' ? el : toText(el);

// eslint-disable-next-line no-cond-assign
while ((m = regex.exec(content)) !== null) {
result.push(m[m.length - 1]);
const { index } = m;
if (index === previousIndex) {
// stop collecting empty matches
break;
}
const result = m.findLast((value) => !!value);
if (result) {
// only add result if non-empty
results.push(result);
}
previousIndex = index;
}
});
return result;
return results;
},
words: (text, start, end) => {
if (Array.isArray(text)) {
Expand Down
12 changes: 12 additions & 0 deletions packages/helix-shared-indexer/test/index-resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ indices:
select: div:foobar([class="embed"])
value: |
textContent(el)
first-alternate:
select: main > div:last-of-type > p:nth-of-type(2)
value: |
match(innerHTML(el), '(.*?)<br><br>.*|(.*)')
second-alternate:
select: main > div:last-of-type > p:nth-of-type(3)
value: |
match(innerHTML(el), '(.*?)<br><br>.*|(.*)')
`;

const BODY = `
Expand Down Expand Up @@ -149,6 +157,8 @@ const BODY = `
</div>
<div class="default">
<p>Topics: A, B, C,</p>
<p>before<br><br>after</p>
<p>before<br>after</p>
</div>
</main>
<footer></footer>
Expand All @@ -166,6 +176,8 @@ describe('Index Resource Tests', () => {
'call-unknown-function': '',
'condition-unsupported': '',
date: 44313,
'first-alternate': 'before',
'second-alternate': 'before<br>after',
'last-modified': 1614007680,
'last-modified-raw': 'Mon, 22 Feb 2021 15:28:00 GMT',
'match-simple': '',
Expand Down

0 comments on commit 94c7e93

Please sign in to comment.