Skip to content

Commit

Permalink
[TSVB] Fix more than one empty labels in markdown breaks the values f…
Browse files Browse the repository at this point in the history
…or all other labels (#100432)

* Fixed multiple empty labels monitoring with [].

* Fixed regexp group behavior and replaced by ''.replaceAll.

* Changed ''.replaceAll with ''.split(...).join(...)

* Added test for (empty) label case.

* Removed not necessary comment.

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Kuznietsov and kibanamachine authored May 25, 2021
1 parent 7bd46ec commit 818fa90
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { i18n } from '@kbn/i18n';
export function replaceVars(str, args = {}, vars = {}) {
try {
// we need add '[]' for emptyLabel because this value contains special characters. (https://handlebarsjs.com/guide/expressions.html#literal-segments)
const template = handlebars.compile(str.replace(emptyLabel, `[${emptyLabel}]`), {
const template = handlebars.compile(str.split(emptyLabel).join(`[${emptyLabel}]`), {
strict: true,
knownHelpersOnly: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { replaceVars } from './replace_vars';
import { emptyLabel } from '../../../../common/empty_label';

describe('replaceVars(str, args, vars)', () => {
test('replaces vars with values', () => {
Expand All @@ -27,4 +28,11 @@ describe('replaceVars(str, args, vars)', () => {
const template = '# {{args.host}} {{total';
expect(replaceVars(template, args, vars)).toEqual('# {{args.host}} {{total');
});

test('replaces (empty).some_path with values', () => {
const vars = { [emptyLabel]: { d: { raw: 100 } } };
const args = {};
const template = `# {{ ${emptyLabel}.d.raw }} {{ ${emptyLabel}.d.raw }}`;
expect(replaceVars(template, args, vars)).toEqual('# 100 100');
});
});

0 comments on commit 818fa90

Please sign in to comment.