Skip to content
Open
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
70 changes: 65 additions & 5 deletions library.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@ async function preview(url) {
});
}

function isExternalLink(urlString) {
let urlObj;
let baseUrlObj;
try {
urlObj = new URL(urlString, nconf.get('url'));
baseUrlObj = nconf.get('url_parsed');
} catch (err) {
return false;
}

if (urlObj.host === null) {
return false;
}

const sameHost = urlObj.host === baseUrlObj.host &&
urlObj.protocol === baseUrlObj.protocol &&
(nconf.get('relative_path').length > 0 ? urlObj.pathname.indexOf(nconf.get('relative_path')) === 0 : true);
return !sameHost;
}

function buildRel(externalBlank, nofollow) {
const rel = [];
if (externalBlank) {
rel.push('noopener', 'noreferrer');
}
if (nofollow) {
rel.push('nofollow', 'ugc');
}
return rel.join(' ');
}

function externalLinkAttrs(url, md) {
const isExternal = isExternalLink(url);
return {
targetAttr: (md.externalBlank && isExternal) ? '_blank' : '',
relAttr: isExternal ? buildRel(md.externalBlank, md.nofollow) : '',
showExternalIcon: md.externalMark && isExternal,
};
}

async function process(content, { type, pid, tid, attachments }) {
const inlineTypes = ['default', 'activitypub.article'];
const processInline = inlineTypes.includes(type);
Expand All @@ -88,6 +128,13 @@ async function process(content, { type, pid, tid, attachments }) {
return content;
}

const markdownSettings = await meta.settings.get('markdown');
const md = {
externalBlank: markdownSettings.externalBlank === 'on',
externalMark: markdownSettings.externalMark === 'on',
nofollow: markdownSettings.nofollow === undefined ? true : markdownSettings.nofollow === 'on',
};

const requests = new Map();
let attachmentHtml = '';
let placeholderHtml = '';
Expand Down Expand Up @@ -205,7 +252,7 @@ async function process(content, { type, pid, tid, attachments }) {
const options = requests.get(url);
const cached = cache.get(`link-preview:${url}`);
if (cached) {
const html = await render(cached);
const html = await render(cached, md);
if (html) {
switch (options.type) {
case 'inline': {
Expand All @@ -224,7 +271,11 @@ async function process(content, { type, pid, tid, attachments }) {
}
} else {
if (options.type === 'attachment') {
placeholderHtml += `<p><a href="${url}" rel="nofollow ugc">${url}</a></p>`;
const { targetAttr, relAttr, showExternalIcon } = externalLinkAttrs(url, md);
const target = targetAttr ? ` target="${targetAttr}"` : '';
const rel = relAttr ? ` rel="${relAttr}"` : '';
const icon = showExternalIcon ? ' <i class="fa fa-external-link small external-link-icon"></i>' : '';
placeholderHtml += `<p><a href="${url}"${target}${rel}>${url}${icon}</a></p>`;
}
cold.add(url);
}
Expand All @@ -246,7 +297,7 @@ async function process(content, { type, pid, tid, attachments }) {
const parsedUrl = new URL(url);
preview.hostname = parsedUrl.hostname;

const html = await render(preview);
const html = await render(preview, md);
if (html) {
switch (options.type) {
case 'inline': {
Expand All @@ -269,7 +320,11 @@ async function process(content, { type, pid, tid, attachments }) {
}));

const placeholderHtml = Array.from(failures).reduce((html, cur) => {
html += `<p><a href="${cur}" rel="nofollow ugc">${cur}</a></p>`;
const { targetAttr, relAttr, showExternalIcon } = externalLinkAttrs(cur, md);
const target = targetAttr ? ` target="${targetAttr}"` : '';
const rel = relAttr ? ` rel="${relAttr}"` : '';
const icon = showExternalIcon ? ' <i class="fa fa-external-link small external-link-icon"></i>' : '';
html += `<p><a href="${cur}"${target}${rel}>${cur}${icon}</a></p>`;
return html;
}, '');
let modified = content;
Expand Down Expand Up @@ -320,7 +375,7 @@ async function process(content, { type, pid, tid, attachments }) {
return modified;
}

async function render(preview) {
async function render(preview, md) {
const { app } = nodebb.require('./src/webserver');
const { embedHtml, embedImage, embedAudio, embedVideo, embedIframe } = await meta.settings.get('link-preview');

Expand All @@ -330,6 +385,11 @@ async function render(preview) {
return false;
}

const { targetAttr, relAttr, showExternalIcon } = externalLinkAttrs(preview.url, md);
preview.targetAttr = targetAttr;
preview.relAttr = relAttr;
preview.showExternalIcon = showExternalIcon;

if (embedHtml && preview.contentType.startsWith('text/html')) {
return await app.renderAsync('partials/link-preview/html', preview);
}
Expand Down
12 changes: 5 additions & 7 deletions static/templates/partials/link-preview/html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@
{{{ if images.length }}}
{{{ each images }}}
{{{ if @first }}}
<a href="{url}" title="{title}">
<img src="{@value}" class="card-img-top not-responsive" style="max-height: 15rem;" alt="Link Preview Image" onerror="this.parentElement.remove()" />
</a>
<a href="{url}" title="{title}"{{{ if targetAttr }}} target="{targetAttr}"{{{ end }}}{{{ if relAttr }}} rel="{relAttr}"{{{ end }}}><img src="{@value}" class="card-img-top not-responsive" style="max-height: 15rem;" alt="Link Preview Image" onerror="this.parentElement.remove()" /></a>
{{{ end }}}
{{{ end }}}
{{{ end }}}
<div class="card-body">
<h5 class="card-title">
<a class="text-decoration-none" href="{url}">
{title}
<a class="text-decoration-none" href="{url}"{{{ if targetAttr }}} target="{targetAttr}"{{{ end }}}{{{ if relAttr }}} rel="{relAttr}"{{{ end }}}>
{title}{{{ if showExternalIcon }}} <i class="fa fa-external-link small external-link-icon"></i>{{{ end }}}
</a>
</h5>
<p class="card-text line-clamp-3">{description}</p>
</div>
<a href="{url}" class="card-footer text-body-secondary small d-flex gap-2 align-items-center lh-2">
<a href="{url}" class="card-footer text-body-secondary small d-flex gap-2 align-items-center lh-2"{{{ if targetAttr }}} target="{targetAttr}"{{{ end }}}{{{ if relAttr }}} rel="{relAttr}"{{{ end }}}>
{{{ if favicons.length }}}
{{{ each favicons }}}
{{{ if @first }}}
<img src="{@value}" alt="favicon" class="not-responsive overflow-hiddden" style="max-width: 21px; max-height: 21px;" onerror="this.remove()"/>
{{{ end }}}
{{{ end }}}
{{{ end }}}
<p class="d-inline-block text-truncate mb-0">{siteName} <span class="text-secondary">({hostname})</span></p>
<p class="d-inline-block text-truncate mb-0">{siteName} <span class="text-secondary">({hostname})</span></p>{{{ if showExternalIcon }}} <i class="fa fa-external-link small external-link-icon"></i>{{{ end }}}
</a>
</div>