change engine, adding template functions

develop
HerrHase 2 years ago
parent 19a4579134
commit 560ba036ca

@ -0,0 +1,4 @@
{
"/spritemap.js": "/spritemap.js?id=2dda73ecee3bb668b395026efda6524c",
"/public/css/styles.css": "/public/css/styles.css?id=c80a1181f87f3c3ef715844b545559f4"
}

@ -5,7 +5,7 @@
"packages/*"
],
"scripts": {
"build": "yarn workspace server run build",
"build": "yarn workspace frontend run build",
"start": "yarn workspace server run start"
}
}

@ -1,6 +1,9 @@
{
"name": "frontend",
"version": "0.1.0",
"scripts": {
"build": "npx mix"
},
"dependencies": {
"@tiny-components/plain-ui": "^0.6.0",
"riot": "^6.1.2"

@ -0,0 +1,3 @@
@import
'@tiny-components/plain-ui/src/scss/plain-ui';

@ -2,7 +2,7 @@
{% block siteHeader %}
<h1 class="site-header__title">
{ site.404_title }
{{ page.data.title }}
</h1>
{% endblock %}
@ -11,7 +11,9 @@
<div class="grid">
<div class="col-12">
<div class="content">
{ marked.parse(site.404_message) }
{% if settings.404_message %}
{{ settings.404_message }}
{% endif %}
</div>
</div>
</div>

@ -0,0 +1,43 @@
<!doctype html>
<html lang="{{ it.settings.data.default_language }}">
<head>
<meta charset="utf-8">
<title>
{{ it.page.data.title }} | {{ it.settings.data.project_name }}
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/static/css/styles.css" rel="stylesheet" type="text/css">
</head>
<body class="site-body {{ it.templateClass(it.page) }}">
<header class="site-header">
<div class="bar">
<div class="bar__start">
{{# if (isHome(it.page)) { }}
<h1 class="site-header__title">
{{ it.page.data.title }}
</h1>
{{# } else { }}
<a class="site-header__title">
{{ it.page.data.title }}
</a>
{{# } }}
</div>
</div>
</header>
<main class="site-main">
{{! it.body }}
</main>
<footer class="site-footer">
<div class="site-footer__copyright">
</div>
</footer>
</body>
</html>

@ -1,46 +0,0 @@
<!doctype html>
<html lang="en_EN">
<head>
<meta charset="utf-8">
<title>
{{ page.data.title }}
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/css/styles.css" rel="stylesheet" type="text/css">
{% block head %}{% endblock %}
</head>
<body class="site-body page-{ siteClass(page) (page.data.slug) ? slug : 'home' }">
<header class="site-header">
{% block siteHeader %}
<h1 class="site-header__title">
{{ page.data.title }}
{{ options.data.title }}
</h1>
{% endblock %}
</header>
<main class="site-main">
{% block siteMain %}
{% endblock %}
</main>
<footer class="site-footer">
{% block siteFooter %}
<div class="">
</div>
{% endblock %}
<div class="site-footer__copyright">
</div>
</footer>
{% block script %}{% endblock %}
</body>
</html>

@ -0,0 +1,12 @@
{% layout('layout') %}
{{# const posts = await it.injectStore('post') }}
{{# if posts.data.length > 0 }}
{{# posts.data.forEach((post) => { }}
{{ post.title }}
{{ marked.parse(page.data.content) }}
{{# }) }}
{{# } else { }}
{{# } }}

@ -1,13 +0,0 @@
{% layout 'layout.liquid' %}
{% block siteMain %}
<div class="container">
<div class="grid">
<div class="col-12">
<div class="content">
{ marked.parse(page.data.content) }
</div>
</div>
</div>
</div>
{% endblock %}

@ -1,13 +1,11 @@
{% layout 'layout.liquid' %}
{{# layout('layout') }}
{% block siteMain %}
<div class="container">
<div class="grid">
<div class="col-12">
<div class="content">
{{ page.data.content }}
{{! it.parseMarkdown(it.page.data.content) }}
</div>
</div>
</div>
</div>
{% endblock %}
</div>

@ -0,0 +1,84 @@
const mix = require('laravel-mix')
const path = require('path')
require('laravel-mix-purgecss')
// plugins
const SvgSpritemapPlugin = require('svg-spritemap-webpack-plugin')
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.webpackConfig({
module: {
rules: [{
test: /\.riot$/,
use: [{
loader: '@riotjs/webpack-loader',
options: {
hot: false
}
}]
}
]},
plugins: [
new SvgSpritemapPlugin([
'./node_modules/@tiny-components/plain-ui/src/icons/mono-icons/svg/*.svg',
'./icons/*.svg'
], {
output: {
filename: 'public/symbol-defs.svg',
chunk: {
keep: true
},
svgo: {
plugins: [{
name: 'convertStyleToAttrs',
active: true
},{
name: 'removeStyleElement',
active: true
}, {
name: 'removeAttrs',
params: {
attrs: 'fill'
}
}]
}
},
sprite: {
prefix: 'icon-'
}
})
]
})
mix
.setPublicPath('../../')
//.js('js/app.js', 'public/js')
.sass('scss/styles.scss', 'public/css')
.purgeCss({
extend: {
content: [
path.join(__dirname, 'js/**/*.riot'),
path.join(__dirname, 'views/*.liquid')
]
}
})
.options({
terser: {
extractComments: false,
},
processCssUrls: false
})
.copyDirectory('node_modules/@tiny-components/plain-ui/src/fonts/**', '../../public/css')
.version()

@ -15,28 +15,42 @@ const server = fastify()
*
*/
import { Liquid } from 'liquidjs'
import * as Eta from 'eta'
import view from '@fastify/view'
const engine = new Liquid({
root: path.join(path.resolve(), '/../frontend/views'),
extname: '.liquid',
})
server.register(view, {
engine: {
liquid: engine
eta: Eta
},
root: path.join(path.resolve(), '/../frontend/views'),
extname: '.eta',
options: {
tags: [ '{{', '}}' ],
includeViewExtension: true,
parse: {
exec: "#",
interpolate: "",
raw: "!"
},
useWith: true,
async: true
}
})
/**
* adding preHandler
*
*
*/
import SettingsStore from './stores/Settings.js'
import SettingsStore from './stores/settings.js'
import templateClass from './eta/templateClass.js'
import isHome from './eta/isHome.js'
import injectStore from './eta/injectStore.js'
import parseMarkdown from './eta/parseMarkdown.js'
// getting options from directus add to all views
server.addHook('preHandler', async function (request, response) {
@ -44,7 +58,11 @@ server.addHook('preHandler', async function (request, response) {
const settings = await settingsStore.find()
response.locals = {
settings: settings
settings: settings,
templateClass: templateClass,
isHome: isHome,
injectStore: injectStore,
parseMarkdown: parseMarkdown
}
})
@ -53,30 +71,27 @@ server.addHook('onRequest', async function (request, response) {
const url = request.url
let pathname = url.split('/')
pathname.shift()
let pathname = url.split('/').shift()
// default value for paged
let paged = 1
// check if pathname has values
if (pathname.length > 0) {
// check if pathname has values and is not 404
if (pathname.length > 0 && request.url !== '/404') {
// try to get number
const result = Number(pathname[pathname.length - 1])
// is result is integer
// is result is an integer
if (Number.isInteger(result)) {
paged = result
// remove last element
// remove last from pathname, add default url to request,
// and check if pathname has more than one, join pathname for new url
pathname.pop()
request.raw.url = '/'
// @TODO change, thats looks strange
if (pathname.length > 0) {
request.raw.url = pathname.join('/')
} else {
request.raw.url = '/'
}
}
}

@ -0,0 +1,14 @@
import slugify from 'slugify'
/**
*
*
*
*/
export default async function injectStore(name) {
const StoreClass = await import('./../stores/' + name + '.js')
const store = new StoreClass.default()
return await store.find()
}

@ -0,0 +1,18 @@
import slugify from 'slugify'
/**
*
*
*
*/
export default function isHome(entity) {
let result = false
if (entity.data && entity.data.permalink && entity.data.permalink === "/") {
result = true
}
return result
}

@ -0,0 +1,11 @@
import { marked } from 'marked'
/**
*
*
*
*/
export default function parseMarkdown(value) {
return marked.parse(value)
}

@ -0,0 +1,15 @@
import slugify from 'slugify'
/**
*
*
*
*/
export default function templateClass(entity, prefix = 'page') {
if (entity.data && entity.data.template) {
prefix += '-' + entity.data.template
}
return prefix
}

@ -0,0 +1,117 @@
/**
* function similar to blade asset
*
* @param $path
* @return string
*
*/
function asset($path, $prefix = '/public')
{
// get flight
$app = Flight::app();
// getting basePath
$basePath = $app->get('basePath');
// path to mix-manifest
$file = $app->get('basePath').'mix-manifest.json';
if (file_exists($file)) {
$manifest = file_get_contents($file);
$files = json_decode($manifest, true);
if (isset($files[$prefix.$path])) {
$path = str_replace($prefix, '', $files[$prefix.$path]);
}
}
return $path;
}
/**
* getting name of view as slug
*
* @param array $page
* @return string
*
*/
function viewName(array $page)
{
$slugify = new \Cocur\Slugify\Slugify();
return $slugify->slugify($page['data']['view']);
}
/**
* 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;
}
/**
* getting title for head
*
* @param array $page
* @param array $site
* @return string
*/
function title($page, $site)
{
$title = [];
if (isset($site['data']['title'])) {
$title[] = $site['data']['title'];
}
// if not homepage set page title first
if ($page['data']['slug']) {
array_unshift($title, $page['data']['title']);
} else {
$title[] = $page['data']['title'];
}
return join(' | ', $title);
}
/**
* getting url for assets of directus api
*
* @param string string
* @param array array
* @return string
*
*/
function assetsUrl(string $id, array $options = NULL)
{
$query = NULL;
if ($options) {
$query = '?'.http_build_query($options);
}
return $_ENV['DIRECTUS_API_URL'].'/assets/'.$id.$query;
}
function styles($values) {
$result = '';
if ($values && is_array($values)) {
$result = implode(' ', $values);
}
return $result;
}

@ -12,8 +12,8 @@
const directusResponseHandler = {
single(entity, response) {
// if data is empty redirect to 404
if (entity.data.length === 0) {
// if data not exists or data is empty redirect to 404
if (!entity.data || entity.data.length === 0) {
return response.redirect('/404')
}
@ -25,7 +25,7 @@ const directusResponseHandler = {
entity.data.template = 'default'
}
return entity
response.locals.page = entity
}
}

@ -17,9 +17,18 @@ export default async function(fastify, opts) {
*
*/
fastify.get('/404', async function(request, response) {
const page = {
data: {
title: '404'
}
}
return response
.code(404)
.view('../frontend/views/404')
.view('../frontend/views/404', {
page: page
})
})
}

@ -1,6 +1,6 @@
import path from 'path'
import PageStore from './../stores/Page.js'
import PageStore from './../stores/page.js'
import directusResponseHandler from './../handlers/directusResponse.js'
@ -29,7 +29,8 @@ export default async function(fastify, opts) {
// getting single page
const page = await pageStore.findOneByPermalink(request.url)
response.page = directusResponseHandler.single(page, response)
directusResponseHandler.single(page, response)
})
/**
@ -40,8 +41,6 @@ export default async function(fastify, opts) {
*
*/
fastify.get('/*', async function(request, response) {
return response.view('../frontend/views/templates/page/' + response.page.data.template, {
'page' : response.page
})
return response.view('../views/templates/page/' + response.locals.page.data.template)
})
}

@ -1,4 +1,4 @@
import PageStore from './../stores/Post.js'
import PageStore from './../stores/post.js'
import path from 'path'
/**
@ -38,8 +38,6 @@ export default async function(fastify, opts) {
*
*/
fastify.get('/:slug([-a-zA-Z0-9]{0,255})', async function(request, response) {
response.view('../frontend/views/templates/post/' + response.post.data.template, {
'page' : post
})
response.view('../views/templates/post/' + response.locals.page.data.template)
})
}

@ -1,6 +1,6 @@
import path from 'path'
import PageStore from './../stores/Page.js'
import PageStore from './../stores/page.js'
import directusResponseHandler from './../handlers/directusResponse.js'
@ -51,6 +51,6 @@ export default async function(fastify, opts) {
})
})
}

@ -1,7 +1,8 @@
{
"name": "server",
"requires": true,
"version": "0.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@directus/sdk": {
"version": "9.14.2",
@ -223,6 +224,11 @@
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="
},
"eta": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/eta/-/eta-1.12.3.tgz",
"integrity": "sha512-qHixwbDLtekO/d51Yr4glcaUJCIjGVJyTzuqV4GPlgZo1YpgOKG+avQynErZIYrfM6JIJdtiG2Kox8tbb+DoGg=="
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
@ -386,11 +392,6 @@
"set-cookie-parser": "^2.4.1"
}
},
"liquidjs": {
"version": "9.39.1",
"resolved": "https://registry.npmjs.org/liquidjs/-/liquidjs-9.39.1.tgz",
"integrity": "sha512-TFu19Cml0K2T34UY50ZKUApHgiiEaayqNW9J3o3ubLQCuhMFhY7jqwNpY1oVvvAJxCCxVqL6bJnGc1Fktth0Fw=="
},
"lru-cache": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
@ -618,6 +619,11 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
},
"slugify": {
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.6.5.tgz",
"integrity": "sha512-8mo9bslnBO3tr5PEVFzMPIWwWnipGS0xVbYf65zxDqfNwmzYn1LpiKNrR6DlClusuvo+hDHd1zKpmfAe83NQSQ=="
},
"sonic-boom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-3.0.0.tgz",

@ -12,8 +12,9 @@
"dayjs": "^1.11.4",
"dompurify": "^2.3.10",
"dotenv": "^10.0.0",
"eta": "^1.12.3",
"fastify": "^4.2.1",
"liquidjs": "^9.39.1",
"marked": "^4.0.18"
"marked": "^4.0.18",
"slugify": "^1.6.5"
}
}

@ -1,4 +1,4 @@
import DirectusAbstractStore from './DirectusAbstract.js'
import DirectusAbstractStore from './directusAbstract.js'
/**
* Comments

@ -1,4 +1,4 @@
import DirectusAbstractStore from './DirectusAbstract.js'
import DirectusAbstractStore from './directusAbstract.js'
/**
* Pages from Directus
@ -21,7 +21,7 @@ class PageStore extends DirectusAbstractStore {
/**
* getting page by permalink
*
*
*
* @param {string} permalink
* @return {object}

@ -1,4 +1,4 @@
import DirectusAbstractStore from './DirectusAbstract.js'
import DirectusAbstractStore from './directusAbstract.js'
/**
* Posts from Directus
@ -26,10 +26,11 @@ class PostStore extends DirectusAbstractStore {
* @return {object}
*/
find(page = 1, limit = 20) {
return this.items.readOne({
return this.items.readByQuery({
fields: [
'title',
'slug',
'status',
'user_created.first_name',
'user_created.last_name',
@ -40,7 +41,6 @@ class PostStore extends DirectusAbstractStore {
'teaser_media.description',
],
filter: {
slug : slug,
status : 'published',
published_at : {
'_nnull': true

@ -1,4 +1,4 @@
import DirectusAbstractStore from './DirectusAbstract.js'
import DirectusAbstractStore from './directusAbstract.js'
/**
* Settings

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
(self["webpackChunkfrontend"] = self["webpackChunkfrontend"] || []).push([["spritemap"],{
/***/ "?c20d":
/*!******************************!*\
!*** spritemap-dummy-module ***!
\******************************/
/***/ (() => {
/***/ })
}]);
Loading…
Cancel
Save