Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Fix package to work with new API requirements #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Editors
.vscode/
.idea/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ plugins: [
resolve:"@weknow/gatsby-remark-twitch",
options: {
width: 800,
height: 400
height: 400,
// Required!
domain: 'example.com' // Also accepts "https://example.com/route
}
}
]
Expand All @@ -45,7 +47,7 @@ https://www.twitch.tv/videos/347319713

#### Twitch clip

https://www.twitch.tv/xisuma/clip/MagnificentOilyUdonTTours
https://clips.twitch.tv/MagnificentOilyUdonTTours

#### Twitch channel

Expand Down
38 changes: 29 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

const visit = require('unist-util-visit');

module.exports = async ({ markdownAST }, options = { width: 600, height: 300 }) => {
module.exports = async ({ markdownAST }, options = { width: 600, height: 300, domain: '' }) => {
if (!options.domain) {
console.error('You must configure a domain for Gatsby Remark Twitch');
return;
}
let parent = options.domain;
// Try & Catch to allow for hosts themselves to be passed
// `new URL('domain.com')` will fail/throw, but is a valid host
try {
const url = new URL(options.domain);
// URLs like 'localhost:3000' might not give host.
// Throw in order to catch in wrapper handler
if (!url.host) throw new Error();
} catch (_) {
const url = new URL('https://' + options.domain);
parent = url.host || parent;
}

// Twitch embedd throws error with strings like 'localhost:3000', but
// those persist with `new URL().host`
if (parent.startsWith('localhost')) {
parent = 'localhost';
}

visit(markdownAST, 'text', async (node) => {
const { value } = node;
const twitchVideo = value.match(/https:\/\/(www\.)?twitch\.tv\/(videos\/[A-Za-z0-9-_?=]*)/gi);
const twitchClip = value.match(/https:\/\/(www\.)?twitch\.tv\/([A-Za-z0-9-_?=]*\/clip\/[A-Za-z0-9-_?=]*)/gi);
const twitchVideo = value.match(/https:\/\/(www\.)?twitch\.tv\/videos\/([A-Za-z0-9-_?=]*)/gi);
const twitchClip = value.match(/https:\/\/(www\.)?clips\.twitch\.tv\/([A-Za-z0-9-_?=]*)/gi);
const twitchChannel = value.match(/https:\/\/(www\.)?twitch\.tv\/([A-Za-z0-9-_?=]*)$/gi);

if (twitchVideo) {
const videoId = (value.split('/')).pop();
console.log(`\n Embeding video: ${twitchVideo} ${videoId} \n`);
node.type = 'html';
node.value = `<div><iframe
src="https://player.twitch.tv/?video=${videoId}&autoplay=false"
src="https://player.twitch.tv/?video=${videoId}&autoplay=false&parent=${parent}"
height="${options.height}"
width="${options.width}"
frameborder="0"
Expand All @@ -25,10 +47,9 @@ module.exports = async ({ markdownAST }, options = { width: 600, height: 300 })

if (twitchClip) {
const clipId = (value.split('/')).pop();
console.log(`\n Embeding clip: ${twitchClip} ${clipId} \n`);
node.type = 'html';
node.value = `<div><iframe
src="https://clips.twitch.tv/embed?clip=${clipId}&autoplay=false"
src="https://clips.twitch.tv/embed?clip=${clipId}&autoplay=false&parent=${parent}"
height="${options.height}"
width="${options.width}"
frameborder="0"
Expand All @@ -38,10 +59,9 @@ module.exports = async ({ markdownAST }, options = { width: 600, height: 300 })
}
if (twitchChannel) {
const channelName = (value.split('/')).pop();
console.log(`\n Embeding channel: ${twitchChannel} ${channelName} \n`);
node.type = 'html';
node.value = `<div><iframe
src="https://player.twitch.tv/?channel=${channelName}&muted=false"
src="https://player.twitch.tv/?channel=${channelName}&muted=false&parent=${parent}"
height="${options.height}"
width="${options.width}"
frameborder="0"
Expand Down
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
"babel-runtime": "^6.26.0",
"unist-util-visit": "^1.1.3"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5",
"unist-util-map": "^1.0.3"
},
"homepage": "https://github.com/weknowinc/gatsby-remark-twitter#readme",
"homepage": "https://github.com/octahedroid/gatsby-remark-twitch#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/weknowinc/gatsby-remark-twitter.git"
"url": "git+https://github.com/octahedroid/gatsby-remark-twitch.git"
}
}
}