Skip to content

Commit

Permalink
Schemas #5 (#21)
Browse files Browse the repository at this point in the history
* Refactored ammend code into its own function.

* Added possibility to amend provider list. Adding support for YouTube and https Flickr.

* Added amCharts Live Editor hakcs.

* Added reddit script.

* Added MixCloud and Nasjonalbiblioteket hacks.
  • Loading branch information
raae authored Aug 18, 2018
1 parent ec95e6e commit 3411f27
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
4 changes: 4 additions & 0 deletions gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ exports.onRenderBody = ({ setPostBodyComponents }) =>
<script
key={`gatsby-plugin-oembed-flickr`}
src="https://embedr.flickr.com/assets/client-code.js"
/>,
<script
key={`gatsby-plugin-oembed-flickr`}
src="https://embed.redditmedia.com/widgets/platform.js"
/>
]);
67 changes: 64 additions & 3 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,84 @@ const select = require("unist-util-select");
const axios = require("axios");

const OEMBED_PROVIDERS_URL = "https://oembed.com/providers.json";
const ENDPOINTS = {
YouTube: {
schemes: [
"http://*.youtube.com/watch*",
"http://*.youtube.com/v/*",
"http://youtu.be/*"
],
url: "https://www.youtube.com/oembed"
},
Nasjonalbiblioteket: {
schemes: ["https://www.nb.no/items/*"],
url: "https://api.nb.no/catalog/v1/oembed"
}
};

const ADD_HTTPS_TO_SCHEMES = [
"amCharts Live Editor",
"YouTube",
"Flickr",
"MixCloud"
];
const ADD_HTTPS_TO_ENDPOINT_URL = ["amCharts Live Editor"];

exports.fetchOembedProviders = async () => {
const response = await axios.get(OEMBED_PROVIDERS_URL);
return response.data;
};

exports.ammendProviders = providers => {
const ammendEndpoints = (endpoints = [], providerName) => {
if (ENDPOINTS[providerName]) {
endpoints = endpoints.concat(ENDPOINTS[providerName]);
}
return endpoints;
};

const ammendSchemes = (schemes = [], providerName) => {
if (ADD_HTTPS_TO_SCHEMES.includes(providerName)) {
const httpsSchemes = [...schemes].map(scheme =>
scheme.replace("http", "https")
);
schemes = schemes.concat(httpsSchemes);
}
return schemes;
};

const ammendEndpointUrl = (endpointUrl = "", providerName) => {
endpointUrl = endpointUrl.replace("{format}", "json");
if (ADD_HTTPS_TO_ENDPOINT_URL.includes(providerName)) {
endpointUrl = endpointUrl.replace("http", "https");
}
return endpointUrl;
};

return providers.map(provider => {
const providerName = provider.provider_name;
provider.endpoints = ammendEndpoints(provider.endpoints, providerName).map(
endpoint => {
endpoint.schemes = ammendSchemes(endpoint.schemes, providerName);
endpoint.url = ammendEndpointUrl(endpoint.url, providerName);
return endpoint;
}
);
return provider;
});
};

exports.getProviderEndpointUrlForLinkUrl = (linkUrl, providers) => {
let endpointUrl = false;

for (const provider of providers) {
for (const endpoint of provider.endpoints || []) {
for (let schema of endpoint.schemes || []) {
for (const endpoint of provider.endpoints) {
for (let schema of endpoint.schemes) {
try {
schema = schema.replace("*", ".*");
const regExp = new RegExp(schema);
if (regExp.test(linkUrl)) {
endpointUrl = endpoint.url.replace("{format}", "json");
endpointUrl = endpoint.url;
}
} catch (error) {
console.log(
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Promise = require("bluebird");

const {
ammendProviders,
fetchOembedProviders,
fetchOembed,
getProviderEndpointUrlForLinkUrl,
Expand All @@ -15,7 +16,8 @@ module.exports = async ({ markdownAST }) => {
);
try {
// Step 1. Fetch the oembed provider list.
const providers = await fetchOembedProviders();
let providers = await fetchOembedProviders();
providers = ammendProviders(providers);

// Step 2. Find link nodes in markdown structure that are on their own, not part of some other content.
const possibleOmbedUrlNodes = selectPossibleOembedLinkNodes(markdownAST);
Expand Down

0 comments on commit 3411f27

Please sign in to comment.