Repository navigation
Expand file tree
/
Copy pathtoDocx.js
More file actions
149 lines (134 loc) · 2.88 KB
/
Copy pathtoDocx.js
File metadata and controls
149 lines (134 loc) · 2.88 KB
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
/**
* /toLatex.js
*
* Author: Ben Siebert <hello@ben-siebert.de>
* Copyright: Copyright (c) 2018-2024 Ben Siebert. All rights reserved.
* License: Project License
* Created At: 12.06.2024
*
*/
const fs = require("fs");
const path = require("path");
const byteSize = require("byte-size");
const docx = require("docx");
const ignore = [
".turbo",
".git",
".idea",
".ico",
".jpg",
".jpeg",
".png",
".docusaurus",
".ttf",
".zip",
".afdesign",
"LICENSE",
".lock",
"package-lock.json",
".log",
"node_modules",
".next",
"dist",
"build",
".codeup_app_stats.json",
".react-email",
".env",
".env.web",
"pdf",
"docs",
"svg",
"codeup.tex",
".woff2",
"toMarkdown.js",
];
let paths = getAllPaths(".", []);
paths = paths.filter((p) => {
let allowed = true;
ignore.forEach((d) => {
if (p.toLowerCase().includes(d.toLowerCase())) {
allowed = false;
}
});
return allowed;
});
const sections = [
{
children: [
new docx.Paragraph({
children: [
new docx.TextRun({
text: "Inhaltsverzeichnis",
font: "Arial",
}),
],
heading: docx.HeadingLevel.TITLE,
}),
new docx.TableOfContents("Inhaltsverzeichnis", {
hyperlink: true,
}),
],
},
];
for (let file of paths) {
file = file.replace(path.join(__dirname, "/"), "");
file = file.replace(/\\/g, "/");
console.log(`Reading ${file}`);
let content = fs.readFileSync(file, "utf8");
/*content = removeEmojis(content);
content = content.replace("[", "\\verb|[|");
content = content.replace("]", "\\verb|]|");*/
let trLines = content.split("\n").map((line) => {
return new docx.Paragraph({
children: [
new docx.TextRun({
text: line,
font: "Arial",
}),
],
});
});
const section = {
properties: {
type: docx.SectionType.CONTINUOUS,
},
children: [
new docx.Paragraph({
children: [
new docx.TextRun({
text: file,
font: "Arial",
}),
],
heading: docx.HeadingLevel.HEADING_2,
}),
...trLines,
],
};
sections.push(section);
}
const doc = new docx.Document({
features: {
updateFields: true,
},
creator: "Ben Siebert",
description: "CodeUp Quellcode",
title: "CodeUp Quellcode",
sections,
});
docx.Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync(path.join(__dirname, "codeup.docx"), buffer);
});
console.log("Done!");
function getAllPaths(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllPaths(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file));
}
});
return arrayOfFiles;
}