adding pageQuery to rendering

develop
HerrHase 2 years ago
parent 8c5cebef39
commit 527a2d1b3a

@ -1,11 +1,21 @@
const HappySite = require('./src/happySite.js') const HappySite = require('./src/happySite.js')
const fs = require('fs')
class HappySiteWebpackPlugin { class HappySiteWebpackPlugin {
constructor(source, views) { constructor(source, views, destination = null) {
this._options = { this._options = {
source: source, source: source,
views: views views: views,
destination: destination
}
if (!fs.existsSync(source)) {
throw new Error('source "' + source + '" not found!')
}
if (!fs.existsSync(views)) {
throw new Error('views "' + views + '" not found!')
} }
} }
@ -21,28 +31,15 @@ class HappySiteWebpackPlugin {
// Compilation object gives us reference to some useful constants. // Compilation object gives us reference to some useful constants.
const { Compilation } = webpack const { Compilation } = webpack
// RawSource is one of the "sources" classes that should be used compiler.hooks.done.tap(pluginName, ({ compilation }) => {
// to represent asset sources in compilation.
const { RawSource } = webpack.sources
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
const compilationHash = compilation.hash
const webpackPublicPath = '.' + compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash })
// Tapping to the assets processing pipeline on a specific stage.
compilation.hooks.processAssets.tap({
name: pluginName,
// Using one of the later asset processing stages to ensure // if destination is null, use from webpack configuration
// that all assets were already added to the compilation by other plugins. if (!this._options.destination) {
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE this._options.destination = compilation.outputOptions.path
}, }
(assets) => { const happySite = new HappySite(this._options.source, this._options.views, this._options.destination)
const happySite = new HappySite(webpackPublicPath + this._options.source, this._options.views, webpackPublicPath) happySite.run()
happySite.run()
})
}) })
} }
} }

@ -1,3 +1,4 @@
title: "test" title: "test"
language: "en" language: "en"
domain: "test.io" domain: "test.io"
https: true

@ -1,7 +1,29 @@
{% macro meta(page) %} {# handle meta in templates #}
{% macro meta(page, site) %}
{% if (page.meta) %} {% if (page.meta) %}
{% set hasCannocial = false %}
{% for key, content in page.meta %} {% for key, content in page.meta %}
<meta name="{{ key }}" content="{{ content }}" /> <meta name="{{ key }}" content="{{ content }}" />
{# check for index in robots if set page has canonical #}
{% if (key === 'robots' and content.indexOf('index') !== -1 and content.indexOf('noindex') === -1) %}
{% set hasCannocial = true %}
{% endif %}
{% endfor %} {% endfor %}
{# check if page has canonical #}
{% if (hasCannocial) %}
{% set http = 'http' %}
{% if (site.https) %}
{% set http = site.https %}
{% endif %}
<link href="{{ http }}://{{ site.domain }}{{ page.path }}" rel="canonical">
<link rel="alternate" href="{{ http }}://{{ site.domain }}{{ page.path }}" hreflang="{{ site.language }}">
{% endif %}
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}

@ -3,6 +3,6 @@
{% block main %} {% block main %}
<img src="{{ page.media.teaser.src | resize({ width: 100 }) }}" /> <img src="{{ page.media.teaser.src | resize({ width: 100 }) }}" />
{{ page.content | safe }} {{ page.content | safe }}
{% endblock %} {% endblock %}

@ -2,7 +2,9 @@ const nunjucks = require('nunjucks')
const { minify } = require('html-minifier') const { minify } = require('html-minifier')
const fs = require('fs') const fs = require('fs')
const { asset, resize } = require('./engine.js') const { asset, resize } = require('./helpers.js')
const PageQuery = require('./queries/pages.js')
const configStore = require('./config.js')
/** /**
* engine - handle eta.js * engine - handle eta.js
@ -36,7 +38,8 @@ class Engine {
// adding defaults for view, function and data = config.yml // adding defaults for view, function and data = config.yml
this._defaults = { this._defaults = {
site: site, site: site,
asset: asset asset: asset,
pageQuery: new PageQuery(configStore.get('source'))
} }
} }

@ -62,7 +62,7 @@ class HappySite {
const sitemap = new Sitemap(this._site) const sitemap = new Sitemap(this._site)
// run through pages and generate html files // run through pages and generate html files
results.forEach((page) => { results.forEach((page, index) => {
page.render(this._engine, (error, content) => { page.render(this._engine, (error, content) => {
// show errors // show errors
@ -82,11 +82,13 @@ class HappySite {
}) })
sitemap.addPage(page) sitemap.addPage(page)
// if run is finish, write sitemap.xml
if ((index + 1) === results.length) {
fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString())
}
}) })
}) })
// write sitemap
fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString())
} }
} }

@ -1,7 +1,7 @@
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const Media = require('./../media.js') const Media = require('./media.js')
/** /**
* asset - checks manifest.json for given path and return * asset - checks manifest.json for given path and return
@ -50,4 +50,4 @@ async function resize(src, sizes, options, done)
done(null, src) done(null, src)
} }
export { asset, resize } module.exports = { asset, resize }

@ -98,4 +98,4 @@ class Media {
} }
} }
module.exports =Media module.exports = Media

@ -30,7 +30,8 @@ class Pages {
// default options for find // default options for find
this._options = { this._options = {
parent: '' parent: '',
deep: -1
} }
this._dirPath = dirPath this._dirPath = dirPath
@ -87,7 +88,12 @@ class Pages {
} }
// if directory going deep // if directory going deep
if (file.isDirectory()) { if (file.isDirectory() && (options.deep > 0 || options.deep === -1)) {
if (options.deep > 0) {
options.deep--
}
const childrenOptions = Object.assign({}, options, { const childrenOptions = Object.assign({}, options, {
'parent': options.parent + '/' + file.name 'parent': options.parent + '/' + file.name
}) })

@ -1,4 +1,4 @@
import HappySite from './src/happySite.js' const HappySite = require('./src/happySite.js')
const happySite = new HappySite('./resources/site', './resources/views', ) const happySite = new HappySite('./resources/site', './resources/views', './public')
happySite.run() happySite.run()
Loading…
Cancel
Save