Article server based
Required Sync changes!
This commit is contained in:
1 parent
6acb7ba64d
commit
be84058aba
18 files changed
+475
-114
No files matched your search
@@ -0,0 +1,91 @@
|
||||
const {readFile} = require('async-file')
|
||||
const handlebars = require('handlebars')
|
||||
|
||||
const url = require('url')
|
||||
const renderApp = require('../lib/component').renderApp
|
||||
const getHtml = require('../lib/component').getHtml
|
||||
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
|
||||
const urlToPattern = require('../lib/url').urlToPattern
|
||||
const isMatchPathPattern = require('../lib/url').isMatchPathPattern
|
||||
const isEqualPate = require('../lib/url').isEqualPath
|
||||
const {compose, any} = require('../lib/common')
|
||||
|
||||
const menuModel = require('../models/menu').default
|
||||
const config = require('../config')
|
||||
|
||||
function isActive(path, menuItem) {
|
||||
return isEqualPate(path, menuItem.url, true)
|
||||
}
|
||||
|
||||
function isHighlighted(path, menuItem) {
|
||||
return any(flatMenuItem(menuItem)
|
||||
.map(i => isMatchPathPattern(
|
||||
compose(urlToPattern, getPathWithNoTrailingSlash)(i.url),
|
||||
path,
|
||||
true
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
function flatMenuItem(item) {
|
||||
const rootUrl = item.url
|
||||
const flatUrls = items => {
|
||||
return items.map(item => ({
|
||||
...item,
|
||||
url: url.resolve(rootUrl, item.url)
|
||||
}))
|
||||
}
|
||||
|
||||
return !item.items
|
||||
? [item]
|
||||
: item.items.reduce((acc, i) => [...acc, ...compose(flatUrls, flatMenuItem)(i)], [item])
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes plain menu model and sample path and defines is an item of menu should be active
|
||||
*
|
||||
* @param path
|
||||
* @param menu
|
||||
* @return {{items}}
|
||||
*/
|
||||
function buildMenu(path, menu) {
|
||||
const items = menu.map(item => ({
|
||||
...item,
|
||||
active: isActive(path, item),
|
||||
highlighted: isHighlighted(path, item),
|
||||
}))
|
||||
return {items}
|
||||
}
|
||||
|
||||
const getTitle = meta => 'title' in meta
|
||||
? meta.title
|
||||
: 'Шлиссельбургская художественная школа'
|
||||
|
||||
const isHtml = data => typeof data === 'string'
|
||||
|
||||
async function render(path, data, meta) {
|
||||
const component = isHtml(data)
|
||||
? getHtml(data)
|
||||
: data
|
||||
|
||||
const menu = buildMenu(path, menuModel)
|
||||
const content = renderApp({menu, component})
|
||||
|
||||
const source = await readFile(config.viewMain, 'utf-8')
|
||||
const template = handlebars.compile(source)
|
||||
|
||||
const metaData = {
|
||||
...meta,
|
||||
description: 'Сайт Шлиссельбургской художественной школы',
|
||||
image: 'https://art.shlisselburg.org/entrance.jpg',
|
||||
url: `https://art.shlisselburg.org${path}`,
|
||||
}
|
||||
|
||||
return template({
|
||||
content,
|
||||
title: getTitle(meta),
|
||||
meta: metaData,
|
||||
})
|
||||
}
|
||||
|
||||
exports.render = render
|
||||
Reference in new issue
Block a user