Skip to content

Commit

Permalink
[Cases] Fix remark stringify version to match remark parse (#119995) (#…
Browse files Browse the repository at this point in the history
…120849)

* match parse and stringify version. try/catch added

* Adding tests and refactoring logError

* Adding relative path to core and kibana utils

* remark curstom serializers adapted to version 8

* add error logging to comments  migration

* Adding tests for mergeMigrationFunctionMap logging

Co-authored-by: Jonathan Buttner <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
# Conflicts:
#	x-pack/plugins/cases/server/saved_object_types/migrations/comments.ts

Co-authored-by: Sergi Massaneda <[email protected]>
  • Loading branch information
jonathan-buttner and semd authored Dec 8, 2021
1 parent b425b06 commit 887d0b7
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 165 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
"redux-thunks": "^1.0.0",
"regenerator-runtime": "^0.13.3",
"remark-parse": "^8.0.3",
"remark-stringify": "^9.0.0",
"remark-stringify": "^8.0.3",
"require-in-the-middle": "^5.1.0",
"reselect": "^4.0.0",
"resize-observer-polyfill": "^1.5.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { Plugin } from 'unified';
import type { TimeRange } from 'src/plugins/data/common';
import { LENS_ID } from './constants';

Expand All @@ -13,8 +14,13 @@ export interface LensSerializerProps {
timeRange: TimeRange;
}

export const LensSerializer = ({ timeRange, attributes }: LensSerializerProps) =>
const serializeLens = ({ timeRange, attributes }: LensSerializerProps) =>
`!{${LENS_ID}${JSON.stringify({
timeRange,
attributes,
})}}`;

export const LensSerializer: Plugin = function () {
const Compiler = this.Compiler;
Compiler.prototype.visitors.lens = serializeLens;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
* 2.0.
*/

import { Plugin } from 'unified';
export interface TimelineSerializerProps {
match: string;
}

export const TimelineSerializer = ({ match }: TimelineSerializerProps) => match;
const serializeTimeline = ({ match }: TimelineSerializerProps) => match;

export const TimelineSerializer: Plugin = function () {
const Compiler = this.Compiler;
Compiler.prototype.visitors.timeline = serializeTimeline;
};
88 changes: 88 additions & 0 deletions x-pack/plugins/cases/common/utils/markdown_plugins/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,93 @@ describe('markdown utils', () => {
const parsed = parseCommentString('hello\n');
expect(stringifyMarkdownComment(parsed)).toEqual('hello\n');
});

// This check ensures the version of remark-stringify supports tables. From version 9+ this is not supported by default.
it('parses and stringifies github formatted markdown correctly', () => {
const parsed = parseCommentString(`| Tables | Are | Cool |
|----------|:-------------:|------:|
| col 1 is | left-aligned | $1600 |
| col 2 is | centered | $12 |
| col 3 is | right-aligned | $1 |`);

expect(stringifyMarkdownComment(parsed)).toMatchInlineSnapshot(`
"| Tables | Are | Cool |
| -------- | :-----------: | ----: |
| col 1 is | left-aligned | $1600 |
| col 2 is | centered | $12 |
| col 3 is | right-aligned | $1 |
"
`);
});

it('parses a timeline url', () => {
const timelineUrl =
'[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))';

const parsedNodes = parseCommentString(timelineUrl);

expect(parsedNodes).toMatchInlineSnapshot(`
Object {
"children": Array [
Object {
"match": "[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))",
"position": Position {
"end": Object {
"column": 138,
"line": 1,
"offset": 137,
},
"indent": Array [],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "timeline",
},
],
"position": Object {
"end": Object {
"column": 138,
"line": 1,
"offset": 137,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`);
});

it('stringifies a timeline url', () => {
const timelineUrl =
'[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))';

const parsedNodes = parseCommentString(timelineUrl);

expect(stringifyMarkdownComment(parsedNodes)).toEqual(`${timelineUrl}\n`);
});

it('parses a lens visualization', () => {
const lensVisualization =
'!{lens{"timeRange":{"from":"now-7d","to":"now","mode":"relative"},"attributes":{"title":"TEst22","type":"lens","visualizationType":"lnsMetric","state":{"datasourceStates":{"indexpattern":{"layers":{"layer1":{"columnOrder":["col2"],"columns":{"col2":{"dataType":"number","isBucketed":false,"label":"Count of records","operationType":"count","scale":"ratio","sourceField":"Records"}}}}}},"visualization":{"layerId":"layer1","accessor":"col2"},"query":{"language":"kuery","query":""},"filters":[]},"references":[{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-current-indexpattern"},{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-layer-layer1"}]}}}';

const parsedNodes = parseCommentString(lensVisualization);
expect(parsedNodes.children[0].type).toEqual('lens');
});

it('stringifies a lens visualization', () => {
const lensVisualization =
'!{lens{"timeRange":{"from":"now-7d","to":"now","mode":"relative"},"attributes":{"title":"TEst22","type":"lens","visualizationType":"lnsMetric","state":{"datasourceStates":{"indexpattern":{"layers":{"layer1":{"columnOrder":["col2"],"columns":{"col2":{"dataType":"number","isBucketed":false,"label":"Count of records","operationType":"count","scale":"ratio","sourceField":"Records"}}}}}},"visualization":{"layerId":"layer1","accessor":"col2"},"query":{"language":"kuery","query":""},"filters":[]},"references":[{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-current-indexpattern"},{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-layer-layer1"}]}}}';

const parsedNodes = parseCommentString(lensVisualization);

expect(stringifyMarkdownComment(parsedNodes)).toEqual(`${lensVisualization}\n`);
});
});
});
21 changes: 7 additions & 14 deletions x-pack/plugins/cases/common/utils/markdown_plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,13 @@ export const parseCommentString = (comment: string) => {
export const stringifyMarkdownComment = (comment: MarkdownNode) =>
unified()
.use([
[
remarkStringify,
{
allowDangerousHtml: true,
handlers: {
/*
because we're using rison in the timeline url we need
to make sure that markdown parser doesn't modify the url
*/
timeline: TimelineSerializer,
lens: LensSerializer,
},
},
],
[remarkStringify],
/*
because we're using rison in the timeline url we need
to make sure that markdown parser doesn't modify the url
*/
LensSerializer,
TimelineSerializer,
])
.stringify(comment);

Expand Down
Loading

0 comments on commit 887d0b7

Please sign in to comment.