nodebb-plugin-link-preview 1.0.0

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/.eslintrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "nodebb"
3
+ }
package/.gitattributes ADDED
@@ -0,0 +1,22 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+
4
+ # Custom for Visual Studio
5
+ *.cs diff=csharp
6
+ *.sln merge=union
7
+ *.csproj merge=union
8
+ *.vbproj merge=union
9
+ *.fsproj merge=union
10
+ *.dbproj merge=union
11
+
12
+ # Standard to msysgit
13
+ *.doc diff=astextplain
14
+ *.DOC diff=astextplain
15
+ *.docx diff=astextplain
16
+ *.DOCX diff=astextplain
17
+ *.dot diff=astextplain
18
+ *.DOT diff=astextplain
19
+ *.pdf diff=astextplain
20
+ *.PDF diff=astextplain
21
+ *.rtf diff=astextplain
22
+ *.RTF diff=astextplain
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 NodeBB Inc. <sales@nodebb.org>
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Link Preview Plugin for NodeBB
2
+
3
+ This plugin adds in additional post parsing capability to NodeBB so that any external links are automatically expanded into an interactive box containing any available metadata (e.g. photo, title, description, etc.)
4
+
5
+ The content shown in the box is powered by [Open Graph tags](https://ogp.me), and this plugin uses [link-preview-js](https://www.npmjs.com/package/link-preview-js) in the backend.
6
+
7
+ ## Installation
8
+
9
+ This plugin comes bundled with NodeBB installs as of v3.1.0. If you wish to use it in older versions, or wish to install it manually, run either of the two commands below:
10
+
11
+ npm install nodebb-plugin-link-preview
12
+
13
+ yarn add nodebb-plugin-link-preview
14
+
15
+ ## Screenshots
16
+
17
+ ![A NodeBB post with an embedded link to a GitHub issue — it looks great!](./screenshots/embed.png)
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ extends: ['@commitlint/config-angular'],
5
+ rules: {
6
+ 'header-max-length': [1, 'always', 72],
7
+ 'type-enum': [
8
+ 2,
9
+ 'always',
10
+ [
11
+ 'breaking',
12
+ 'build',
13
+ 'chore',
14
+ 'ci',
15
+ 'docs',
16
+ 'feat',
17
+ 'fix',
18
+ 'perf',
19
+ 'refactor',
20
+ 'revert',
21
+ 'style',
22
+ 'test',
23
+ ],
24
+ ],
25
+ },
26
+ };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const Controllers = module.exports;
4
+
5
+ Controllers.renderAdminPage = function (req, res/* , next */) {
6
+ res.render('admin/plugins/link-preview', {
7
+ title: 'Link Preview',
8
+ });
9
+ };
package/library.js ADDED
@@ -0,0 +1,162 @@
1
+ 'use strict';
2
+
3
+ const winston = require.main.require('winston');
4
+ const dns = require('dns');
5
+
6
+ const { getLinkPreview } = require('link-preview-js');
7
+
8
+ const meta = require.main.require('./src/meta');
9
+ const cache = require.main.require('./src/cache');
10
+
11
+ const controllers = require('./lib/controllers');
12
+
13
+ const routeHelpers = require.main.require('./src/routes/helpers');
14
+
15
+ const plugin = {};
16
+
17
+ plugin.init = async (params) => {
18
+ const { router /* , middleware , controllers */ } = params;
19
+
20
+ routeHelpers.setupAdminPageRoute(router, '/admin/plugins/link-preview', [], controllers.renderAdminPage);
21
+ };
22
+
23
+ plugin.applyDefaults = async (data) => {
24
+ const { plugin, values } = data;
25
+
26
+ if (plugin === 'link-preview') {
27
+ ['embedHtml', 'embedImage', 'embedAudio', 'embedVideo'].forEach((prop) => {
28
+ if (!values.hasOwnProperty(prop)) {
29
+ values[prop] = 'on';
30
+ }
31
+ });
32
+ }
33
+
34
+ return data;
35
+ };
36
+
37
+ async function process(content) {
38
+ const anchorRegex = /<a\s+(?:[^>]*?\s+)?href=["']([^"']*)["'][^>]*>(.*?)<\/a>/g;
39
+ const matches = [];
40
+ let match;
41
+
42
+ const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
43
+ if (![embedHtml, embedImage, embedAudio, embedVideo].some(prop => prop === 'on')) {
44
+ return content;
45
+ }
46
+
47
+ // eslint-disable-next-line no-cond-assign
48
+ while ((match = anchorRegex.exec(content)) !== null) {
49
+ matches.push(match);
50
+ }
51
+
52
+ const previews = await Promise.all(matches.map(async (match) => {
53
+ const anchor = match[1];
54
+
55
+ const cached = cache.get(`link-preview:${anchor}`);
56
+ if (cached) {
57
+ return await render(cached);
58
+ }
59
+
60
+ // Generate the preview, but return false for now so as to not block response
61
+ getLinkPreview(anchor, {
62
+ resolveDNSHost: async url => new Promise((resolve, reject) => {
63
+ const { hostname } = new URL(url);
64
+ dns.lookup(hostname, (err, address) => {
65
+ if (err) {
66
+ reject(err);
67
+ return;
68
+ }
69
+
70
+ resolve(address); // if address resolves to localhost or '127.0.0.1' library will throw an error
71
+ });
72
+ }),
73
+ followRedirects: `manual`,
74
+ handleRedirects: (baseURL, forwardedURL) => {
75
+ const urlObj = new URL(baseURL);
76
+ const forwardedURLObj = new URL(forwardedURL);
77
+ if (
78
+ forwardedURLObj.hostname === urlObj.hostname ||
79
+ forwardedURLObj.hostname === `www.${urlObj.hostname}` ||
80
+ `www.${forwardedURLObj.hostname}` === urlObj.hostname
81
+ ) {
82
+ return true;
83
+ }
84
+
85
+ return false;
86
+ },
87
+ }).then((preview) => {
88
+ const parsedUrl = new URL(anchor);
89
+ preview.hostname = parsedUrl.hostname;
90
+
91
+ winston.verbose(`[link-preview] ${preview.url} (${preview.contentType}, cache: miss)`);
92
+ cache.set(`link-preview:${anchor}`, preview);
93
+ }).catch(() => {
94
+ winston.verbose(`[link-preview] ${anchor} (invalid, cache: miss)`);
95
+ cache.set(`link-preview:${anchor}`, {
96
+ url: anchor,
97
+ });
98
+ });
99
+
100
+ return false;
101
+ }));
102
+
103
+ // Replace match with embed
104
+ previews.forEach((preview, idx) => {
105
+ if (preview) {
106
+ const match = matches[idx];
107
+ const { index } = match;
108
+ const { length } = match[0];
109
+
110
+ content = `${content.substring(0, index)}${preview}${content.substring(index + length)}`;
111
+ }
112
+ });
113
+
114
+ return content;
115
+ }
116
+
117
+ async function render(preview) {
118
+ const { app } = require.main.require('./src/webserver');
119
+ const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
120
+
121
+ winston.verbose(`[link-preview] ${preview.url} (${preview.contentType || 'invalid'}, cache: hit)`);
122
+
123
+ if (embedHtml && preview.contentType.startsWith('text/html')) {
124
+ return await app.renderAsync('partials/link-preview/html', preview);
125
+ }
126
+
127
+ if (embedImage && preview.contentType.startsWith('image/')) {
128
+ return await app.renderAsync('partials/link-preview/image', preview);
129
+ }
130
+
131
+ if (embedAudio && preview.contentType.startsWith('audio/')) {
132
+ return await app.renderAsync('partials/link-preview/audio', preview);
133
+ }
134
+
135
+ if (embedVideo && preview.contentType.startsWith('video/')) {
136
+ return await app.renderAsync('partials/link-preview/video', preview);
137
+ }
138
+
139
+ return false;
140
+ }
141
+
142
+ plugin.onParse = async (payload) => {
143
+ if (typeof payload === 'string') { // raw
144
+ payload = await process(payload);
145
+ } else if (payload && payload.postData && payload.postData.content) { // post
146
+ payload.postData.content = await process(payload.postData.content);
147
+ }
148
+
149
+ return payload;
150
+ };
151
+
152
+ plugin.addAdminNavigation = (header) => {
153
+ header.plugins.push({
154
+ route: '/plugins/link-preview',
155
+ icon: 'fa-tint',
156
+ name: 'Link Preview',
157
+ });
158
+
159
+ return header;
160
+ };
161
+
162
+ module.exports = plugin;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "nodebb-plugin-link-preview",
3
+ "version": "1.0.0",
4
+ "description": "A starter kit for quickly creating NodeBB plugins",
5
+ "main": "library.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/nodebb/nodebb-plugin-link-preview"
9
+ },
10
+ "keywords": [
11
+ "nodebb",
12
+ "plugin",
13
+ "link-preview",
14
+ "shell"
15
+ ],
16
+ "husky": {
17
+ "hooks": {
18
+ "pre-commit": "lint-staged",
19
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
20
+ }
21
+ },
22
+ "lint-staged": {
23
+ "*.js": [
24
+ "eslint --fix",
25
+ "git add"
26
+ ]
27
+ },
28
+ "license": "MIT",
29
+ "bugs": {
30
+ "url": "https://github.com/nodebb/nodebb-plugin-link-preview/issues"
31
+ },
32
+ "readmeFilename": "README.md",
33
+ "nbbpm": {
34
+ "compatibility": "^3.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@commitlint/cli": "17.6.5",
38
+ "@commitlint/config-angular": "17.6.5",
39
+ "eslint": "8.42.0",
40
+ "eslint-config-nodebb": "0.2.1",
41
+ "eslint-plugin-import": "2.27.5",
42
+ "husky": "8.0.3",
43
+ "lint-staged": "13.2.2"
44
+ },
45
+ "dependencies": {
46
+ "link-preview-js": "^3.0.4"
47
+ }
48
+ }
package/plugin.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "id": "nodebb-plugin-link-preview",
3
+ "url": "https://github.com/NodeBB/nodebb-plugin-link-preview",
4
+ "library": "./library.js",
5
+ "hooks": [
6
+ { "hook": "static:app.load", "method": "init" },
7
+ { "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
8
+ { "hook": "filter:settings.get", "method": "applyDefaults" },
9
+ { "hook": "filter:parse.post", "method": "onParse" },
10
+ { "hook": "filter:parse.raw", "method": "onParse" }
11
+ ],
12
+ "scss": [
13
+ "static/scss/link-preview.scss"
14
+ ],
15
+ "modules": {
16
+ "../admin/plugins/link-preview.js": "./static/lib/admin.js"
17
+ },
18
+ "templates": "static/templates"
19
+ }
package/renovate.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": [
3
+ "config:base"
4
+ ]
5
+ }
Binary file
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "nodebb/public"
3
+ }
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ define('admin/plugins/link-preview', ['settings'], function (settings) {
4
+ var ACP = {};
5
+
6
+ ACP.init = function () {
7
+ settings.load('link-preview', $('.link-preview-settings'));
8
+ $('#save').on('click', saveSettings);
9
+ };
10
+
11
+ function saveSettings() {
12
+ settings.save('link-preview', $('.link-preview-settings'));
13
+ }
14
+
15
+ return ACP;
16
+ });
@@ -0,0 +1,5 @@
1
+ .link-preview {
2
+ .card-img-top {
3
+ object-fit: cover;
4
+ }
5
+ }
@@ -0,0 +1,36 @@
1
+ <form role="form" class="link-preview-settings">
2
+ <div class="row mb-4">
3
+ <div class="col-sm-2 col-12 settings-header">Media Types</div>
4
+ <div class="col-sm-10 col-12">
5
+ <p>
6
+ If enabled, URLs of those types will be converted to preview boxes.
7
+ </p>
8
+
9
+ <div class="form-check form-switch mb-3">
10
+ <input type="checkbox" class="form-check-input" id="embedHtml" name="embedHtml">
11
+ <label for="embedHtml" class="form-check-label">Websites</label>
12
+ </div>
13
+
14
+ <div class="form-check form-switch mb-3">
15
+ <input type="checkbox" class="form-check-input" id="embedImage" name="embedImage">
16
+ <label for="embedImage" class="form-check-label">Images</label>
17
+ </div>
18
+
19
+ <div class="form-check form-switch mb-3">
20
+ <input type="checkbox" class="form-check-input" id="embedAudio" name="embedAudio">
21
+ <label for="embedAudio" class="form-check-label">Audio</label>
22
+ </div>
23
+
24
+ <div class="form-check form-switch mb-3">
25
+ <input type="checkbox" class="form-check-input" id="embedVideo" name="embedVideo">
26
+ <label for="embedVideo" class="form-check-label">Video</label>
27
+ </div>
28
+
29
+ <p class="help-text">
30
+ Please note that the "audio" and "video" formats only apply if the URL is a direct link to the audio/video file. Links to video hosting sites (e.g. YouTube) would fall under the "websites" category.
31
+ </p>
32
+ </div>
33
+ </div>
34
+ </form>
35
+
36
+ <!-- IMPORT admin/partials/save_button.tpl -->
@@ -0,0 +1 @@
1
+ <audio controls src="{url}"></audio>
@@ -0,0 +1,27 @@
1
+ <div class="card col-6 position-relative link-preview">
2
+ {{{ if images.length }}}
3
+ {{{ each images }}}
4
+ {{{ if @first }}}
5
+ <img src="{@value}" class="card-img-top" style="max-height: 15rem;" />
6
+ {{{ end }}}
7
+ {{{ end }}}
8
+ {{{ end }}}
9
+ <div class="card-body">
10
+ <h5 class="card-title">
11
+ <a href="{url}">
12
+ {title}
13
+ </a>
14
+ </h5>
15
+ <p class="card-text line-clamp-3">{description}</p>
16
+ </div>
17
+ <a href="{url}" class="card-footer text-body-secondary small d-flex gap-2 align-items-center lh-2">
18
+ {{{ if favicons.length }}}
19
+ {{{ each favicons }}}
20
+ {{{ if @first }}}
21
+ <img src="{@value}" alt="favicon" style="max-width: 21px; max-height: 21px;" />
22
+ {{{ end }}}
23
+ {{{ end }}}
24
+ {{{ end }}}
25
+ <p class="d-inline-block text-truncate mb-0">{siteName} <span class="text-secondary">({hostname})</span></p>
26
+ </a>
27
+ </div>
@@ -0,0 +1 @@
1
+ <img class="img-thumbnail" src="{url}" />
@@ -0,0 +1,3 @@
1
+ <video controls>
2
+ <source src="{url}" type="{contentType}">
3
+ </video>
package/test/.eslintrc ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "env": {
3
+ "mocha": true
4
+ },
5
+ "rules": {
6
+ "no-unused-vars": "off"
7
+ }
8
+ }
9
+
package/test/index.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * You can run these tests by executing `npx mocha test/plugins-installed.js`
3
+ * from the NodeBB root folder. The regular test runner will also run these
4
+ * tests.
5
+ *
6
+ * Keep in mind tests do not activate all plugins, so if you are testing
7
+ * hook listeners, socket.io, or mounted routes, you will need to add your
8
+ * plugin to `config.json`, e.g.
9
+ *
10
+ * {
11
+ * "test_plugins": [
12
+ * "nodebb-plugin-link-preview"
13
+ * ]
14
+ * }
15
+ */
16
+
17
+ 'use strict';
18
+
19
+ /* globals describe, it, before */
20
+
21
+ const assert = require('assert');
22
+
23
+ const db = require.main.require('./test/mocks/databasemock');
24
+
25
+ describe('nodebb-plugin-link-preview', () => {
26
+ before(() => {
27
+ // Prepare for tests here
28
+ });
29
+
30
+ it('should pass', (done) => {
31
+ const actual = 'value';
32
+ const expected = 'value';
33
+ assert.strictEqual(actual, expected);
34
+ done();
35
+ });
36
+
37
+ it('should load config object', async () => { // Tests can be async functions too
38
+ const config = await db.getObject('config');
39
+ assert(config);
40
+ });
41
+ });