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
@@ -3,13 +3,20 @@ import {renderToStaticMarkup} from 'react-dom/server'
|
||||
|
||||
const App = require('../components/App')
|
||||
|
||||
function renderApp({menu, page}) {
|
||||
function renderApp({menu, component}) {
|
||||
const app = (
|
||||
<App menu={menu}>
|
||||
<div dangerouslySetInnerHTML={{__html: page.data}}/>
|
||||
{component}
|
||||
</App>
|
||||
)
|
||||
return renderToStaticMarkup(app)
|
||||
}
|
||||
|
||||
function getHtml(html) {
|
||||
return (
|
||||
<div dangerouslySetInnerHTML={{__html: html}}/>
|
||||
)
|
||||
}
|
||||
|
||||
exports.renderApp = renderApp
|
||||
exports.getHtml = getHtml
|
||||
@@ -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
|
||||
+27
-1
@@ -1,7 +1,10 @@
|
||||
function getPathWithNoTrailingSlash(path) {
|
||||
return path.charAt(path.length - 1) === '/'
|
||||
path = path.charAt(path.length - 1) === '/'
|
||||
? path.substr(0, path.length - 1)
|
||||
: path
|
||||
return path.length === 0
|
||||
? '/'
|
||||
: path
|
||||
}
|
||||
|
||||
function isEqualPath(a, b, ignoreTrailingSlash = false) {
|
||||
@@ -10,5 +13,28 @@ function isEqualPath(a, b, ignoreTrailingSlash = false) {
|
||||
: a === b
|
||||
}
|
||||
|
||||
function isMatchPathPattern(pattern, path, ignoreTrailingSlash = false) {
|
||||
const test = ignoreTrailingSlash
|
||||
? getPathWithNoTrailingSlash(path)
|
||||
: path
|
||||
return pattern.test(test)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* /article/* -> ^\/article\/[/\w\d-_]+\/?$
|
||||
*
|
||||
* @param url
|
||||
* @return {RegExp}
|
||||
*/
|
||||
function urlToPattern(url) {
|
||||
const u = url.replace('/*', '[/\\w\\d-_]+')
|
||||
const pattern = `^${u}/?$`
|
||||
|
||||
return new RegExp(pattern)
|
||||
}
|
||||
|
||||
exports.getPathWithNoTrailingSlash = getPathWithNoTrailingSlash
|
||||
exports.isMatchPathPattern = isMatchPathPattern
|
||||
exports.urlToPattern = urlToPattern
|
||||
exports.isEqualPath = isEqualPath
|
||||
Reference in new issue
Block a user