Skip to content

Commit

Permalink
feat: add query params support to metaInfoBaseUrls (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
mb1te authored Aug 18, 2021
1 parent 491787f commit 8af41d0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
19 changes: 17 additions & 2 deletions lib/static/components/section/body/meta-info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path';
import url from 'url';
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
Expand All @@ -15,13 +14,29 @@ const mkLinkToUrl = (url, text = url) => {

const serializeMetaValues = (metaInfo) => mapValues(metaInfo, (v) => isObject(v) ? JSON.stringify(v) : v);

const resolveUrl = (baseUrl, value) => {
const parsedBaseUrl = new URL(baseUrl);
const baseSearchParams = new URLSearchParams(parsedBaseUrl.search);
if (baseSearchParams) {
parsedBaseUrl.search = '';
}

const resolvedUrl = new URL(value, parsedBaseUrl.href);

for (let [key, value] of baseSearchParams) {
resolvedUrl.searchParams.append(key, value);
}

return resolvedUrl.href;
};

const metaToElements = (metaInfo, metaInfoBaseUrls) => {
return map(metaInfo, (value, key) => {
if (isUrl(value)) {
value = mkLinkToUrl(value);
} else if (metaInfoBaseUrls[key]) {
const baseUrl = metaInfoBaseUrls[key];
const link = isUrl(baseUrl) ? url.resolve(baseUrl, value) : path.join(baseUrl, value);
const link = isUrl(baseUrl) ? resolveUrl(baseUrl, value) : path.join(baseUrl, value);
value = mkLinkToUrl(link, value);
} else if (typeof value === 'boolean') {
value = value.toString();
Expand Down
54 changes: 50 additions & 4 deletions test/unit/lib/static/components/section/body/meta-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,71 @@ describe('<MetaInfo />', () => {
[
{
type: 'path',
metaInfo: {
file: 'test/file'
},
metaInfoBaseUrls: {file: 'base/path'},
expectedFileUrl: 'base/path/test/file'
},
{
type: 'url',
metaInfo: {
file: 'test/file'
},
metaInfoBaseUrls: {file: 'http://127.0.0.1'},
expectedFileUrl: 'http://127.0.0.1/test/file'
},
{
type: 'url with one query param',
metaInfo: {
file: 'test/file'
},
metaInfoBaseUrls: {
file: 'http://127.0.0.1?a=b'
},
expectedFileUrl: 'http://127.0.0.1/test/file?a=b'
},
{
type: 'url with few query params',
metaInfo: {
file: 'test/file'
},
metaInfoBaseUrls: {
file: 'http://127.0.0.1?a=b&b=c&c=d'
},
expectedFileUrl: 'http://127.0.0.1/test/file?a=b&b=c&c=d'
},
{
type: 'url with pathname',
metaInfo: {
file: 'test/file'
},
metaInfoBaseUrls: {
file: 'http://127.0.0.1/path/'
},
expectedFileUrl: 'http://127.0.0.1/path/test/file'
},
{
type: 'url with query params and meta value with query params',
metaInfo: {
file: 'test/file?a=b'
},
metaInfoBaseUrls: {
file: 'http://127.0.0.1/?c=d&e=f'
},
expectedFileUrl: 'http://127.0.0.1/test/file?a=b&c=d&e=f'
}
].forEach((stub) => {
it(`should render link in meta info based upon metaInfoBaseUrls ${stub.type}`, () => {
const initialConfig = {config: {metaInfoBaseUrls: stub.metaInfoBaseUrls}};
const result = {
metaInfo: {file: 'test/file'}
metaInfo: stub.metaInfo
};
const initialConfig = {config: {metaInfoBaseUrls: stub.metaInfoBaseUrls}};

const component = mkMetaInfoComponent({result}, initialConfig);
component.debug();
component.find('.details__summary').simulate('click');

assert.equal(component.find('.meta-info__item:first-child').text(), 'file: test/file');
assert.equal(component.find('.meta-info__item:first-child').text(), `file: ${stub.metaInfo.file}`);
assert.equal(component.find('.meta-info__item:first-child a').prop('href'), stub.expectedFileUrl);
});
});
Expand Down

0 comments on commit 8af41d0

Please sign in to comment.