forked from FrontendMasters/grid-flexbox-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.js
93 lines (74 loc) · 2.76 KB
/
csv.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
90
91
92
93
const fs = require("fs").promises;
const path = require("path");
const fm = require("front-matter");
const isUrl = require("is-url-superb");
const parseLinks = require("parse-markdown-links");
const { sorter } = require("./src/util/helpers");
const mdDir = process.env.MARKDOWN_DIR || path.join(__dirname, "lessons/");
const outputPath =
process.env.OUTPUT_CSV_PATH || path.join(__dirname, "public/lessons.csv");
const linksOutputPath =
process.env.LINKS_CSV_PATH || path.join(__dirname, "public/links.csv");
async function createCsv() {
console.log(`making the markdown files into a CSV from ${mdDir}`);
// get paths
const allFiles = await fs.readdir(mdDir);
const files = allFiles.filter(filePath => filePath.endsWith(".md"));
// read paths, get buffers
const buffers = await Promise.all(
files.map(filePath => fs.readFile(path.join(mdDir, filePath)))
);
// make buffers strings
const contents = buffers.map(content => content.toString());
// make strings objects
let frontmatters = contents.map(fm);
// find all attribute keys
const seenAttributes = new Set();
frontmatters.forEach(item => {
Object.keys(item.attributes).forEach(attr => seenAttributes.add(attr));
});
const attributes = Array.from(seenAttributes.values());
if (attributes.includes("order")) {
frontmatters = frontmatters.sort(sorter);
}
// get all data into an array
let rows = frontmatters.map(item => {
const row = attributes.map(attr =>
item.attributes[attr] ? JSON.stringify(item.attributes[attr]) : ""
);
return row;
});
// header row must be first row
rows.unshift(attributes);
// join into CSV string
const csv = rows.map(row => row.join(",")).join("\n");
// write file out
await fs.writeFile(outputPath, csv);
console.log(`Wrote ${rows.length} rows to ${outputPath}`);
// make links csv
let longestLength = 0;
let linksArray = frontmatters.map(row => {
const links = parseLinks(row.body).filter(isUrl);
longestLength = longestLength > links.length ? longestLength : links.length;
const newRow = [row.attributes.order, row.attributes.title, ...links];
return newRow;
});
if (longestLength) {
// add title row
linksArray = linksArray.map(array => {
const lengthToFill = longestLength + 2 - array.length;
return array.concat(Array.from({ length: lengthToFill }).fill(""));
});
linksArray.unshift(
["order", "title"].concat(
Array.from({ length: longestLength }).map((_, index) => `link${index}`)
)
);
// join into CSV string
const linksCsv = linksArray.map(row => row.join(",")).join("\n");
// write file out
await fs.writeFile(linksOutputPath, linksCsv);
console.log(`Wrote ${linksArray.length} rows to ${linksOutputPath}`);
}
}
createCsv();