Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaPolit committed Jun 7, 2017
2 parents a144a5a + 5b92524 commit d79e21a
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/react/Documents/components/SearchText.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {t} from 'app/I18N';
import {Field, LocalForm} from 'react-redux-form';

export class SearchText extends Component {
search(){}
resetSearch(){}
search() {}
resetSearch() {}

render() {
const {snippets} = this.props;
Expand Down
2 changes: 1 addition & 1 deletion app/react/Entities/components/EntityViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class EntityViewer extends Component {
entityBeingEdited={entityBeingEdited} />
</ShowIf>

<SidePanel className={"entity-connections entity-" + this.props.tab} open={this.props.sidepanelOpen}>
<SidePanel className={'entity-connections entity-' + this.props.tab} open={this.props.sidepanelOpen}>
<ShowIf if={selectedTab === 'info' || selectedTab === 'connections'}>
<div className="sidepanel-footer">
<ResetSearch />
Expand Down
8 changes: 4 additions & 4 deletions app/react/I18N/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ let t = (contextId, key, _text) => {
let context = translation.contexts.find((ctx) => ctx.id === contextId) || {values: {}};

if (!context.values) {
console.log(contextId);
console.log(key);
console.log(_text);
console.log(context);
console.log(contextId); // eslint-disable-line no-console
console.log(key); // eslint-disable-line no-console
console.log(_text); // eslint-disable-line no-console
console.log(context); // eslint-disable-line no-console
}

if (contextId === 'System' && !context.values[key]) {
Expand Down
2 changes: 1 addition & 1 deletion app/react/Library/components/UploadEntityStatus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PropTypes from 'prop-types';
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {ItemFooter} from 'app/Layout/Lists';
import {connect} from 'react-redux';
Expand Down
14 changes: 12 additions & 2 deletions app/react/Pages/PageView.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import rison from 'rison';

import RouteHandler from 'app/App/RouteHandler';
import api from 'app/Search/SearchAPI';
import PagesAPI from './PagesAPI';
import TemplatesAPI from 'app/Templates/TemplatesAPI';
import ThesaurisAPI from 'app/Thesauris/ThesaurisAPI';
import {actions} from 'app/BasicReducer';
import queryString from 'query-string';

import PageViewer from './components/PageViewer';
import pageItemLists from './utils/pageItemLists';
Expand All @@ -15,7 +15,17 @@ function prepareLists(page) {
const listsData = pageItemLists.generate(page.metadata.content);

listsData.searchs = listsData.params.map(params => {
let query = params ? queryString.parse(params) : {filters: {}, types: []};
const sanitizedParams = params ? decodeURI(params) : '';
const queryDefault = {filters: {}, types: []};
let query = queryDefault;

if (sanitizedParams) {
query = rison.decode(sanitizedParams.replace('?q=', '') || '()');
if (typeof query !== 'object') {
query = queryDefault;
}
}

query.limit = '6';
return api.search(query);
});
Expand Down
17 changes: 10 additions & 7 deletions app/react/Pages/specs/PageView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('PageView', () => {
spyOn(ThesaurisAPI, 'get').and.returnValue(Promise.resolve('thesauris'));
spyOn(pageItemLists, 'generate').and.returnValue({
content: 'parsedContent',
params: ['?a=1&b=2', '', '?x=1&y=2&limit=24']
params: ['?q=(a:1,b:2)', '', '?q=(x:1,y:!(%27array%27),limit:24)', '?order=metadata.form&treatAs=number']
});

RouteHandler.renderedFromServer = true;
Expand All @@ -56,19 +56,22 @@ describe('PageView', () => {
it('should request each list inside the content limited to 6 items and set the state', (done) => {
PageView.requestState({pageId: 'abc2'})
.then((response) => {
expect(api.search.calls.count()).toBe(3);
expect(JSON.parse(JSON.stringify(api.search.calls.argsFor(0)[0]))).toEqual({a: '1', b: '2', limit: '6'});
expect(api.search.calls.count()).toBe(4);
expect(JSON.parse(JSON.stringify(api.search.calls.argsFor(0)[0]))).toEqual({a: 1, b: 2, limit: '6'});
expect(api.search.calls.argsFor(1)[0]).toEqual({filters: {}, types: [], limit: '6'});
expect(JSON.parse(JSON.stringify(api.search.calls.argsFor(2)[0]))).toEqual({x: '1', y: '2', limit: '6'});
expect(JSON.parse(JSON.stringify(api.search.calls.argsFor(2)[0]))).toEqual({x: 1, y: ['array'], limit: '6'});
expect(api.search.calls.argsFor(3)[0]).toEqual({filters: {}, types: [], limit: '6'});

expect(response.page.itemLists.length).toBe(3);
expect(response.page.itemLists.length).toBe(4);

expect(response.page.itemLists[0].params).toBe('?a=1&b=2');
expect(response.page.itemLists[0].params).toBe('?q=(a:1,b:2)');
expect(response.page.itemLists[0].items).toEqual(['resultsFor:0']);
expect(response.page.itemLists[1].params).toBe('');
expect(response.page.itemLists[1].items).toEqual(['resultsFor:1']);
expect(response.page.itemLists[2].params).toBe('?x=1&y=2&limit=24');
expect(response.page.itemLists[2].params).toBe('?q=(x:1,y:!(%27array%27),limit:24)');
expect(response.page.itemLists[2].items).toEqual(['resultsFor:2']);
expect(response.page.itemLists[3].params).toBe('?order=metadata.form&treatAs=number');
expect(response.page.itemLists[3].items).toEqual(['resultsFor:3']);
done();
})
.catch(done.fail);
Expand Down
16 changes: 9 additions & 7 deletions app/react/Pages/utils/pageItemLists.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import markdownEscapedValues from 'app/utils/markdownEscapedValues';

const listPlaceholder = '{---UWAZILIST---}';
const listEscape = '{list}';

export default {
generate: (originalText) => {
const listMatch = /{list}\((.*?)\)/g;
const params = [];
const originalContent = originalText || '';
const values = markdownEscapedValues(originalText, '(...)', listEscape);

const content = originalContent.replace(listMatch, (_, list) => {
const listParams = /\?(.*)/g.exec(list);
params.push(listParams ? listParams[0] : '');
return listPlaceholder;
let content = originalText || '';
const params = values.map(match => {
content = content.replace(`${listEscape}(${match})`, listPlaceholder);
const urlParams = /\?(.*)/g.exec(match);
return urlParams ? urlParams[0] : '';
});

return {params, content};
Expand Down
11 changes: 7 additions & 4 deletions app/react/Pages/utils/specs/pageItemLists.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-len */

import pageLists from '../pageItemLists';

describe('Pages: pageItemLists util', () => {
Expand All @@ -6,10 +8,10 @@ describe('Pages: pageItemLists util', () => {
beforeEach(() => {
content = '## title\nSome text with a [URL](http://google.com) inside.' +
'\n\n{list}(http://someurl:3000/es/?parameters=values)' +
'\n\nWhich should be in its own line, separated with TWO line breaks (to create a new <p> Element)' +
'\n\nWhich should be in its own line, "separated" with TWO line breaks (to create a new <p> Element)' +
'\n\n{list}(http://someurl:3000/es/)' +
'\n\nAnd should allow multiple lists with different values' +
'\n\n{list}(http://anotherurl:4000/es/?different=parameters)' +
'\n\n{list}(https://cejil.uwazi.io/es/library/?q=(filters:(mandatos_de_la_corte:(from:1496620800)),order:asc,sort:title,types:!(%2758b2f3a35d59f31e1345b4b6%27)))' +
'\n\n{list}(http://anotherurl:5000/es/?a=b)' +
'\n\n```javascript\nCode\n```';
});
Expand All @@ -19,14 +21,15 @@ describe('Pages: pageItemLists util', () => {
expect(params.length).toBe(4);
expect(params[0]).toBe('?parameters=values');
expect(params[1]).toBe('');
expect(params[2]).toBe('?different=parameters');
expect(params[2]).toBe('?q=(filters:(mandatos_de_la_corte:(from:1496620800)),order:asc,sort:title,types:!(%2758b2f3a35d59f31e1345b4b6%27))');
expect(params[3]).toBe('?a=b');
});

it('should return the content with list placeholders', () => {
const newContent = pageLists.generate(content).content;
expect(newContent).toContain('{---UWAZILIST---}');
expect(newContent).not.toContain('?different=parameters');
expect(newContent).not.toContain('?parameters=values');
expect(newContent).not.toContain('order:asc,sort:title,types:!(%2758b2f3a35d59f31e1345b4b6%27)');
});

it('should return empty if no content', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-nested-callbacks */
import backend from 'fetch-mock';
import {APIURL} from 'app/config.js';
import {actions as formActions} from 'react-redux-form';
Expand Down
1 change: 1 addition & 0 deletions app/react/Viewer/actions/specs/documentActions.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-nested-callbacks */
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import backend from 'fetch-mock';
Expand Down
50 changes: 50 additions & 0 deletions app/react/utils/markdownEscapedValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable max-depth */

// Adapted from http://blog.stevenlevithan.com/archives/javascript-match-nested
export default (function () {
const formatParts = /^([\S\s]+?)\.\.\.([\S\s]+)/;
const metaChar = /[-[\]{}()*+?.\\^$|,]/g;
const escape = function (str) {
return str.replace(metaChar, '\\$&');
};

return function (str, format, escapeCode = '') {
const p = formatParts.exec(format);
if (!p) {
throw new Error('format must include start and end tokens separated by "..."');
}

if (p[1] === p[2]) {
throw new Error('start and end format tokens cannot be identical');
}

const opener = p[1];
const closer = p[2];
const iterator = new RegExp(format.length === 5 ? '[' + escape(opener + closer) + ']' : escape(opener) + '|' + escape(closer), 'g');
const results = [];
let openTokens;
let matchStartIndex;
let match;

do {
openTokens = 0;
while ((match = iterator.exec(str)) !== null) {
if (match[0] === opener) {
if (!openTokens) {
matchStartIndex = iterator.lastIndex;
}
openTokens += 1;
} else if (openTokens) {
openTokens -= 1;
if (!openTokens) {
if (str.slice(matchStartIndex - escapeCode.length - opener.length, matchStartIndex - opener.length) === escapeCode) {
results.push(str.slice(matchStartIndex, match.index));
}
}
}
}
} while (openTokens && (iterator.lastIndex = matchStartIndex));

return results;
};
}());
24 changes: 24 additions & 0 deletions app/react/utils/specs/markdownEscapedValues.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import markdownEscapedValues from '../markdownEscapedValues';

describe('markdownEscapedValues', () => {
it('should return an emtpy array when no match found', () => {
expect(markdownEscapedValues(null, '(...)')).toEqual([]);
expect(markdownEscapedValues('', '(...)')).toEqual([]);
expect(markdownEscapedValues('Unmatched text', '(...)')).toEqual([]);
});

it('should extract found matches, even in nested configurations', () => {
expect(markdownEscapedValues('This is a (match)', '(...)')).toEqual(['match']);

expect(markdownEscapedValues('This should also (match(as(nested(parenthesis),with more data)))', '(...)'))
.toEqual(['match(as(nested(parenthesis),with more data))']);
});

it('should extract matches with custom escape code only, and avoid other type of escapes that may use similar patters', () => {
expect(markdownEscapedValues('This {should}(not match), this is a {a}(match), this other [a](should not)', '(...)', '{a}'))
.toEqual(['match']);

expect(markdownEscapedValues('This {should}(not match), this is a {a}(should(match)), this other {a}(too)', '(...)', '{a}'))
.toEqual(['should(match)', 'too']);
});
});
4 changes: 2 additions & 2 deletions database/elastic_mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export default {
path_match: 'fullText',
match_mapping_type: 'string',
mapping: {
type: 'string',
type: 'text',
index: 'analyzed',
omit_norms: true,
analyzer: 'standard',
fielddata: {format: 'enabled'}
term_vector: 'with_positions_offsets'
}
}
}, {
Expand Down

0 comments on commit d79e21a

Please sign in to comment.