adding pageQuery to rendering

develop
HerrHase 2 years ago
parent 8c5cebef39
commit 527a2d1b3a

@ -1,11 +1,21 @@
const HappySite = require('./src/happySite.js')
const fs = require('fs')
class HappySiteWebpackPlugin {
constructor(source, views) {
constructor(source, views, destination = null) {
this._options = {
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,29 +31,16 @@ class HappySiteWebpackPlugin {
// Compilation object gives us reference to some useful constants.
const { Compilation } = webpack
// RawSource is one of the "sources" classes that should be used
// to represent asset sources in compilation.
const { RawSource } = webpack.sources
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compiler.hooks.done.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
// that all assets were already added to the compilation by other plugins.
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
},
// if destination is null, use from webpack configuration
if (!this._options.destination) {
this._options.destination = compilation.outputOptions.path
}
(assets) => {
const happySite = new HappySite(webpackPublicPath + this._options.source, this._options.views, webpackPublicPath)
const happySite = new HappySite(this._options.source, this._options.views, this._options.destination)
happySite.run()
})
})
}
}

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

@ -1,7 +1,29 @@
{% macro meta(page) %}
{# handle meta in templates #}
{% macro meta(page, site) %}
{% if (page.meta) %}
{% set hasCannocial = false %}
{% for key, content in page.meta %}
<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 %}
{# 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 %}
{% endmacro %}

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

@ -2,7 +2,9 @@ const nunjucks = require('nunjucks')
const { minify } = require('html-minifier')
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
@ -36,7 +38,8 @@ class Engine {
// adding defaults for view, function and data = config.yml
this._defaults = {
site: site,
asset: asset
asset: asset,
pageQuery: new PageQuery(configStore.get('source'))
}
}

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

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

@ -30,7 +30,8 @@ class Pages {
// default options for find
this._options = {
parent: ''
parent: '',
deep: -1
}
this._dirPath = dirPath
@ -87,7 +88,12 @@ class Pages {
}
// 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, {
'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()
Loading…
Cancel
Save