This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/lib/render-topic-featured-link.js
Bianca Nenciu 7d7551adfc
DEV: Remove user options from current user serializer (#19089)
User options were serialized at the root level of CurrentUserSerializer,
but UserSerializer has a user_option field. This inconsistency caused
issues in the past because user_option fields had to be duplicated on
the frontend.
2022-12-05 18:25:30 +02:00

69 lines
1.5 KiB
JavaScript

import User from "discourse/models/user";
import { h } from "virtual-dom";
import { renderIcon } from "discourse-common/lib/icon-library";
const _decorators = [];
export function addFeaturedLinkMetaDecorator(decorator) {
_decorators.push(decorator);
}
export function extractLinkMeta(topic) {
const href = topic.get("featured_link");
const target = User.currentProp("user_option.external_links_in_new_tab")
? "_blank"
: "";
const domain = topic.get("featured_link_root_domain");
let allowList = topic.siteSettings.exclude_rel_nofollow_domains;
let rel = "nofollow ugc";
if (allowList) {
allowList = allowList.split("|");
if (allowList.includes(domain)) {
rel = rel.replace("nofollow ", "");
}
}
if (!href) {
return;
}
const meta = {
target,
href,
domain,
rel,
};
if (_decorators.length) {
_decorators.forEach((cb) => cb(meta));
}
return meta;
}
export default function renderTopicFeaturedLink(topic) {
const meta = extractLinkMeta(topic);
if (meta) {
return `<a class="topic-featured-link" rel="${meta.rel}" target="${
meta.target
}" href="${meta.href}">${renderIcon("string", "external-link-alt")} ${
meta.domain
}</a>`;
} else {
return "";
}
}
export function topicFeaturedLinkNode(topic) {
const meta = extractLinkMeta(topic);
if (meta) {
return h(
"a.topic-featured-link",
{
attributes: { href: meta.href, rel: meta.rel, target: meta.target },
},
[renderIcon("node", "external-link-alt"), meta.domain]
);
}
}