forked from maxhoffmann/yaml-markdown-to-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (96 loc) · 3.39 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* eslint no-console: 0 */
"use strict";
const path = require("path");
const fs = require("fs-extra");
const chalk = require("chalk");
const { cloneDeep } = require("lodash");
const REGEX_NO_FOLDER = /^[^\/]+(\/index)?$/;
async function redaktor(cliParams) {
const fileContents = await Promise.all(
(cliParams.files || []).map(getFileContents(cliParams.dataFolder))
);
const withoutSkippedFiles = fileContents.filter(Boolean);
const renderedFiles = await Promise.all(
withoutSkippedFiles.map(
renderEachFile(cliParams.htmlFolder, cliParams.cmsFolder)
)
);
console.log(chalk.green(`✅ rendered ${renderedFiles.length} files`));
}
function getFileContents(dataFolder) {
return async (filePath) => {
try {
const extension = path.extname(filePath);
const relativePath = path
.relative(dataFolder, filePath)
.replace(new RegExp(extension + "$"), "");
console.log(chalk.blue("👓 reading " + filePath));
const data = await fs.readJSON(filePath);
return { data, path: relativePath };
} catch (error) {
console.error(`⏩ skipped ${filePath}: ${error}`);
return false;
}
};
}
function renderEachFile(htmlFolder, cmsFolder) {
return async (file, _, allFiles) => {
const currentFolder = path.join(file.path, "..");
const folderPattern =
currentFolder === "."
? REGEX_NO_FOLDER
: new RegExp("^" + currentFolder + "/[^/]+(/index)?$");
const filesInCurrentFolder = allFiles.filter(function (testedFile) {
return (
folderPattern.test(testedFile.path) && testedFile.path !== file.path
);
});
const destinationPath = path.join(htmlFolder, file.path + ".html");
const clonedFile = cloneDeep(file);
console.log(chalk.cyan("⚙️ rendering " + file.path));
let documentFunction = () => Promise.resolve(`error loading page document`);
try {
documentFunction = require(path.resolve(
path.join(cmsFolder, "page-documents"),
file.data.default.required.documentType || "default"
));
} catch (error) {}
try {
documentFunction = require(path.resolve(
path.join(cmsFolder, "system", "page-documents"),
file.data.default.required.documentType || "default"
));
} catch (error) {
throw new Error(`error loading page document: ${error}`);
}
const pageType = file.data.default.required.pageType;
let viewFunction = () => Promise.resolve(`error rendering ${pageType}`);
try {
viewFunction = require(path.resolve(
path.join(cmsFolder, "page-views"),
file.data.default.required.pageType || "default"
));
} catch (error) {
try {
viewFunction = require(path.resolve(
path.join(cmsFolder, "system", "page-views"),
file.data.default.required.pageType || "default"
));
} catch (error) {
viewFunction = () =>
Promise.resolve(`error rendering ${pageType}: ${error}`);
}
}
const renderedView = await viewFunction(
clonedFile,
cloneDeep(filesInCurrentFolder),
cloneDeep(allFiles)
);
const renderedHtml = await documentFunction(clonedFile, renderedView);
console.log(chalk.yellow("🖨 writing " + clonedFile.path));
await fs.outputFile(destinationPath, renderedHtml);
clonedFile.renderedPath = destinationPath;
return clonedFile;
};
}
module.exports = redaktor;