-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItsABlog.ts
186 lines (160 loc) · 5.75 KB
/
ItsABlog.ts
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as fs from 'fs';
import objectPath from 'object-path';
import marked from 'marked';
import typeset from 'typeset';
import { ItsABlogOptions, ItsABlogFileManifest } from './types/ItsABlog';
/**
* ItsABlog
*/
export default class ItsABlog {
/**
* Private Members
*/
private options: ItsABlogOptions;
private fileNames: string[];
private fileManifest: ItsABlogFileManifest;
constructor(options?: Record<string, unknown>) {
const defaultOptions: ItsABlogOptions = {
metaTagStart: '<meta>',
metaTagEnd: '</meta>',
dir: 'blog',
encoding: 'utf8',
pretty: true,
output: 'blog.json'
};
const compiledOptions: ItsABlogOptions = {} as ItsABlogOptions;
Object.keys(defaultOptions).forEach((key) => {
compiledOptions[key] = objectPath.has(options, key) ? options[key] :
defaultOptions[key];
});
this.options = compiledOptions;
}
/**
* Runner Methods
*/
getPosts(): ItsABlogFileManifest {
this.configureFileManifest();
return this.fileManifest;
}
/**
* Write fileManifest to given output file
*/
outputToFile(): void {
this.configureFileManifest();
this.writeToFile();
console.log('Output to file: "' + this.options.output + '"');
}
/**
* Builder Methods
*/
/**
* Sets up the file manifest, if it hasn't been done already
*/
configureFileManifest(): void {
if(typeof this.fileManifest === 'undefined') {
this.getNamesOfFilesFromDir();
this.initiateFileManifest();
this.initializeMetaData();
this.configureCustomMetaData();
this.removeMetaDataString();
this.compileContent();
this.options.pretty && this.prettifyFileManifest();
}
}
/**
* Sets the fileNames member equal to all file names in the given dir
*/
getNamesOfFilesFromDir(): void {
this.fileNames = fs.readdirSync(this.options.dir);
}
/**
* Sets the fileManifest to have keys based on the file names from the directory,
* and their content
*/
initiateFileManifest(): void {
if(!objectPath.get(this, 'fileNames.length')) {
throw 'no files found in given dir';
}
this.fileManifest = {};
this.fileNames.forEach((fileName) => {
this.fileManifest[fileName] = {
content: fs.readFileSync(this.options.dir + '/' + fileName, {
encoding: this.options.encoding
})
};
});
delete this.fileNames;
}
/**
* Sets up metaData for each item in fileManifest
*/
initializeMetaData(): void {
Object.keys(this.fileManifest).forEach((key) => {
this.fileManifest[key].meta = {
creationDate: fs.statSync(this.options.dir + '/' + key)
.birthtime,
lastEdited: fs.statSync(this.options.dir + '/' + key)
.mtime
};
});
}
/**
* Adds meta data set within the blog post to the meta data for the fileManifest
*/
configureCustomMetaData(): void {
Object.keys(this.fileManifest).forEach((key) => {
const containsCustomMetaData = this.fileManifest[key].content.indexOf(this.options.metaTagStart) > -1;
let customMetaDataString, customMetaData;
if(containsCustomMetaData) {
customMetaDataString = this.fileManifest[key].content.substring(this.fileManifest[key]
.content.indexOf(this.options.metaTagStart) +
this.options.metaTagStart.length, this.fileManifest[key]
.content.indexOf(this.options.metaTagEnd));
customMetaData = JSON.parse(customMetaDataString);
Object.assign(this.fileManifest[key].meta, customMetaData);
}
});
}
/**
* Removes text that contains meta data from the outputted content of the item in the fileManifest
*/
removeMetaDataString(): void {
Object.keys(this.fileManifest).forEach((key) => {
const customMetaDataString = this.fileManifest[key].content.substring(this.fileManifest[key]
.content.indexOf(this.options.metaTagStart), this.fileManifest[key]
.content.indexOf(this.options.metaTagEnd) + this.options.metaTagEnd.length);
this.fileManifest[key].content = this.fileManifest[key].content.substring(
this.fileManifest[key].content.indexOf(customMetaDataString) + customMetaDataString.length,
this.fileManifest[key].content.length
);
});
}
/**
* Changes content of each item in fileManifest to be html compatable, and
* char changed to typeset
*/
compileContent(): void {
Object.keys(this.fileManifest).forEach((key) => {
this.fileManifest[key].content =
typeset(marked(this.fileManifest[key].content));
});
}
/**
* Changes fileManifest keys to be the file name without the extension, by making
* them substrings up to the first '.'
*/
prettifyFileManifest(): void {
Object.keys(this.fileManifest).forEach((key) => {
const prettyName = key.substr(0, key.indexOf('.'));
this.fileManifest[prettyName] = this.fileManifest[key];
delete this.fileManifest[key];
});
}
/**
* Writes the fileManifest to the file set in the options
*/
writeToFile(): void {
fs.writeFileSync(this.options.output, JSON.stringify(this.fileManifest, null, this.options.pretty ?
'\t' : null));
}
}