import slugify from 'slugify' import { marked } from 'marked' import * as fs from 'fs' import path from 'path' const basePath = path.join(path.resolve(), '/../../') /** * etaHelpers * * collection for helpers to extend eta * * */ /** * getting name of view as slug * * @param array $page * @return string * */ /**function canonical() { if (isset($_SERVER['HTTPS'])) { $canoncial = 'https'; } else { $canoncial = 'http'; } $canoncial .= '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; return $canoncial; }*/ /** * asset - checks manifest.json for given path and return * file path with id for cache busting * * * @param {String} publicPath * */ function asset(publicPath) { // getting basePath let result = publicPath // path to mix-manifest const file = basePath + 'mix-manifest.json' if (fs.existsSync(file)) { const manifest = fs.readFileSync(file) const files = JSON.parse(manifest) if (files[publicPath]) { result = files[publicPath] } } return result } /** * templateClass - parse template name to css-classname, * use prefix, default is "page" * * * @param {Object} entity * @param {String} [prefix='page'] * @return {String} * */ function templateClass(entity, prefix = 'page') { if (entity.data && entity.data.template) { prefix += '-' + entity.data.template } return prefix } /** * isHome - check if entity is home, * checks for permalink * * * @param {Object} entity * @return {Boolean} * */ function isHome(entity) { let result = false if (entity.data && entity.data.permalink && entity.data.permalink === "/") { result = true } return result } /** * injectStore - import store, check if store exists and * create object from class * * * @param {String} name * @return {Object} * */ async function injectStore(name) { const importPath = './../stores/' + name + '.js' const filePath = basePath + 'packages/server/stores/' + name + '.js' // if class not exists, throw exception if (!fs.existsSync(filePath)) { throw new Error(name + ' not exists!') } const StoreClass = await import(importPath) const store = new StoreClass.default() return store } /** * mediaUrl - create url for assets from directus, * default options are with access_token for permissions * * * @param {String} id * @param {Object} options * @return {String} * */ function mediaUrl(id, options = {}) { let query // merge options, default is access_token options = Object.assign({ 'access_token': process.env.DIRECTUS_API_TOKEN }, options) // create query query = '?' + new URLSearchParams(options).toString(); return process.env.DIRECTUS_API_URL + '/assets/' + id + query; } export { asset, templateClass, isHome, injectStore, mediaUrl }