diff --git a/package.json b/package.json index 2061727..34b7080 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@site-o-mat/core", - "version": "0.4.0", + "version": "0.4.1", "build": "webpack", "author": "Björn Hase ", "main": "index.js", diff --git a/src/factories/media.js b/src/factories/media.js index 84816cf..9ad0277 100644 --- a/src/factories/media.js +++ b/src/factories/media.js @@ -21,8 +21,8 @@ const configStore = require('./../config.js') class Media { - constructor(path = NULL) { - this._path = path + constructor(dirPath = NULL) { + this._path = dirPath this._DIR_ASSETS = '/assets/' } @@ -43,11 +43,7 @@ class Media { const filename = slugify(path.basename(src, extension)) // check for images in path - if (this._path && fs.existsSync(configStore.get('source') + this._path + '/' + src)) { - sourcePath = configStore.get('source') + this._path + '/' + src - } else { - sourcePath = configStore.get('source') + '/' + src - } + sourcePath = this._getSourcePath(src) // getting sharp const process = sharp(sourcePath) @@ -92,6 +88,30 @@ class Media { return this._reduce(results) } + /** + * get source path + * + * @param {String} src + * @return {String} + * + */ + _getSourcePath(src) { + + let sourcePath = configStore.get('source') + '/' + src + + if (Array.isArray(this._path)) { + if (fs.existsSync(configStore.get('source') + this._path[0] + '/' + src)) { + sourcePath = configStore.get('source') + this._path[0] + '/' + src + } else if (fs.existsSync(configStore.get('source') + this._path[1] + '/' + src)) { + sourcePath = configStore.get('source') + this._path[1] + '/' + src + } + } else if (this._path && fs.existsSync(configStore.get('source') + this._path + '/' + src)) { + sourcePath = configStore.get('source') + this._path + '/' + src + } + + return sourcePath + } + /** * if only full is in results, reduce object to string * diff --git a/src/factories/page.js b/src/factories/page.js index 91ccd55..40965fa 100644 --- a/src/factories/page.js +++ b/src/factories/page.js @@ -33,8 +33,19 @@ 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._dirPath) + const result = parseMarkdownFile(fileString, [ + this._permalink, this._dirPath + ]) // fields merge by default values this._fields = merge({ @@ -46,23 +57,17 @@ class Page { hidden: false }, result.fields) - // 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 - } - - this._filename += '.' + this._fields.extensions - this._content = result.content this._blocks = blocks + this._filename += '.' + this._fields.extensions + // check for fields and resolve media if (this._fields) { - this._fields = this._resolveMedia(this._fields, this._dirPath) + this._fields = this._resolveMedia(this._fields, [ + this._permalink, + this._dirPath + ]) } // check for fields and resolve media @@ -70,10 +75,16 @@ 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._dirPath + '/_blocks') + this._blocks[key][index] = this._resolveMedia(fields, [ + this._permalink + '/_blocks', + this._dirPath + '/_blocks' + ]) }) } else { - this._blocks[key] = this._resolveMedia(this._blocks[key], this._dirPath + '/_blocks') + this._blocks[key] = this._resolveMedia(this._blocks[key], [ + this._permalink + '/_blocks', + this._dirPath + '/_blocks' + ]) } } } diff --git a/test/pages.js b/test/pages.js index 50ba339..0d7e652 100644 --- a/test/pages.js +++ b/test/pages.js @@ -63,9 +63,9 @@ describe('Page /blog/index.md', function () { it('fields has media src', function() { assert.deepEqual(page.media.src, { - "300": "/assets/88c010ea/4ca9b5f5/6024c57d/05899fae/a33d9a45/dog300.webp", - "500x100": "/assets/88c010ea/4ca9b5f5/6024c57d/05899fae/a33d9a45/dog500x100.webp", - "full": "/assets/88c010ea/4ca9b5f5/6024c57d/05899fae/a33d9a45/dog.webp" + "300": "/assets/a6c45d17/11bf0a4e/a2b1d75d/dc85ca56/71c63294/dog300.webp", + "500x100": "/assets/a6c45d17/11bf0a4e/a2b1d75d/dc85ca56/71c63294/dog500x100.webp", + "full": "/assets/a6c45d17/11bf0a4e/a2b1d75d/dc85ca56/71c63294/dog.webp" }) }) })