Skip to content

Commit 4b26ca5

Browse files
committed
feat: source youtube
1 parent 3210c0e commit 4b26ca5

File tree

8 files changed

+239
-169
lines changed

8 files changed

+239
-169
lines changed

demo/gatsby-config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module.exports = {
33
{
44
resolve: "@raae/gatsby-source-youtube-oembed",
55
options: {
6-
emoji: "pirate",
7-
message: "Hello!!!",
6+
youTubeIds: ["Bk1jonYPFD4", "TzJfepDjpzM"],
87
},
98
},
109
],

demo/src/pages/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
2+
import { graphql } from "gatsby";
23

3-
const IndexPage = () => {
4+
const IndexPage = ({ data }) => {
45
return (
56
<main>
67
<header>
@@ -13,9 +14,22 @@ const IndexPage = () => {
1314
&nbsp;🎉
1415
</span>
1516
</h1>
17+
<pre>{JSON.stringify(data, null, 4)}</pre>
1618
</header>
1719
</main>
1820
);
1921
};
2022

23+
export const query = graphql`
24+
{
25+
allYouTube {
26+
nodes {
27+
oEmbed {
28+
title
29+
}
30+
}
31+
}
32+
}
33+
`;
34+
2135
export default IndexPage;

plugin/constants.js

-5
This file was deleted.

plugin/gatsby-browser.js

-10
This file was deleted.

plugin/gatsby-node.js

+49-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
11
// https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
22

3-
const { EMOJIS } = require("./constants");
4-
5-
const validKeys = Object.keys(EMOJIS);
3+
const axios = require("axios");
4+
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
65

76
exports.pluginOptionsSchema = ({ Joi }) => {
87
return Joi.object({
9-
message: Joi.string().default("Hello from the Plugin"),
10-
emoji: Joi.string()
11-
.valid(...validKeys)
12-
.default(validKeys[0])
13-
.description(`Select between the emoji options`),
8+
youTubeIds: Joi.array().items(Joi.string()),
9+
});
10+
};
11+
12+
const fetchEmbed = async (id) => {
13+
const ytUrl = `https://youtu.be/${id}`;
14+
const { data } = await axios.get("https://www.youtube.com/oembed", {
15+
params: {
16+
url: ytUrl,
17+
},
1418
});
19+
return data;
20+
};
21+
22+
const prepYouTubeNode = async (gatsbyUtils, id) => {
23+
const {
24+
actions: { createNode },
25+
getCache,
26+
createNodeId,
27+
createContentDigest,
28+
} = gatsbyUtils;
29+
30+
const youTubeNodeId = createNodeId(`you-tube-${id}`);
31+
const embedData = await fetchEmbed(id);
32+
33+
const imageFile = await createRemoteFileNode({
34+
// The source url of the remote file
35+
url: embedData.thumbnail_url,
36+
parentNodeId: youTubeNodeId,
37+
getCache,
38+
createNode,
39+
createNodeId,
40+
});
41+
42+
createNode({
43+
id: youTubeNodeId,
44+
oEmbed: embedData,
45+
thumnail___NODE: imageFile.id,
46+
internal: {
47+
type: `YouTube`,
48+
contentDigest: createContentDigest(embedData),
49+
},
50+
});
51+
};
52+
53+
exports.sourceNodes = async (gatsbyUtils, pluginOptions) => {
54+
const { youTubeIds = [] } = pluginOptions;
55+
await Promise.all(youTubeIds.map((id) => prepYouTubeNode(gatsbyUtils, id)));
1556
};

plugin/gatsby-ssr.js

-1
This file was deleted.

plugin/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
},
2525
"homepage": "https://github.com/queen-raae/gatsby-source-youtube-oembed#readme",
2626
"devDependencies": {},
27-
"dependencies": {},
27+
"dependencies": {
28+
"axios": "0.25.0",
29+
"gatsby-source-filesystem": "4.6.0"
30+
},
2831
"peerDependencies": {
2932
"gatsby": "^3.0.0 || ^4.0.0"
3033
}

0 commit comments

Comments
 (0)