fixed path separator on multi-system

This commit is contained in:
Klemek
2019-06-20 18:23:10 +02:00
parent 2552ac0ea2
commit 61308c20e7
2 changed files with 14 additions and 10 deletions
+5 -3
View File
@@ -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)
+9 -7
View File
@@ -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();
});