Skip to content

Commit

Permalink
feat: support characters function (#1022)
Browse files Browse the repository at this point in the history
* feat: support characters function

* fix: support negative numbers
  • Loading branch information
dominique-pfister authored Nov 7, 2024
1 parent 76785d2 commit 1f4e6f2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
40 changes: 40 additions & 0 deletions packages/helix-shared-indexer/src/index-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,37 @@ import { toText } from 'hast-util-to-text';
import { toHtml } from 'hast-util-to-html';
import { unified } from 'unified';

/**
* Returns the substring from `start` to `end` of the given text. If `start` or `end` are
* negative, they address the position counted from the end of the text. `end` is optional,
* and defaults to the length of the text.
*
* Examples:
* - characters('hello, world.', 0, 5) = 'hello'
* - characters('hello, world.', 7) = 'world.'
* - characters('hello, world.', -6, -1) = 'world'
*
* @param {string|Array<string>} text
* @param {number} start The start position.
* @param {number} [end] The end position.
* @returns {Array<string>}
*/
const characters = (text, start, end) => {
if (Array.isArray(text)) {
// eslint-disable-next-line no-param-reassign
text = text.join(' ');
}
if (start < 0) {
// eslint-disable-next-line no-param-reassign
start += text.length;
}
if (end <= 0) {
// eslint-disable-next-line no-param-reassign
end += text.length;
}
return [text.substring(start, end)];
};

const helpers = {
dateValue: (elements, format) => {
const result = helpers.parseTimestamp(elements, format);
Expand Down Expand Up @@ -93,6 +124,7 @@ const helpers = {
}
return [text.split(/\s+/g).slice(start, end).join(' ')];
},
characters,
replace: (s, searchValue, replaceValue) => [s.replace(searchValue, replaceValue)],
replaceAll: (s, searchValue, replaceValue) => [s.replaceAll(searchValue, replaceValue)],
};
Expand Down Expand Up @@ -129,6 +161,14 @@ function evaluate(expression, context) {
case Jsep.LITERAL: {
return node.value;
}
case Jsep.UNARY_EXP: {
const value = evalNode(node.argument);
if (node.operator === '-') {
return -value;
}
log.warn('operator not supported: ', node.operator);
return undefined;
}
default: {
log.warn('evaluate type not supported: ', node.type);
}
Expand Down
48 changes: 36 additions & 12 deletions packages/helix-shared-indexer/test/index-resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,38 @@ indices:
select: main > div:nth-of-type(5)
value: |
innerHTML(el)
teaser:
words:
select: main > div:nth-child(n+4) p
value: |
words(textContent(el), 0, 20)
last-modified:
non-array-words:
select: none
value: |
parseTimestamp(headers['last-modified'], 'ddd, DD MMM YYYY hh:mm:ss GMT')
last-modified-raw:
words(headers['last-modified'])
characters:
select: main > div:nth-child(n+4) p
value: |
characters(textContent(el), 0, 20)
characters-negative-start:
select: main > div:nth-child(n+4) p
value: |
characters(textContent(el), -5)
characters-negative-end:
select: main > div:nth-child(n+4) p
value: |
characters(textContent(el), -5, -1)
non-array-characters:
select: none
value: |
headers['last-modified']
non-array-words:
characters(headers['last-modified'], 0, 5)
parse-timestamp:
select: none
value: |
words(headers['last-modified'])
parseTimestamp(headers['last-modified'], 'ddd, DD MMM YYYY hh:mm:ss GMT')
headers-last-modified:
select: none
value: |
headers['last-modified']
match-simple:
select: meta[name="x-source-hash"]
value: |
Expand Down Expand Up @@ -124,6 +140,9 @@ indices:
selectFirst: foo
value: |
attribute(el, 'href')
unknown-operator:
select: none
value: '+5'
`;

const BODY = `
Expand Down Expand Up @@ -193,31 +212,36 @@ describe('Index Resource Tests', () => {
author: 'Max',
'bad-selector': '',
'call-unknown-function': '',
characters: 'Lorem ipsum dolor si',
'characters-negative-start': 'after',
'characters-negative-end': 'afte',
'condition-unsupported': '',
date: 44313,
'first-alternate': 'before',
'first-href-in-a': 'https://my.domain.com/assets/asset-link',
'first-href-in-foo': '',
'second-alternate': 'before<br>after',
'last-modified': 1614007680,
'last-modified-raw': 'Mon, 22 Feb 2021 15:28:00 GMT',
'first-segment': 'abc',
'headers-last-modified': 'Mon, 22 Feb 2021 15:28:00 GMT',
'match-simple': '',
'member-unknown-var': '',
'member-without-get': '',
'missing-header': '',
'non-array-characters': 'Mon, ',
'non-array-words': 'Mon, 22 Feb 2021 15:28:00 GMT',
paragraph: '\n <p>consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>\n ',
'first-segment': 'abc',
'parse-timestamp': 1614007680,
'replace-path': '/zc/de/ab/fg/abcd',
'replaceAll-path': '/zc/de/z/fg/zcd',
'second-alternate': 'before<br>after',
sourceHash: 'JJYxCM1NDG4ahJm9f',
teaser: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut',
title: 'I feel good',
topics: [
'A',
'B',
'C',
],
'unknown-operator': '',
words: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut',
});
});
});

0 comments on commit 1f4e6f2

Please sign in to comment.