From 61308c20e79efb0bc1ad701f5f4a876f309e8189 Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 20 Jun 2019 18:23:10 +0200 Subject: [PATCH] fixed path separator on multi-system --- src/file_walker.js | 8 +++++--- test/file_walker.test.js | 16 +++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/file_walker.js b/src/file_walker.js index d62b9e6..b0bdcae 100644 --- a/src/file_walker.js +++ b/src/file_walker.js @@ -1,6 +1,8 @@ const fs = require('fs'); const path = require('path'); +const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/'); + /** * Get all files path inside a given folder path * @param dir @@ -78,7 +80,7 @@ module.exports = (config) => { let remaining = 0; paths.forEach((p) => { const article = { - path: path.join(p[0], p[1], p[2]), + path: joinUrl(p[0], p[1], p[2]), realPath: path.join(config['data_dir'], p[0], p[1], p[2]), year: parseInt(p[0]), month: parseInt(p[1]), @@ -91,9 +93,9 @@ module.exports = (config) => { if (err) return cb(err); article.title = info.title || config['article']['default_title']; - article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail']; + article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail']; article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_'); - article.url = '/' + path.join(article.path, article.escapedTitle) + '/'; + article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/'; articles[article.path] = article; remaining--; if (remaining === 0) diff --git a/test/file_walker.test.js b/test/file_walker.test.js index 20ec67b..4eb6c2c 100644 --- a/test/file_walker.test.js +++ b/test/file_walker.test.js @@ -6,6 +6,8 @@ const utils = require('./test_utils'); const dataDir = 'test_data'; const testIndex = 'testindex.md'; +const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/'); + const config = { 'test': true, 'data_dir': dataDir, @@ -226,8 +228,8 @@ describe('Test article fetching', () => { expect(err).toBeNull(); expect(dict).toBeDefined(); expect(Object.keys(dict).length).toBe(1); - expect(dict[path.join('2019', '05', '05')]).toEqual({ - path: path.join('2019', '05', '05'), + expect(dict[joinUrl('2019', '05', '05')]).toEqual({ + path: joinUrl('2019', '05', '05'), realPath: dir, year: 2019, month: 5, @@ -236,7 +238,7 @@ describe('Test article fetching', () => { title: 'Untitled', thumbnail: 'default.png', escapedTitle: 'untitled', - url: '/' + path.join('2019', '05', '05', 'untitled') + '/', + url: '/' + joinUrl('2019', '05', '05', 'untitled') + '/', }); done(); }); @@ -256,17 +258,17 @@ describe('Test article fetching', () => { expect(err).toBeNull(); expect(dict).toBeDefined(); expect(Object.keys(dict).length).toBe(1); - expect(dict[path.join('2019', '05', '05')]).toEqual({ - path: path.join('2019', '05', '05'), + expect(dict[joinUrl('2019', '05', '05')]).toEqual({ + path: joinUrl('2019', '05', '05'), realPath: dir, year: 2019, month: 5, day: 5, date: date, title: 'Title with : info !', - thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'), + thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'), escapedTitle: 'title_with___info', - url: '/' + path.join('2019', '05', '05', 'title_with___info') + '/', + url: '/' + joinUrl('2019', '05', '05', 'title_with___info') + '/', }); done(); });