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 * * */ /** * 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 - * * * @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 } /** * getting url for assets of directus api * * @param string string * @param array array * @return string * */ function mediaUrl(id, options = null) { let query = ''; if (options) { query = '?' + new URLSearchParams(options).toString(); } return process.env.DIRECTUS_API_URL + '/assets/' + id + query; } export { asset, templateClass, isHome, injectStore, mediaUrl }