-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.ts
116 lines (97 loc) · 4.06 KB
/
action.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
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { marked } from 'marked'
import { glob } from 'glob'
import { EPub } from '@lesjoursfr/html-to-epub'
import { type IChapter } from './interfaces/IChapter.ts'
// GitHub workspace directory.
const gitHubWorkspaceDir: string = process.env.GITHUB_WORKSPACE || '/github/workspace'
// Inputs.
const markdownFiles: string = process.env.INPUT_MARKDOWNFILES // Required parameter.
const title: string = process.env.INPUT_TITLE // Required parameter.
const author: string = process.env.INPUT_AUTHOR // Required parameter.
const publisher: string = process.env.INPUT_PUBLISHER || undefined
const version: number = parseInt(process.env.INPUT_VERSION) || 3
const lang: string = process.env.INPUT_LANG || 'en'
const tocTitle: string = process.env.INPUT_TOCTITLE || undefined
const hideToC: boolean = process.env.INPUT_HIDETOC === 'true'
const output: string = process.env.INPUT_OUTPUT || 'book.epub'
let cover: string = process.env.INPUT_COVER
if (!markdownFiles) {
console.error('Missing required input: \'markdownFiles\'')
process.exit(1)
}
if (!title) {
console.error('Missing required input: \'title\'')
process.exit(1)
}
if (!author) {
console.error('Missing required input: \'author\'')
process.exit(1)
}
// Check if the cover is a URL or a file path.
if (typeof cover === 'string' && !cover.trim().startsWith('http')) {
cover = `${gitHubWorkspaceDir}/${process.env.INPUT_COVER}`
}
const includes: Array<string> = markdownFiles?.split('\n') || []
const chapters: Array<IChapter> = []
for (const includeIndex in includes) {
const regex: string = includes[includeIndex]
const markdownFileNames: Array<string> = await glob(`${gitHubWorkspaceDir}/**/${regex.trim()}`, { ignore: 'node_modules/**' })
// Sort the markdown files by name.
if (markdownFileNames.length > 0) {
markdownFileNames.sort()
}
for (const fileIndex in markdownFileNames) {
const markdownFileName: string = markdownFileNames[fileIndex]
// Read the markdown file to get the content of the file.
const markdown: string = fs.readFileSync(path.resolve(import.meta.dirname, markdownFileName)).toString().trim()
// Extract chapter title from markdown metadata.
const chapterTitleMatch: Array<string> = markdown.match(/\[metadata:title\]:- "([^"]+)"/i)
const chapterTitle: string | undefined = chapterTitleMatch ? chapterTitleMatch[1].trim() : undefined
// Extract chapter author from markdown metadata.
const chapterAuthorMatch: Array<string> = markdown.match(/\[metadata:author\]:- "([^"]+)"/i)
const chapterAuthor: string | undefined = chapterAuthorMatch ? chapterAuthorMatch[1].trim() : undefined
// Extract chapter excludeFromToc from markdown metadata.
const chapterExcludeFromTocMatch: Array<string> = markdown.match(/\[metadata:excludeFromToc\]:- "([^"]+)"/i)
const chapterExcludeFromToc: boolean | undefined = chapterExcludeFromTocMatch
? chapterExcludeFromTocMatch[1].trim() === 'true'
: undefined
// Extract chapter excludeFromToc from markdown metadata.
const chapterBeforeTocMatch: Array<string> = markdown.match(/\[metadata:beforeToc\]:- "([^"]+)"/i)
const chapterBeforeToc: boolean | undefined = chapterBeforeTocMatch
? chapterBeforeTocMatch[1].trim() === 'true'
: undefined
// Generate the HTML content from markdown.
const html: string = marked.parse(markdown)
// Concatenate the chapter to the chapters list.
chapters.push({
title: chapterTitle,
author: chapterAuthor,
data: html,
excludeFromToc: chapterExcludeFromToc,
beforeToc: chapterBeforeToc,
})
console.log('Generated chapter from markdown file:', markdownFileName)
}
}
const option = {
title,
author,
publisher,
cover,
version,
lang,
tocTitle,
hideToC,
verbose: true,
content: chapters,
}
try {
const epub = new EPub(option, `${gitHubWorkspaceDir}/${output}`);
await epub.render()
console.log('Ebook Generated Successfully! Output:', output)
} catch (error) {
console.error('Failed to generate Ebook because of:', error);
}