This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first code * fix typo * fix package.json
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
# gatsby-remark-twitter | ||
|
||
Embed Tweet cards in your markdown! | ||
|
||
### [View a live demo here](https://jmolivas.weknowinc.com/) | ||
|
||
## Install | ||
|
||
1. Install plugin to your site: | ||
|
||
```bash | ||
yarn add gatsby-remark-twitter | ||
``` | ||
|
||
2. Add `gatsby-remark-twitter` to your `gatsby-transformer-remark` plugins in `gatsby-config.js`: | ||
|
||
```js | ||
plugins: [ | ||
{ | ||
resolve: "gatsby-transformer-remark", | ||
options: { | ||
plugins: ["gatsby-remark-twitter"] | ||
} | ||
} | ||
]; | ||
``` | ||
|
||
3. Restart gastby. | ||
|
||
## Usage | ||
|
||
```markdown | ||
## My blog post | ||
|
||
This is an example of embedding a single tweet card. | ||
Just type your markdown as you normally do, and then insert a valid | ||
tweet link anywhere to automatically transform it into an embed! | ||
|
||
https://twitter.com/gatsbyjs/status/1055939617646465024 | ||
|
||
You can also embed several tweets | ||
|
||
https://twitter.com/wesbos/status/1070699265272496128 | ||
|
||
|
||
``` | ||
|
||
> __NOTE:__ Make sure to copy the link instead of embed code. | ||
<p align="center"><img src="https://i.imgur.com/evEv2LJ.jpg" alt="screenshot for share > copy tweet link" /></p> | ||
|
||
## Configuration | ||
|
||
```js | ||
plugins: [ | ||
{ | ||
resolve: "gatsby-transformer-remark", | ||
options: { | ||
plugins: [ | ||
"gatsby-remark-twitter", | ||
] | ||
} | ||
} | ||
]; | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const injectTwitterScript = () => { | ||
function addJS(jsCode) { | ||
const s = document.createElement('script'); | ||
|
||
s.type = 'text/javascript'; | ||
s.innerText = jsCode; | ||
document.getElementsByTagName('head')[0].appendChild(s); | ||
} | ||
addJS(` | ||
window.twttr = (function(d, s, id) { | ||
var js, fjs = d.getElementsByTagName(s)[0], | ||
t = window.twttr || {}; | ||
if (d.getElementById(id)) return t; | ||
js = d.createElement(s); | ||
js.id = id; | ||
js.src = "https://platform.twitter.com/widgets.js"; | ||
fjs.parentNode.insertBefore(js, fjs); | ||
t._e = []; | ||
t.ready = function(f) { | ||
t._e.push(f); | ||
}; | ||
return t; | ||
}(document, "script", "twitter-wjs")); | ||
`); | ||
}; | ||
|
||
let injectedTwitterScript = false; | ||
exports.onRouteUpdate = function ({ location }) { | ||
// If there's an embedded tweet, lazy-load the twitter script (if it hasn't | ||
// already been loaded), and then run the twitter load function. | ||
// console.log('load tweeter script'); | ||
if (document.querySelector('.twitter-tweet') !== null) { | ||
if (!injectedTwitterScript) { | ||
injectTwitterScript(); | ||
injectedTwitterScript = true; | ||
} | ||
|
||
if ( | ||
typeof twttr !== 'undefined' | ||
&& window.twttr.widgets | ||
&& typeof window.twttr.widgets.load === 'function' | ||
) { | ||
window.twttr.widgets.load(); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
|
||
const visit = require('unist-util-visit'); | ||
const fetch = require('node-fetch'); | ||
|
||
const getBloquequote = async (url) => { | ||
try { | ||
const apiUrl = `https://publish.twitter.com/oembed?url=${url}`; | ||
|
||
const response = await fetch(apiUrl); | ||
const json = await response.json(); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
module.exports = async ({ markdownAST }) => { | ||
visit(markdownAST, 'text', async (node) => { | ||
const { value } = node; | ||
const tweetLink = value.match(/https:\/\/twitter\.com\/([A-Za-z0-9-_]*\/status\/[A-Za-z0-9-_?=]*)/gi); | ||
if (tweetLink) { | ||
console.log(`\n embeding tweet: ${tweetLink} \n`); | ||
const embedData = await getBloquequote(tweetLink); | ||
node.type = 'html'; | ||
node.value = embedData.html; | ||
} | ||
}); | ||
|
||
return markdownAST; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "gatsby-remark-twitter", | ||
"version": "0.1.0", | ||
"description": "Embed Tweet cards in Gatsby via Markdown", | ||
"main": "index.js", | ||
"author": "Jairo Rondon <jrondon@anexusit.com>", | ||
"license": "MIT", | ||
"keywords": ["gatsby", "gatsby-plugin", "remark", "twitter"], | ||
"dependencies": { | ||
"babel-runtime": "^6.26.0", | ||
"unist-util-visit": "^1.1.3", | ||
"node-fetch": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"cross-env": "^5.0.5", | ||
"unist-util-map": "^1.0.3" | ||
}, | ||
"scripts": { | ||
"build": "babel src --out-dir .", | ||
"prepublish": "cross-env NODE_ENV=production npm run build", | ||
"watch": "babel -w src --out-dir ." | ||
}, | ||
"homepage": "https://github.com/weknow/gatsby-remark-twitter#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/weknow/gatsby-remark-twitter.git" | ||
} | ||
} |