file walker now read title and thumbnail infos

This commit is contained in:
Klemek
2019-06-19 19:41:29 +02:00
parent 1c30f95677
commit 9300bfbb78
5 changed files with 169 additions and 51 deletions
+1 -27
View File
@@ -26,33 +26,7 @@ npm install
cd gitblog.md cd gitblog.md
cp config.example.json config.json cp config.example.json config.json
``` ```
then edit the config.json file with your values : then edit the config.json file with your custom values.
> default values for config.json
````json
{
"nodePort": 3000,
"dataDir": "data",
"modules" : {
"plantuml" : false,
"rss": true,
"webhook": true
},
"home" : {
"index" : "index.ejs"
},
"article" : {
"index" : "index.md"
},
"rss" : {
"endpoint" : "/rss",
"length" : 10
},
"webhook" : {
"endpoint": "/webhook",
"secretFile": "git_secret"
}
}
````
**3. Start your server** **3. Start your server**
+4 -1
View File
@@ -10,7 +10,10 @@
"index" : "index.ejs" "index" : "index.ejs"
}, },
"article" : { "article" : {
"index" : "index.md" "index" : "index.md",
"thumbnail_tag" : "thumbnail",
"default_title": "Untitled",
"default_thumbnail" : null
}, },
"rss" : { "rss" : {
"endpoint" : "/rss", "endpoint" : "/rss",
+1 -1
View File
@@ -15,7 +15,7 @@ const merge = (ref, src) => {
module.exports = () => { module.exports = () => {
try { try {
let configData = fs.readFileSync('./config.json'); let configData = fs.readFileSync('./config.json', {encoding:'UTF-8'});
let config = JSON.parse(configData); let config = JSON.parse(configData);
return merge(refConfig, config); return merge(refConfig, config);
} catch (error) { } catch (error) {
+37 -9
View File
@@ -1,6 +1,6 @@
const fs = require('fs'); const fs = require('fs');
const fileTree = (path, cb) => { const getFileTree = (path, cb) => {
let list = []; let list = [];
let remaining = 0; let remaining = 0;
fs.readdir(path, {withFileTypes: true}, (err, items) => { fs.readdir(path, {withFileTypes: true}, (err, items) => {
@@ -9,7 +9,7 @@ const fileTree = (path, cb) => {
items.forEach((item) => { items.forEach((item) => {
if (item.isDirectory()) { if (item.isDirectory()) {
remaining++; remaining++;
fileTree(`${path}/${item.name}`, (err, out) => { getFileTree(`${path}/${item.name}`, (err, out) => {
if (err) if (err)
return cb(err); return cb(err);
list.push(...out); list.push(...out);
@@ -26,16 +26,30 @@ const fileTree = (path, cb) => {
}); });
}; };
const readIndexFile = (path,cb) => { const readIndexFile = (path, thumbnailTag, cb) => {
//TODO reading page title and possibly ![thumbnail](url) fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
let info = {};
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
cb(null, info);
});
}; };
module.exports = (config) => { module.exports = (config) => {
return { return {
fileTree: config['test'] ? fileTree : undefined, fileTree: config['test'] ? getFileTree : undefined,
readIndexFile: config['test'] ? readIndexFile : undefined, readIndexFile: config['test'] ? readIndexFile : undefined,
fetchArticles: (cb) => { fetchArticles: (cb) => {
fileTree(config['data_dir'], (err, fileList) => { getFileTree(config['data_dir'], (err, fileList) => {
if (err) if (err)
return cb(err); return cb(err);
const paths = fileList const paths = fileList
@@ -44,17 +58,31 @@ module.exports = (config) => {
.map(path => path.substr(0, path.length - config['article']['index'].length)) .map(path => path.substr(0, path.length - config['article']['index'].length))
.map(path => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/)) .map(path => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
.filter(path => path && path.length > 1); .filter(path => path && path.length > 1);
if (paths.length === 0)
cb(null, []);
const list = []; const list = [];
let remaining = 0;
paths.forEach(path => { paths.forEach(path => {
list.push({ const article = {
path: config['data_dir'] + path[0] + config['article']['index'], path: config['data_dir'] + path[0] + config['article']['index'],
year: parseInt(path[1]), year: parseInt(path[1]),
month: parseInt(path[2]), month: parseInt(path[2]),
day: parseInt(path[3]) day: parseInt(path[3])
}); };
}); remaining++;
readIndexFile(article.path, config['article']['thumbnail_tag'], (err, info) => {
if (err)
return cb(err);
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? (config['data_dir'] + path[0] + info.thumbnail) : config['article']['default_thumbnail'];
list.push(article);
remaining--;
if (remaining === 0)
cb(null, list); cb(null, list);
}); });
});
});
} }
}; };
}; };
+127 -14
View File
@@ -8,14 +8,17 @@ const config = {
'test': true, 'test': true,
'data_dir': dataDir, 'data_dir': dataDir,
'article': { 'article': {
'index': testIndex 'index': testIndex,
'default_title': 'Untitled',
'default_thumbnail': 'default.png',
'thumbnail_tag': 'thumbnail'
} }
}; };
const fw = require('../src/file_walker')(config); const fw = require('../src/file_walker')(config);
const deleteFolderSync = (path) => { const deleteFolderSync = (path) => {
if(!fs.existsSync(path)) if (!fs.existsSync(path))
return; return;
fs.readdirSync(path, {withFileTypes: true}).forEach((item) => { fs.readdirSync(path, {withFileTypes: true}).forEach((item) => {
if (item.isDirectory()) if (item.isDirectory())
@@ -105,10 +108,95 @@ describe('Test function fileTree', () => {
}); });
}); });
describe('Test index article reading', () => {
const file = `${dataDir}/${testIndex}`;
test('invalid file', (done) => {
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
expect(err).not.toBeNull();
expect(info).not.toBeDefined();
done();
});
});
test('correct file', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('no title', (done) => {
fs.writeFileSync(file, `
## This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
### this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).not.toBeDefined();
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('title at beginning', (done) => {
fs.writeFileSync(file, '#title');
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('title');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('no thumbnail', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('multiple thumbnails', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
![custom_thumbnail](./thumbnail2.jpg)
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
});
describe('Test article fetching', () => { describe('Test article fetching', () => {
test('invalid data dir', (done) => { test('invalid data dir', (done) => {
config['data_dir'] = 'invalid root'; config['data_dir'] = 'invalid root';
fw.fetchArticles((err,list) => { fw.fetchArticles((err, list) => {
expect(err).not.toBeNull(); expect(err).not.toBeNull();
expect(list).not.toBeDefined(); expect(list).not.toBeDefined();
config['data_dir'] = dataDir; config['data_dir'] = dataDir;
@@ -116,7 +204,7 @@ describe('Test article fetching', () => {
}); });
}); });
test('empty data dir', (done) => { test('empty data dir', (done) => {
fw.fetchArticles((err,list) => { fw.fetchArticles((err, list) => {
expect(err).toBeNull(); expect(err).toBeNull();
expect(list).toBeDefined(); expect(list).toBeDefined();
expect(list.length).toBe(0); expect(list.length).toBe(0);
@@ -133,30 +221,55 @@ describe('Test article fetching', () => {
`${dataDir}/test/test/${testIndex}`, `${dataDir}/test/test/${testIndex}`,
`${dataDir}/2019/05/${testIndex}`, `${dataDir}/2019/05/${testIndex}`,
]); ]);
fw.fetchArticles((err,list) => { fw.fetchArticles((err, list) => {
expect(err).toBeNull(); expect(err).toBeNull();
expect(list).toBeDefined(); expect(list).toBeDefined();
expect(list.length).toBe(0); expect(list.length).toBe(0);
done(); done();
}); });
}); });
test('empty index file', (done) => {
createEmptyDirs([
`${dataDir}/2019/05/05`,
]);
const file = `${dataDir}/2019/05/05/${testIndex}`;
createEmptyFiles([file]);
fw.fetchArticles((err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(1);
expect(list[0]).toEqual({
path: file,
year: 2019,
month: 5,
day: 5,
title:'Untitled',
thumbnail:'default.png'
});
done();
});
});
test('correct index file', (done) => { test('correct index file', (done) => {
createEmptyDirs([ createEmptyDirs([
`${dataDir}/2019/05/05`, `${dataDir}/2019/05/05`,
]); ]);
const fileList = [ const file = `${dataDir}/2019/05/05/${testIndex}`;
`${dataDir}/2019/05/05/${testIndex}`, fs.writeFileSync(file, `
]; # Title
createEmptyFiles(fileList); ![thumbnail](./thumbnail.jpg)
fw.fetchArticles((err,list) => { this is some text
`);
fw.fetchArticles((err, list) => {
expect(err).toBeNull(); expect(err).toBeNull();
expect(list).toBeDefined(); expect(list).toBeDefined();
expect(list.length).toBe(1); expect(list.length).toBe(1);
expect(list[0]).toEqual({ expect(list[0]).toEqual({
path: fileList[0], path: file,
year:2019, year: 2019,
month:5, month: 5,
day:5 day: 5,
title:'Title',
thumbnail:`${dataDir}/2019/05/05/./thumbnail.jpg`
}); });
done(); done();
}); });