2016-12-05 20:31:43 +08:00
|
|
|
import { h } from 'virtual-dom';
|
|
|
|
|
|
|
|
const _decorators = [];
|
|
|
|
|
|
|
|
export function addFeaturedLinkMetaDecorator(decorator) {
|
|
|
|
_decorators.push(decorator);
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:53:35 +08:00
|
|
|
export function extractLinkMeta(topic) {
|
2017-11-29 21:52:41 +08:00
|
|
|
const href = topic.get('featured_link');
|
|
|
|
const target = Discourse.User.currentProp('external_links_in_new_tab') ? '_blank' : '';
|
2016-12-10 02:48:29 +08:00
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
if (!href) { return; }
|
|
|
|
|
2017-11-29 21:52:41 +08:00
|
|
|
const meta = {
|
|
|
|
target: target,
|
|
|
|
href,
|
|
|
|
domain: topic.get('featured_link_root_domain'),
|
|
|
|
rel: 'nofollow'
|
|
|
|
};
|
2016-12-05 20:31:43 +08:00
|
|
|
|
|
|
|
if (_decorators.length) {
|
|
|
|
_decorators.forEach(cb => cb(meta));
|
|
|
|
}
|
2017-11-29 21:52:41 +08:00
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
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}">${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 }
|
|
|
|
}, meta.domain);
|
|
|
|
}
|
|
|
|
}
|