From 4cac52b19605d8290a9dfd83964fb2acc1eac0aa Mon Sep 17 00:00:00 2001 From: Trevor Blades Date: Mon, 27 Jul 2020 13:09:12 -0700 Subject: [PATCH 1/4] Fix repository and homepage fields in package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 655871c..267a1fa 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "cross-env": "^5.0.5", "unist-util-map": "^1.0.3" }, - "homepage": "https://github.com/weknowinc/gatsby-remark-twitter#readme", + "homepage": "https://github.com/weknowinc/gatsby-remark-twitch#readme", "repository": { "type": "git", - "url": "git+https://github.com/weknowinc/gatsby-remark-twitter.git" + "url": "git+https://github.com/weknowinc/gatsby-remark-twitch.git" } -} \ No newline at end of file +} From 300bdeefa18d554b5b209cd49c4caab5473a5587 Mon Sep 17 00:00:00 2001 From: Trevor Blades Date: Mon, 27 Jul 2020 13:11:00 -0700 Subject: [PATCH 2/4] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 267a1fa..63739eb 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "cross-env": "^5.0.5", "unist-util-map": "^1.0.3" }, - "homepage": "https://github.com/weknowinc/gatsby-remark-twitch#readme", + "homepage": "https://github.com/octahedroid/gatsby-remark-twitch#readme", "repository": { "type": "git", - "url": "git+https://github.com/weknowinc/gatsby-remark-twitch.git" + "url": "git+https://github.com/octahedroid/gatsby-remark-twitch.git" } } From 6da15063e000b3b382f7ed2a491c1387de12fcba Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Wed, 21 Jul 2021 14:09:29 -0700 Subject: [PATCH 3/4] Remove unused deps, add gitignore --- .gitignore | 3 +++ package.json | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac16a50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Editors +.vscode/ +.idea/ diff --git a/package.json b/package.json index 63739eb..3052135 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,6 @@ "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/octahedroid/gatsby-remark-twitch#readme", "repository": { "type": "git", From 4c4be734a9441d0c941d01bc2f7845bbffc0bccd Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Wed, 21 Jul 2021 14:15:08 -0700 Subject: [PATCH 4/4] Update to work with new Twitch API requirements --- README.md | 6 ++++-- index.js | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index aca2c28..4650c1b 100644 --- a/README.md +++ b/README.md @@ -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 } } ] @@ -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 diff --git a/index.js b/index.js index ffeb989..0f02f72 100644 --- a/index.js +++ b/index.js @@ -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 = `