Skip to content

Commit 8df5f33

Browse files
committed
Initial commit
0 parents  commit 8df5f33

File tree

7 files changed

+1894
-0
lines changed

7 files changed

+1894
-0
lines changed

.eslintrc.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
env: {
3+
commonjs: true,
4+
es6: true,
5+
node: true,
6+
},
7+
extends: [
8+
'airbnb-base',
9+
],
10+
globals: {
11+
Atomics: 'readonly',
12+
SharedArrayBuffer: 'readonly',
13+
},
14+
parserOptions: {
15+
ecmaVersion: 2018,
16+
},
17+
rules: {
18+
'no-console': 'off',
19+
'func-names': 'off'
20+
},
21+
};

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.vscode
3+
.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Jozef Maxted
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# @zefman/gridsome-source-webmentions
2+
3+
> Webmentions source for Gridsome 💬
4+
5+
Loads webmentions from [https://webmention.io](https://webmention.io)
6+
7+
I will write a tutorial soon on how to set up webmentions with Gridsome using this plugin, in the meantime if you don't know what a webmention is give
8+
this a read: (https://en.wikipedia.org/wiki/Webmention)[https://en.wikipedia.org/wiki/Webmention]
9+
10+
## Install
11+
- `npm install @zefman/gridsome-source-webmentions`
12+
- `yarn add @zefman/gridsome-source-webmentions`
13+
14+
## Usage
15+
16+
Add the plugin in `gridsome.config.js`
17+
18+
```js
19+
export default {
20+
plugins: [
21+
{
22+
use: '@zefman/gridsome-source-webmentions',
23+
options: {
24+
domain: 'pixelhop.io', // Your webmention domain
25+
token: 'your-token', // Your secret webmention token
26+
},
27+
}
28+
]
29+
}
30+
```
31+
32+
Example query to load a pages webmentions
33+
34+
```
35+
<page-query>
36+
query($path: String!) {
37+
mentions: allWebMention(filter: { wmTarget: { regex: $path } }) {
38+
edges {
39+
node {
40+
wmId
41+
url
42+
wmProperty
43+
wmSource
44+
content {
45+
text
46+
}
47+
author {
48+
name
49+
photo
50+
url
51+
}
52+
}
53+
}
54+
}
55+
}
56+
</page-query>
57+
```
58+
59+
## Todo
60+
61+
* Write blog post explaining usage
62+
* Handle webmention.io pagination. Currently limited to 2000 mentions

index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const axios = require('axios');
2+
const camelcaseKeys = require('camelcase-keys');
3+
4+
// get all mentions for a token and a specific domain
5+
const getMentions = async ({ domain, token }) => axios
6+
.get(
7+
`https://webmention.io/api/mentions.jf2?domain=${domain}&token=${token}&per-page=2000`,
8+
)
9+
.then((response) => response.data)
10+
.then((mentions) => {
11+
if (!mentions || !mentions.children) {
12+
return [];
13+
}
14+
// For entries to be automatically created the keys in the
15+
// objects need to be in camelcase
16+
return camelcaseKeys(mentions.children);
17+
});
18+
19+
module.exports = function (api, options) {
20+
api.loadSource(async ({ addCollection }) => {
21+
const contentType = addCollection({
22+
typeName: options.typeName,
23+
});
24+
25+
try {
26+
const mentions = await getMentions(options);
27+
28+
mentions.forEach((mention) => {
29+
contentType.addNode(mention);
30+
});
31+
32+
console.log(`💬 Added ${mentions.length} web mentions`);
33+
} catch (error) {
34+
console.error('Error getting web mentions');
35+
console.error(error);
36+
}
37+
});
38+
};
39+
40+
module.exports.defaultOptions = () => ({
41+
typeName: 'WebMention',
42+
});

0 commit comments

Comments
 (0)