Skip to content

Commit

Permalink
Resolve urls with either the github or octolinker API
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Buck committed Feb 15, 2019
1 parent f6fc910 commit 29e62ce
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
119 changes: 86 additions & 33 deletions packages/core/loader.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,101 @@
function createStore(json, payload) {
const store = global.__ocotlinker_cache || {};
import { fetchTree } from '@octolinker/helper-github-api';
import { bulkAction } from '@octolinker/helper-octolinker-api';

payload.forEach(({ registry, target }, index) => {
store[registry] = store[registry] || {};
store[registry][target] = json[index];
});
function matchFor({ urls }, types) {
return urls.every(url => url.type.includes(types));
}

global.__ocotlinker_cache = store;
function filterLiveResolver(matches) {
return matches.reduce(
(memo, match) => {
if (matchFor(match, ['registry', 'ping'])) {
// Match contains just external urls
memo.external.push(match, ['internal-link']);
} else if (matchFor(match)) {
// Match contains just internal urls
memo.internal.push(match);
} else {
// Match contains both internal and external urls
memo.internal.push(match);
memo.external.push(match);
}

return memo;
},
{
external: [],
internal: [],
},
);
}
function getRepoMetadata(data) {
// Find first internal links which contains the information we need
const result = data.find(item =>
item.urls.find(({ type }) => type === 'internal-link'),
);

async function runLiveQuery(matches) {
if (!matches.length) {
return [];
if (result && result.urls[0]) {
const { user, repo, branch } = result.urls[0];
return { user, repo, branch };
}

const payload = [].concat(...matches.map(match => match.urls));
return {};
}

const response = await fetch('https://githublinker.herokuapp.com/bulk', {
method: 'POST',
body: JSON.stringify(payload),
headers: new Headers({
'Content-Type': 'application/json',
}),
});
const json = await response.json();
async function resolveInternalLinks(data) {
const { user, repo, branch } = getRepoMetadata(data);

createStore(json, payload);
}
let tree = [];
if (user && repo && branch) {
tree = await fetchTree({ user, repo, branch });
}

function matchContainsOnlyRegistyMatches(match) {
return match.urls.every(url => url.type === 'registry');
}
for (const { urls, link } of data) {
if (link.href) {
// Return early if link is already set
return;
}

function filterLiveResolver(matches) {
return matches.reduce((memo, match) => {
if (matchContainsOnlyRegistyMatches(match)) {
memo.push(match);
for (const item of urls) {
if (item.type === 'internal-link' && tree.includes(item.path)) {
link.href = item.url;
break;
}
}
}
}

async function resolveExternalLinks(data) {
const response = await bulkAction(data);

return memo;
}, []);
data.forEach(({ link, urls }) => {
urls.forEach(url => {
if (link.href) {
// Return early if link is already set
return;
}

try {
const finalUrl = response.find(
({ type, target }) =>
(type === url.registry || type === url.type) &&
target === url.target,
);

if (finalUrl && finalUrl.result) {
link.href = finalUrl.result;
}
} catch (error) {
// error
}
});
});
}

export default function(matches) {
const registryMatch = filterLiveResolver(matches);
runLiveQuery(registryMatch);
export default async function(matches) {
const { external, internal } = filterLiveResolver(matches);
console.log(matches);

resolveExternalLinks(external);
resolveInternalLinks(internal);
}
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"main": "./index.js",
"dependencies": {
"@octolinker/blob-reader": "1.0.0",
"@octolinker/helper-github-api": "1.0.0",
"@octolinker/helper-octolinker-api": "1.0.0",
"@octolinker/helper-insert-link": "1.0.0",
"@octolinker/helper-settings": "1.0.0",
"@octolinker/helper-sort-urls": "1.0.0",
Expand Down

0 comments on commit 29e62ce

Please sign in to comment.