-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-to-pdf.js
45 lines (38 loc) · 1 KB
/
challenges-to-pdf.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
import fs from 'fs-extra'
import path from 'path'
import markdownpdf from 'markdown-pdf'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const dirPath = path.join(__dirname, './src/lib/challenges')
const outputPath = 'merged.pdf'
function naturalSort(a, b) {
return a.localeCompare(b, undefined, {
numeric: true,
sensitivity: 'base',
})
}
async function mergeMarkdownToPDF() {
try {
const files = (await fs.readdir(dirPath)).sort(naturalSort)
let markdownContent = ''
for (const file of files) {
if (path.extname(file) === '.svx') {
const content = await fs.readFile(
path.join(dirPath, file),
'utf-8',
)
markdownContent += `# ${file}\n\n${content}\n\n`
}
}
markdownpdf()
.from.string(markdownContent)
.to(outputPath, () => {
console.log(`Merged PDF created at ${outputPath}`)
})
} catch (err) {
console.error('Error:', err)
}
}
mergeMarkdownToPDF()