You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webpack-plugin/src/factories/page.js

111 lines
2.4 KiB

const path = require('path')
const slugify = require('slugify')
const merge = require('lodash.merge')
const nunjucks = require('nunjucks')
const assign = require('assign-deep')
const parseMarkdownFile = require('./../parsers/markdown.js')
/**
* Page
*
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/siteomat-webpack-plugin.git
*
*/
class Page {
/**
*
*
* @param {object} file
* @param {string} parent
* @param {string} fileString
* @param {object} [blocks=null]
*
*/
constructor(file, parent, fileString, blocks = {}) {
// parse file
const result = parseMarkdownFile(fileString)
// fields merge by default values
this._fields = merge({
view: 'page.njk',
1 year ago
type: 'html',
meta: {
robots: 'index'
}
}, result.fields)
1 year ago
// adding filename for html as pathname and relative path in structure
this._filename = this._resolveFilename(file)
this._pathname = this._resolvePathname(parent)
this._content = result.content
this._blocks = blocks
}
/**
* create Page Object
*
*
* @return {object}
*
*/
get() {
return assign({
'content' : this._content,
'blocks' : this._blocks,
'path' : this._pathname + '/' + this._filename,
'filename' : this._filename,
'pathname' : this._pathname
}, this._fields)
}
/**
* create html-filename = filename
*
* @param {string} file
* @return {string}
*
*/
_resolveFilename(file) {
let filename = file.name
if (filename === 'index.md') {
filename = 'index'
} else {
if (path.extname(filename) === '.md') {
filename = filename.replace('.md', '')
}
filename = slugify(filename)
}
1 year ago
return filename + '.' + this._fields.type
}
/**
* pathname = parent
*
* @param {string} parent
* @return {string}
*
*/
_resolvePathname(parent) {
let pathname = parent
if (parent === '/') {
pathname = ''
}
return pathname
}
}
module.exports = Page