From 6dcb72d83a0a151f0c9a6807dc108b73b9c50ccc Mon Sep 17 00:00:00 2001 From: HerrHase Date: Fri, 11 Aug 2023 12:54:49 +0200 Subject: [PATCH] adding #20 #19 #17 --- src/factories/page.js | 54 +++++++++++++++++++++------------------- src/factories/sitemap.js | 18 ++++++++++++-- test/pages.js | 2 +- test/sitemap.js | 4 +-- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/factories/page.js b/src/factories/page.js index 40965fa..8d2913a 100644 --- a/src/factories/page.js +++ b/src/factories/page.js @@ -3,9 +3,11 @@ const slugify = require('slugify') const merge = require('lodash.merge') const nunjucks = require('nunjucks') const assign = require('assign-deep') +const fs = require('fs') const Media = require('./../factories/media.js') const parseMarkdownFile = require('./../parsers/markdown.js') +const configStore = require('./../config.js') /** * Page - building from markdown file @@ -33,19 +35,8 @@ class Page { // getting dirPath for files for page this._dirPath = this._resolvePath(parent) - // 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 - } - // parse file - const result = parseMarkdownFile(fileString, [ - this._permalink, this._dirPath - ]) + const result = parseMarkdownFile(fileString, this._dirPath) // fields merge by default values this._fields = merge({ @@ -57,17 +48,29 @@ class Page { hidden: false }, result.fields) - this._content = result.content - this._blocks = blocks + // 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' + } this._filename += '.' + this._fields.extensions + this._content = result.content + this._blocks = blocks + // check for fields and resolve media if (this._fields) { - this._fields = this._resolveMedia(this._fields, [ - this._permalink, - this._dirPath - ]) + this._fields = this._resolveMedia(this._fields, this._dirPath) } // check for fields and resolve media @@ -75,16 +78,10 @@ class Page { for (const key of Object.keys(this._blocks)) { if (Array.isArray(this._blocks[key])) { this._blocks[key].forEach((fields, index) => { - this._blocks[key][index] = this._resolveMedia(fields, [ - this._permalink + '/_blocks', - this._dirPath + '/_blocks' - ]) + this._blocks[key][index] = this._resolveMedia(fields, this._dirPath + '/_blocks') }) } else { - this._blocks[key] = this._resolveMedia(this._blocks[key], [ - this._permalink + '/_blocks', - this._dirPath + '/_blocks' - ]) + this._blocks[key] = this._resolveMedia(this._blocks[key], this._dirPath + '/_blocks') } } } @@ -126,7 +123,12 @@ class Page { return fields } + /** + * + * + */ _resolveMediaSrc(field, dirPath) { + const media = new Media(dirPath) if (typeof field === 'string' || field instanceof String) { diff --git a/src/factories/sitemap.js b/src/factories/sitemap.js index 09a4619..5048e26 100644 --- a/src/factories/sitemap.js +++ b/src/factories/sitemap.js @@ -1,5 +1,6 @@ const { XMLParser, XMLBuilder, XMLValidator} = require('fast-xml-parser') const dayjs = require('dayjs') +const assign = require('assign-deep') /** * @@ -19,7 +20,11 @@ class Sitemap { * */ constructor(site) { - this._site = site + this._site = assign({ + 'sitemap': { + 'use_permalinks': true + } + }, site) this._urls = [] } @@ -31,8 +36,17 @@ class Sitemap { */ addPage(page) { if (this._isValid(page)) { + + let path = page.permalink + + console.log(this._site) + + if (this._site.sitemap.use_permalinks === false) { + path = page.path + } + this._urls.push({ - loc: 'https://' + this._site.domain + page.path, + loc: 'https://' + this._site.domain + path, lastmod: dayjs().format() }) } diff --git a/test/pages.js b/test/pages.js index 0d7e652..cfca229 100644 --- a/test/pages.js +++ b/test/pages.js @@ -54,7 +54,7 @@ describe('Page /blog/index.md', function () { }) it('path', function() { - assert.equal(page.path, '/blog.html') + assert.equal(page.path, '/blog/index.html') }) it('permalink', function() { diff --git a/test/sitemap.js b/test/sitemap.js index 666f9b0..1eda2f5 100644 --- a/test/sitemap.js +++ b/test/sitemap.js @@ -25,10 +25,10 @@ describe('Sitemap', function () { // check results it('loc-tag with url', function() { - assert.match(sitemap.getXmlAsString(), /https:\/\/test.lan\/blog\/article.html<\/loc>/) + assert.match(sitemap.getXmlAsString(), /https:\/\/test.lan\/blog\/article<\/loc>/) }) it('loc-tag has robotos:noindex and has missing', function() { - assert.notMatch(sitemap.getXmlAsString(), /https:\/\/test.lan\/index.html<\/loc>/) + assert.notMatch(sitemap.getXmlAsString(), /https:\/\/test.lan\/<\/loc>/) }) })