empress-blog
Version:
Fully-functional, SEO friendly static site implementation of a blog system built on Ember.
107 lines (82 loc) • 2.58 kB
JavaScript
/* eslint-disable ember/no-computed-properties-in-native-classes, prettier/prettier */
import HeadData from 'ember-meta/services/head-data';
import { getOwner } from '@ember/application';
import { computed } from '@ember/object';
import config from 'ember-get-config';
import { getExcerpt, stripHTML } from '../helpers/excerpt';
const { blog } = config;
export default class HeadDataService extends HeadData {
get config() {
return config['blog'];
}
get siteName() {
return this.config.title;
}
get currentController() {
return getOwner(this).lookup(`controller:${this.routeName}`)
}
get currentRouteMeta() {
return this.currentController.model.post ?? this.currentController.model;
}
get title() {
if(this.routeName === 'tag') {
return `Tag: ${this.currentRouteMeta.name}`;
}
if(this.routeName === 'author') {
return `Author: ${this.currentRouteMeta.name}`;
}
return super.title;
}
get author() {
return this.currentRouteMeta.authors?.firstObject;
}
get description() {
let currentModel = this.currentRouteMeta;
if(currentModel && currentModel.html) {
const excerpt = getExcerpt(currentModel.html, {
words: 33
})
return `${excerpt}${excerpt.length !== stripHTML(currentModel.html).length ? '...' : ''}`;
}
return blog.description;
}
get slug() {
return this.currentRouteMeta?.id;
}
get categories() {
return this.currentRouteMeta?.tags?.mapBy('name');
}
get imgSrc() {
let url = blog.host ? `${blog.host}` : '';
url += this.currentRouteMeta.image || blog.rssLogo || blog.logo;
return url;
}
get url() {
// url is only ever valid if you have a host
if(!blog.host) {
return null;
}
// we remove any trailing / from the host and add it back in to make sure
// that we always have a consistent URL
const normalisedHost = blog.host.replace(/\/$/, '');
const normalisedUrl = this.router.currentURL.replace(/\/$/, '');
return `${normalisedHost}${normalisedUrl}/`;
}
get type() {
if(this.routeName === 'post') {
return 'article';
}
return 'website';
}
}