up generate.js
condense generating the cover letter
This commit is contained in:
parent
9cb5bbf0bb
commit
656e0bde02
1 changed files with 18 additions and 50 deletions
|
@ -84,58 +84,26 @@ router.post('/download', async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
function generateCoverLetter(rawText, outputFilename) {
|
function generateCoverLetter(rawText, outputFilename) {
|
||||||
const header = rawText.match(/<header>(.*?)<\/header>/s)[1].trim();
|
// Extract all sections in one go using an object
|
||||||
const greeting = rawText.match(/<greeting>(.*?)<\/greeting>/s)[1].trim();
|
const sections = ['header', 'greeting', 'introduction', 'body', 'conclusion', 'signature']
|
||||||
const introduction = rawText.match(/<introduction>(.*?)<\/introduction>/s)[1].trim();
|
.reduce((acc, section) => ({
|
||||||
const body = rawText.match(/<body>(.*?)<\/body>/s)[1].trim();
|
...acc,
|
||||||
const conclusion = rawText.match(/<conclusion>(.*?)<\/conclusion>/s)[1].trim();
|
[section]: rawText.match(new RegExp(`<${section}>(.*?)<\/${section}>`, 's'))[1].trim()
|
||||||
const signature = rawText.match(/<signature>(.*?)<\/signature>/s)[1].trim();
|
}), {});
|
||||||
|
|
||||||
// Create a new document using docx
|
// Create document with all sections
|
||||||
const doc = new Document({
|
const doc = new Document({
|
||||||
sections: [
|
sections: [{
|
||||||
{
|
|
||||||
properties: {},
|
properties: {},
|
||||||
children: [
|
children: Object.values(sections).map(text =>
|
||||||
new Paragraph({
|
new Paragraph({
|
||||||
children: [
|
children: [
|
||||||
new TextRun(header),
|
new TextRun(text),
|
||||||
new TextRun("\n\n"), // Add line breaks between sections
|
new TextRun("\n\n")
|
||||||
],
|
]
|
||||||
}),
|
})
|
||||||
new Paragraph({
|
)
|
||||||
children: [
|
}]
|
||||||
new TextRun(greeting),
|
|
||||||
new TextRun("\n\n"),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
new Paragraph({
|
|
||||||
children: [
|
|
||||||
new TextRun(introduction),
|
|
||||||
new TextRun("\n\n"),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
new Paragraph({
|
|
||||||
children: [
|
|
||||||
new TextRun(body),
|
|
||||||
new TextRun("\n\n"),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
new Paragraph({
|
|
||||||
children: [
|
|
||||||
new TextRun(conclusion),
|
|
||||||
new TextRun("\n\n"),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
new Paragraph({
|
|
||||||
children: [
|
|
||||||
new TextRun(signature),
|
|
||||||
new TextRun("\n\n"),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Packer.toBuffer(doc);
|
return Packer.toBuffer(doc);
|
||||||
|
|
Loading…
Reference in a new issue