|
|
|
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 path = './../stores/' + name + '.js'
|
|
|
|
|
|
|
|
// if file
|
|
|
|
//if (!fs.existsSync(path)) {
|
|
|
|
//throw new Error(name + ' not exists!')
|
|
|
|
//}
|
|
|
|
|
|
|
|
const StoreClass = await import(path)
|
|
|
|
const store = new StoreClass.default()
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getting url for assets of directus api
|
|
|
|
*
|
|
|
|
* @param string string
|
|
|
|
* @param array array
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function fileUrl(id, options = NULL)
|
|
|
|
{
|
|
|
|
$query = NULL;
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
$query = '?'.http_build_query($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return process.ENV.DIRECTUS_API_URL + '/assets/' + id + $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { asset, templateClass, isHome, injectStore }
|