|
| 1 | +import Marked from 'marked'; |
| 2 | +import Fetch from 'isomorphic-unfetch'; |
| 3 | + |
| 4 | +const HOUR = 1000 * 60 * 60; |
| 5 | +const dirCache = []; |
| 6 | +const pageCache = []; |
| 7 | + |
| 8 | +function checkCache(cache, path) { |
| 9 | + let entry = null; |
| 10 | + let index = 0; |
| 11 | + let foundIndex = null; |
| 12 | + let found = false; |
| 13 | + |
| 14 | + for (index = 0, found = false; index < cache.length && found === false; index += 1) { |
| 15 | + if (cache[index].path === path) { |
| 16 | + entry = cache[index]; |
| 17 | + found = true; |
| 18 | + foundIndex = index; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + if (!entry) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + if (entry.timestamp + HOUR < Date.now()) { |
| 26 | + cache.splice(foundIndex, 1); // Remove entry |
| 27 | + return false; |
| 28 | + } |
| 29 | + return entry.data; |
| 30 | +} |
| 31 | + |
| 32 | +function addCache(cache, path, data) { |
| 33 | + cache.push({ path, data, timestamp: Date.now() }); |
| 34 | +} |
| 35 | + |
| 36 | +async function getPage(path) { |
| 37 | + const cacheResponse = checkCache(pageCache, path); |
| 38 | + if (cacheResponse) { |
| 39 | + return cacheResponse; |
| 40 | + } |
| 41 | + // Using the raw access rather than API due to the rate limiting |
| 42 | + const uri = `https://raw.githubusercontent.com/codebru/markdown-test-repo/master/${path}`; |
| 43 | + const res = await Fetch(uri); |
| 44 | + const data = await res.text(); |
| 45 | + if (String(data) === '404: Not Found\n') { |
| 46 | + return false; |
| 47 | + } |
| 48 | + const contentMd = data; |
| 49 | + const contentHTML = Marked(contentMd); |
| 50 | + addCache(pageCache, path, contentHTML); |
| 51 | + return contentHTML; |
| 52 | +} |
| 53 | + |
| 54 | +async function getDirectory(path) { |
| 55 | + try { |
| 56 | + const cacheResponse = checkCache(dirCache, path); |
| 57 | + if (cacheResponse) { |
| 58 | + return cacheResponse; |
| 59 | + } |
| 60 | + const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`; |
| 61 | + const res = await Fetch(uri); |
| 62 | + const data = await res.json(); |
| 63 | + if (!data || !Array.isArray(data) || !Object.prototype.hasOwnProperty.call(data[0], 'path')) { |
| 64 | + throw new Error('Failed to fetch tree.'); |
| 65 | + } |
| 66 | + const pathArray = data.map(entry => entry.path); |
| 67 | + addCache(dirCache, path, pathArray); |
| 68 | + return pathArray; |
| 69 | + } catch (e) { |
| 70 | + return `Unable to get data: ${e}`; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export { getPage, getDirectory }; |
0 commit comments