forked from wdzajicek/rss-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (81 loc) · 2.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const fs = require('fs');
const colors = require('colors'); // Colored console output
const { cleanContent } = require('./helpers/cleanContent'); // transforms strings to desired format
let Parser = require('rss-parser');
let parser = new Parser({
timeout: 100000
});
// Configure this object to set the source file and destination path
const config = {
input: {
source: 'update-articles_2017-2022.xml',
},
output: {
path: './dist/update',
}
}
// Setup a theme for colors (colored console output)
colors.setTheme({
info: 'brightCyan',
warn: 'yellow',
success: 'green',
debug: 'cyan',
error: 'red'
});
function createFiles(path, postname, content) {
fs.writeFile(`${path}/${postname}.md`, content, (err) => {
if (err) throw err;
console.log(`${colors.info('[WROTE FILE]')}: ${path}/${postname}.md`);
});
}
function createFolders(path, postname, content) {
fs.mkdir(path, { recursive: true }, (err) => {
if (err) throw err;
createFiles(path, postname, content)
});
}
function processFeed(feed) {
feed.items.forEach((item) => {
const { title, link, pubDate, author, content, guid, isoDate } = item; // unpack the parts of each feed item
const cleanTitle = title.replace(/(")/g, `\\$1`).trim(); // Escape any quotes in the title
const clean = cleanContent(content); // Modifies <img> elements in the feed
// for creating a proper Jekyll post name: `YEAR-MONTH-DAY-TITLE.md`
const d = new Date(pubDate);
const month = ((d.getMonth() + 1) <= 9) ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
const day = (d.getDate() <= 9) ? `0${d.getDate()}` : d.getDate();
const year = d.getFullYear();
// construct our file content
const fileArray = [
'---', // YAML front-matter start
`\ntitle: "${cleanTitle}"`,
`\nlink: ${link}`,
`\nauthor: ${author}`,
`\npublish_date: ${pubDate}`,
`\nguid: ${guid}`,
`\nisoDate: ${isoDate}`,
`\n---`, // YAML front-matter end
`\n`,
`\n${clean}`
];
const fileContent = fileArray.join('');
const file = title.replace(/[^a-zA-z0-9\s]/g, '');
const filename = file.trim().replace(/\s/g, '-').toLowerCase();
const postname = `${year}-${month}-${day}-${filename.replace(/--/g, '-')}`;
createFolders(config.output.path, postname, fileContent);
});
}
function parseFeed(data) {
parser.parseString(data, (err, feed) => {
if (err) throw err;
console.log(`${colors.info('[PARSED FEED]')}: ${feed.title}`);
console.log(`${colors.info('[PROCESSING FEED ITEMS]')}...`);
processFeed(feed);
});
}
function rssToMarkdown(source) {
fs.readFile(`./src/${source}`, 'utf8', (err, data) => {
if (err) throw err;
parseFeed(data);
});
}
rssToMarkdown(config.input.source);