webmention-web-components 0.0.1-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/package.json +28 -0
- package/src/utils.js +40 -0
- package/src/webmention-list.js +118 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Martin Severin Steffensen
|
|
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.
|
package/README.md
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "webmention-web-components",
|
|
3
|
+
"version": "0.0.1-alpha.2",
|
|
4
|
+
"description": "Web components for presenting webmentions.",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"src",
|
|
9
|
+
"LICENSE",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./index.js",
|
|
14
|
+
"./list": "./src/webmention-list.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"webmention",
|
|
18
|
+
"indieweb",
|
|
19
|
+
"web-components",
|
|
20
|
+
"custom-elements"
|
|
21
|
+
],
|
|
22
|
+
"author": "Martin Severin Steffensen <martin.severin.steffensen@proton.me> (https://clueless.no/)",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/Maritims/webmention.git"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} Webmention
|
|
3
|
+
* @property {string} target The target URL of the webmention.
|
|
4
|
+
* @property {string} source The source URL of the webmention.
|
|
5
|
+
* @property {string} mentionText The text of the webmention.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} WebmentionFetchResult
|
|
10
|
+
* @property {Webmention[]} webmentions The webmentions fetched from the endpoint.
|
|
11
|
+
* @property {boolean} ok Whether the last fetch was successful.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Fetches webmentions from the specified endpoint.
|
|
16
|
+
* @param url The URL to fetch webmentions from.
|
|
17
|
+
* @return {Promise<WebmentionFetchResult>}
|
|
18
|
+
*/
|
|
19
|
+
export const fetchWebmentions = async (url) => {
|
|
20
|
+
/** @type {WebmentionFetchResult} */
|
|
21
|
+
let webmentionFetchResult = {webmentions: [], ok: false};
|
|
22
|
+
|
|
23
|
+
if (url) {
|
|
24
|
+
try {
|
|
25
|
+
const response = await fetch(url);
|
|
26
|
+
if (response.ok) {
|
|
27
|
+
const webmentions = await response.json();
|
|
28
|
+
webmentionFetchResult = {webmentions, ok: true};
|
|
29
|
+
} else {
|
|
30
|
+
console.error(`An error occurred while attempting to fetch webmentions from ${url}. Status code: ${response.status}.`);
|
|
31
|
+
}
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(`An error occurred while attempting to fetch webmentions from ${url}.`, error);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
console.error('No endpoint specified Ensure that the endpoint attribute is set with a value pointing to the appropriate endpoint for fetching webmention entries.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return webmentionFetchResult;
|
|
40
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {fetchWebmentions} from './utils.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A component for displaying webmentions fetched from a specified endpoint.
|
|
5
|
+
* @attribute hide-when-empty Whether to hide the webmentions section when there are no webmentions to display. Defaults to false.
|
|
6
|
+
* @attribute endpoint The webmention endpoint to fetch from.
|
|
7
|
+
* @attribute heading The heading to display above the webmentions. Defaults to "Mentions".
|
|
8
|
+
* @example <webmention-list heading="Read what others are saying" endpoint="https://example.com/webmention/endpoint" hide-when-empty="false"></webmention-list>/
|
|
9
|
+
*/
|
|
10
|
+
export class WebmentionList extends HTMLElement {
|
|
11
|
+
/**
|
|
12
|
+
* @type {string} The webmention endpoint to fetch from.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
_endpoint;
|
|
16
|
+
/**
|
|
17
|
+
* The webmentions fetched from the endpoint.
|
|
18
|
+
* @type {Webmention[]}
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
_webmentions = [];
|
|
22
|
+
/**
|
|
23
|
+
* @type {boolean} Whether the last fetch was successful.
|
|
24
|
+
* @private
|
|
25
|
+
*/
|
|
26
|
+
_ok;
|
|
27
|
+
|
|
28
|
+
constructor() {
|
|
29
|
+
super();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static observedAttributes = ['endpoint', 'heading', 'hide-when-empty'];
|
|
33
|
+
|
|
34
|
+
#escapeHtml(str) {
|
|
35
|
+
if (!str) {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const div = document.createElement('div');
|
|
40
|
+
div.textContent = str;
|
|
41
|
+
return div.innerHTML;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Sanitizes a URL to ensure it is a valid protocol relative URL.
|
|
46
|
+
* @param url The URL to sanitize.
|
|
47
|
+
* @return {string} The sanitized URL.
|
|
48
|
+
*/
|
|
49
|
+
#sanitizeUrl(url) {
|
|
50
|
+
try {
|
|
51
|
+
const protocol = new URL(url, window.location.href).protocol;
|
|
52
|
+
return (protocol === 'http:' || protocol === 'https:') ? url : '#';
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return '#';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async attributeChangedCallback(name, oldValue, newValue) {
|
|
59
|
+
if (oldValue !== newValue) {
|
|
60
|
+
switch (name) {
|
|
61
|
+
case 'endpoint':
|
|
62
|
+
this._endpoint = newValue;
|
|
63
|
+
break;
|
|
64
|
+
case 'heading':
|
|
65
|
+
this._heading = newValue;
|
|
66
|
+
break;
|
|
67
|
+
case 'hide-when-empty':
|
|
68
|
+
this._hideWhenEmpty = newValue === 'true';
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (this.isConnected) {
|
|
75
|
+
if (name === 'endpoint') {
|
|
76
|
+
await this.fetch();
|
|
77
|
+
}
|
|
78
|
+
this.render();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
connectedCallback() {
|
|
84
|
+
this._hideWhenEmpty = this.getAttribute('hide-when-empty') === 'true' || false;
|
|
85
|
+
this._endpoint = this.getAttribute('endpoint');
|
|
86
|
+
this._heading = this.getAttribute('heading') || 'Mentions';
|
|
87
|
+
this.fetch().then(() => this.render());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
render() {
|
|
91
|
+
if (this._hideWhenEmpty && this._webmentions.length === 0) {
|
|
92
|
+
this.innerHTML = '';
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this.innerHTML = `
|
|
97
|
+
<section class="webmention">
|
|
98
|
+
<h2>${this.#escapeHtml(this._heading)}</h2>
|
|
99
|
+
<ul>${this._webmentions.map(webmention => `
|
|
100
|
+
<li>
|
|
101
|
+
<a href="${this.#sanitizeUrl(webmention.target)}">${this.#escapeHtml(webmention.mentionText)}</a> (from: <a href="${this.#sanitizeUrl(webmention.source)}">${this.#escapeHtml(webmention.source)})</a>
|
|
102
|
+
</li>
|
|
103
|
+
`).join('')}
|
|
104
|
+
</ul>
|
|
105
|
+
</section>`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async fetch() {
|
|
109
|
+
const webmentionFetchResult = fetchWebmentions(this._endpoint);
|
|
110
|
+
const { webmentions, ok } = await webmentionFetchResult;
|
|
111
|
+
this._webmentions = webmentions;
|
|
112
|
+
this._ok = ok;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!customElements.get('webmention-list')) {
|
|
117
|
+
customElements.define('webmention-list', WebmentionList);
|
|
118
|
+
}
|