Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Embed GitHub gist #84

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions demo/src/pages/gist.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import * as Component from 'astro-embed';
import Base from '../layouts/Base.astro';
---

<Base title="Gist component examples">
<h2>All files</h2>
<Component.Gist
id="https://gist.github.com/vasfvitor/1f995c47ee82ae652496622db1e678ea"
/>

<h2>Specific file</h2>
<Component.Gist
id="https://gist.github.com/vasfvitor/1f995c47ee82ae652496622db1e678ea"
fileName="03-fileName.rs"
/>
</Base>
3 changes: 3 additions & 0 deletions demo/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import Base from '../layouts/Base.astro';
<li>
<a href="/youtube"><code>&lt;YouTube/&gt;</code> component examples</a>
</li>
<li>
<a href="/gist"><code>&lt;Gist/&gt;</code> component examples</a>
</li>
</ul>
<h2>Other examples</h2>
<ul>
Expand Down
2 changes: 2 additions & 0 deletions demo/src/pages/integration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ https://twitter.com/astrodotbuild/status/1511750228428435457
https://vimeo.com/32001208

http://www.youtube.com/watch?v=Hoe-woAhq_k

https://gist.github.com/vasfvitor/1f995c47ee82ae652496622db1e678ea
51 changes: 36 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions packages/astro-embed-gist/Gist.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
export interface Props {
/**
* Gist url to fetch
*/
id: string;
/**
* Optional file name to fetch, case sensitive
*/
fileName?: string;
}
const { id, fileName } = Astro.props;
let url = id + '.json';
if (fileName) {
url += `?file=${fileName}`;
}

async function fetchGist(id: string) {
try {
const gistUrl = new URL(url);
return (await fetch(gistUrl).then((res) => res.json())) as {
description: string;
created_at: Date;
files: string[];
owner: string;
stylesheet: string;
div: string;
};
} catch (e) {
console.error(
`[error] astro-embed
${e.status} - ${e.statusText}: Failed to fetch gist ${id}`
);
}
}
const gist = await fetchGist(id);

// TODO use stylesheet
const stylesheetUrl = gist?.stylesheet;
---

{
gist && (
<astro-embed-gist data-cssurl={stylesheetUrl}>
<p class="gist-embed-description">{gist.description}</p>
<Fragment set:html={gist.div} />
<span class="gist-embed-owner">gist by {gist.owner}</span>
</astro-embed-gist>
)
}
<style>
astro-embed-gist {
display: block;
}
.gist-embed-owner {
}
.gist-embed-description {
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function () {
const gists = document.querySelectorAll('astro-embed-gist');
gists.forEach((e) => {
const stylesheetUrl = e.dataset.cssurl;
const linkElement = document.createElement('link');
linkElement.href = stylesheetUrl;
linkElement.rel = 'stylesheet';
linkElement.type = 'text/css';
document.head.appendChild(linkElement);
});
});
</script>
1 change: 1 addition & 0 deletions packages/astro-embed-gist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Gist } from './Gist.astro';
36 changes: 36 additions & 0 deletions packages/astro-embed-gist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@astro-community/astro-embed-gist",
"version": "0.5.2",
"description": "Component to easily embed Gists on your Astro site",
"type": "module",
"exports": {
".": "./index.ts",
"./matcher": "./matcher.ts"
},
"files": [
"index.ts",
"Gist.astro"
],
"repository": {
"type": "git",
"url": "git+https://github.com/delucis/astro-embed.git"
},
"keywords": [
"astro",
"astro-component",
"embeds",
"gist"
],
"author": "delucis",
"license": "MIT",
"bugs": {
"url": "https://github.com/delucis/astro-embed/issues"
},
"homepage": "https://github.com/delucis/astro-embed/tree/main/packages/astro-embed-gist#readme",
"dependencies": {
"@astro-community/astro-embed-utils": "^0.1.0"
},
"peerDependencies": {
"astro": "^2.0.0 || ^3.0.0-beta || ^4.0.0-beta"
}
}
1 change: 1 addition & 0 deletions packages/astro-embed/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Tweet } from '@astro-community/astro-embed-twitter';
export { YouTube } from '@astro-community/astro-embed-youtube';
export { Vimeo } from '@astro-community/astro-embed-vimeo';
export { Gist } from '@astro-community/astro-embed-gist';