-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
258 lines (230 loc) · 8.01 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* eslint-disable @typescript-eslint/no-var-requires */
import fs from "fs";
import { join, relative, dirname, basename, extname, sep } from "path";
import frontMatter from "front-matter";
import globby from "globby";
import startCase from "lodash.startcase";
const transformLinks = require("transform-markdown-links");
const transform = require("doctoc/lib/transform");
const { readFile } = fs.promises;
export const gitHubLink = (val: string): string =>
val
.trim()
.toLowerCase()
.replace(/[^\w\- ]+/g, "")
.replace(/\s/g, "-")
.replace(/-+$/, "");
/** @ignore */
interface File {
path: string;
[key: string]: any;
}
/**
* Concat function options.
*/
export interface ConcatOptions {
/**
* Whether to add a table of contents.
*/
toc?: boolean;
/**
* Limit TOC entries to headings only up to the specified level.
*/
tocLevel?: number;
/**
* Glob patterns to exclude in `dir`.
*/
ignore?: string | string[];
/**
* Whether to decrease levels of all titles in markdown file to set them below file and directory title levels.
*/
decreaseTitleLevels?: boolean;
/**
* Level to start file and directory levels.
*/
startTitleLevelAt?: number;
/**
* String to be used to join concatenated files.
*/
joinString?: string;
/**
* Key name to get title in `FrontMatter` meta data in markdown headers.
*/
titleKey?: string;
/**
* Whether to use file names as titles.
*/
fileNameAsTitle?: boolean;
/**
* Whether to use directory names as titles.
*/
dirNameAsTitle?: boolean;
}
/**
* Makes given input array and returns it.
*
* @param input to construct array from
* @returns created array.
* @ignore
*/
function arrify<T>(input: T | T[]): T[] {
return Array.isArray(input) ? input : [input];
}
/** @ignore */
class MarkDownConcatenator {
private dir: string;
private toc: boolean;
private ignore: string | string[];
private decreaseTitleLevels: boolean;
private startTitleLevelAt: number;
private titleKey?: string;
private fileNameAsTitle: boolean;
private dirNameAsTitle: boolean;
private joinString: string;
private visitedDirs: Set<string> = new Set();
private fileTitleIndex: Map<string, { title: string; level: number; md: string }> = new Map();
private tocLevel: number;
private files: File[] = [];
public constructor(
dir: string,
{
toc = false,
tocLevel = 3,
ignore = [],
decreaseTitleLevels = false,
startTitleLevelAt = 1,
joinString = "\n",
titleKey,
dirNameAsTitle = false,
fileNameAsTitle = false,
}: ConcatOptions = {} as any
) {
this.dir = dir;
this.toc = toc;
this.tocLevel = tocLevel;
this.ignore = ignore;
this.decreaseTitleLevels = decreaseTitleLevels;
this.startTitleLevelAt = startTitleLevelAt;
this.joinString = joinString;
this.titleKey = titleKey;
this.dirNameAsTitle = dirNameAsTitle;
this.fileNameAsTitle = fileNameAsTitle;
}
private decreaseTitleLevelsBy(body: string, level: number): string {
return !this.decreaseTitleLevels || level <= 0 ? body : body.replace(/(^#+)/gm, `$1${"#".repeat(level)}`);
}
private async getFileNames(): Promise<string[]> {
const paths = await globby([`**/*.md`], { cwd: this.dir, ignore: arrify(this.ignore) });
return paths.map(path => join(this.dir, path));
}
private async getFileDetails(): Promise<File[]> {
const fileNames = await this.getFileNames();
return Promise.all(
fileNames.map(async fileName => ({ path: fileName, ...frontMatter(await readFile(fileName, { encoding: "utf8" })) }))
);
}
private getDirParts(file: File): string[] {
return this.dir === dirname(file.path) ? [] : relative(this.dir, dirname(file.path)).split(sep);
}
private addTitle(file: File): void {
let titleMd = "";
let fileTitle;
const titleSuffix = "\n\n";
let level = this.startTitleLevelAt - 1;
if (this.dirNameAsTitle) {
let currentDir = "";
const dirParts = this.getDirParts(file);
dirParts.forEach(part => {
currentDir += currentDir ? sep + part : part;
level += 1;
if (!this.visitedDirs.has(currentDir)) {
const dirTitlePrefix = "#".repeat(level); // #, ##, ### ...etc.
const dirTitle = startCase(part);
titleMd += `${dirTitlePrefix} ${dirTitle}${titleSuffix}`;
this.visitedDirs.add(currentDir);
this.fileTitleIndex.set(join(this.dir, currentDir), { title: dirTitle, md: titleMd, level });
}
});
}
const titleFromMeta: string = this.titleKey && file.attributes && file.attributes[this.titleKey];
const titleFromFileName = `${startCase(basename(file.path, extname(file.path)))}`;
if (titleFromMeta || this.fileNameAsTitle) {
fileTitle = titleFromMeta || titleFromFileName;
level += 1;
const titlePrefix = "#".repeat(level); // #, ##, ### ...etc.
titleMd += `${titlePrefix} ${fileTitle}${titleSuffix}`;
} else {
fileTitle = gitHubLink(relative(this.dir, file.path));
titleMd += `\n<a name="${fileTitle}"></a>\n\n`; // Provide an anchor to point links to this location. (For existing links pointing to file.)
}
this.fileTitleIndex.set(file.path, { title: fileTitle, md: titleMd, level });
}
private getTitle(filePath: string): { title: string; level: number; md: string } {
const title = this.fileTitleIndex.get(filePath);
/* istanbul ignore next */
if (!title) {
throw new Error(`Cannot get title for ${filePath}`);
}
return title;
}
private addToc(content: string): string {
if (!this.toc) {
return content;
}
const TOC_TAG = "<!-- START doctoc -->\n<!-- END doctoc -->";
let result = content;
if (!result.includes(TOC_TAG)) {
result = `${TOC_TAG}\n\n${result}`;
}
const docTocResult = transform(result, "github.com", this.tocLevel, undefined, true);
if (docTocResult.transformed) {
result = docTocResult.data;
}
return result;
}
private modifyLinks(file: File): string {
return transformLinks(file.body, (link: string): string => {
if (link.startsWith("http")) {
return link;
}
// [ModifyCondition](../interfaces/modifycondition.md) - Link to file.
// [FileFormat](../README.md#fileformat) - Section in relative file.
// [saveSync](datafile.md#savesync) - Link in same file
// <a name="there_you_go"></a>Take me there
const absoluteTargetPath = join(dirname(file.path), link);
const hashPosition = absoluteTargetPath.indexOf("#");
const hash = hashPosition > -1 ? absoluteTargetPath.slice(hashPosition) : "";
const targetFile = hashPosition > -1 ? absoluteTargetPath.slice(0, hashPosition) : absoluteTargetPath;
const newLink = hash || `#${gitHubLink(this.getTitle(targetFile).title)}`;
return newLink;
// console.log(link, newLink, targetFile, hash);
});
}
public async concat(): Promise<string> {
const files = await this.getFileDetails();
files.forEach(file => {
this.addTitle(file);
const { level, md } = this.getTitle(file.path);
const body = this.decreaseTitleLevelsBy(file.body, level);
file.body = `${md}${body}`; // eslint-disable-line no-param-reassign
});
// 2nd pass loop is necessary, because all titles has to be processed.
files.forEach(file => {
file.body = this.modifyLinks(file); // eslint-disable-line no-param-reassign
});
let result = files.map(file => file.body).join(this.joinString);
result = this.addToc(result);
return result;
}
}
/**
* Scans and concatenates all markdown files in given directory.
*
* @param dir is the directory to scan markdown files in.
* @param options are several parameters to modify concatenation behaviour.
* @returns concatenated contents of markdown files.
*/
export default async function concatMd(dir: string, options?: ConcatOptions): Promise<string> {
const markDownConcatenator = new MarkDownConcatenator(dir, options);
return markDownConcatenator.concat();
}