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.
core/src/factories/page.js

207 lines
4.9 KiB

1 year ago
const path = require('path')
const slugify = require('slugify')
const merge = require('lodash.merge')
const nunjucks = require('nunjucks')
const assign = require('assign-deep')
10 months ago
const fs = require('fs')
1 year ago
1 year ago
const Media = require('./../factories/media.js')
1 year ago
const parseMarkdownFile = require('./../parsers/markdown.js')
10 months ago
const configStore = require('./../config.js')
1 year ago
/**
1 year ago
* Page - building from markdown file
1 year ago
*
*
* @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 = {}) {
1 year ago
// getting dirPath for files for page
this._dirPath = this._resolvePath(parent)
1 year ago
// parse file
10 months ago
const result = parseMarkdownFile(fileString, this._dirPath)
1 year ago
// fields merge by default values
this._fields = merge({
view: 'page.njk',
1 year ago
extensions: 'html',
1 year ago
meta: {
robots: 'index'
1 year ago
},
hidden: false
1 year ago
}, result.fields)
10 months ago
// adding filename for html as pathname and relative path in structure
this._filename = this._resolveFilename(file)
this._slug = this._resolveSlug(this._filename)
this._permalink = this._dirPath
if (this._slug) {
this._permalink = this._dirPath + '/' + this._slug
}
// check if page is in subdirectory
if (fs.existsSync(configStore.get('source') + this._permalink) && this._slug) {
this._dirPath += '/' + this._slug
this._filename = 'index'
}
1 year ago
1 year ago
this._filename += '.' + this._fields.extensions
10 months ago
this._content = result.content
this._blocks = blocks
1 year ago
// check for fields and resolve media
if (this._fields) {
10 months ago
this._fields = this._resolveMedia(this._fields, this._dirPath)
1 year ago
}
// check for fields and resolve media
if (this._blocks) {
for (const key of Object.keys(this._blocks)) {
if (Array.isArray(this._blocks[key])) {
this._blocks[key].forEach((fields, index) => {
10 months ago
this._blocks[key][index] = this._resolveMedia(fields, this._dirPath + '/_blocks')
1 year ago
})
} else {
10 months ago
this._blocks[key] = this._resolveMedia(this._blocks[key], this._dirPath + '/_blocks')
1 year ago
}
}
}
1 year ago
}
/**
* create Page Object
*
*
* @return {object}
*
*/
get() {
return assign({
1 year ago
'content' : this._content,
'blocks' : this._blocks,
'dirPath' : this._dirPath,
'path' : this._dirPath + '/' + this._filename,
1 year ago
'permalink' : this._permalink,
'filename' : this._filename
1 year ago
}, this._fields)
}
1 year ago
/**
*
*
*/
_resolveMedia(fields, dirPath) {
for (const key of Object.keys(fields)) {
if (key === 'src') {
fields[key] = this._resolveMediaSrc(fields[key], dirPath)
}
if (toString.call(fields[key]) === '[object Object]') {
fields[key] = this._resolveMedia(fields[key], dirPath)
}
}
return fields
}
10 months ago
/**
*
*
*/
1 year ago
_resolveMediaSrc(field, dirPath) {
10 months ago
1 year ago
const media = new Media(dirPath)
if (typeof field === 'string' || field instanceof String) {
field = media.resolve(field)
}
if (typeof field === 'object' || field instanceof Object) {
1 year ago
if (field.options) {
field = media.resolve(field.src, field.sizes, field.options)
} else {
field = media.resolve(field.src, field.sizes)
}
1 year ago
}
return field
}
1 year ago
/**
*
*
*/
_resolveSlug(filename) {
let slug = filename
if (slug === 'index') {
slug = ''
}
return slug
}
1 year ago
/**
* 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
1 year ago
}
/**
* pathname = parent
*
* @param {string} parent
* @return {string}
*
*/
1 year ago
_resolvePath(parent, slug) {
let path = parent
1 year ago
if (parent === '/') {
1 year ago
path = ''
1 year ago
}
1 year ago
return path
1 year ago
}
}
1 year ago
module.exports = Page