From 518a6ab9646af0848450f2cfbd89567b3abfc6fb Mon Sep 17 00:00:00 2001 From: Harry Maynard Date: Sat, 17 Aug 2024 22:25:11 -0700 Subject: [PATCH] Generating EPub file from markdown! --- .github/workflows/ci.yml | 1 - action.js | 66 +++++++++++++++++++++++++--------------- action.yml | 4 +++ test-data/chapter-01.md | 4 +-- test-data/chapter-02.md | 3 ++ 5 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 test-data/chapter-02.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45f543d..accc0e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,6 @@ jobs: uses: harrymaynard/markdowntoepub-action@main with: markdownFiles: |- - README.md test-data/*.md title: My Book author: Harry Maynard diff --git a/action.js b/action.js index ea85859..72b8205 100644 --- a/action.js +++ b/action.js @@ -16,29 +16,62 @@ const version = parseInt(process.env.INPUT_VERSION) || 3 const lang = process.env.INPUT_LANG || 'en' const tocTitle = process.env.INPUT_TOCTITLE || undefined const hideToC = process.env.INPUT_HIDETOC === 'true' +const output = process.env.INPUT_OUTPUT || 'book.epub' if (!markdownFiles) { console.error('Missing required input: \'markdownFiles\'') process.exit(1) } -const includes = markdownFiles?.split('\\n') || [] -let allMarkdown = '' +if (!title) { + console.error('Missing required input: \'title\'') + process.exit(1) +} + +if (!author) { + console.error('Missing required input: \'author\'') + process.exit(1) +} -console.log('markdownFiles:', markdownFiles) +const includes = markdownFiles?.split('\\n') || [] +const chapters = [] for (const includeIndex in includes) { const regex = includes[includeIndex] const markdownFileNames = await glob(regex.trim(), { ignore: 'node_modules/**' }) - console.log('markdownFileNames:', markdownFileNames) + + // Sort the markdown files by name. + if (markdownFileNames.length > 0) { + markdownFileNames.sort() + } + for (const fileIndex in markdownFileNames) { const markdownFileName = markdownFileNames[fileIndex] + + // Read the markdown file to get the content of the file. const markdown = fs.readFileSync(path.resolve(import.meta.dirname, markdownFileName)).toString().trim() - allMarkdown += `${markdown}\n\n` + + // Extract chapter title from markdown metadata. + const chapterTitleMatch = markdown.match(/\[metadata:title\]:- "([^"]+)"/i) + const chapterTitle = chapterTitleMatch ? chapterTitleMatch[1] : undefined + + // Extract chapter author from markdown metadata. + const chapterAuthorMatch = markdown.match(/\[metadata:author\]:- "([^"]+)"/i) + const chapterAuthor = chapterAuthorMatch ? chapterAuthorMatch[1] : undefined + + // Generate the HTML content from markdown. + const html = marked.parse(markdown) + + // Concatenate the chapter to the chapters list. + chapters.push({ + title: chapterTitle, + author: chapterAuthor, + data: html, + }) + console.log('Generated chapter from markdown file:', markdownFileName) } } - const option = { title, author, @@ -49,28 +82,13 @@ const option = { tocTitle, hideToC, verbose: true, - content: [ - { - title: "About the author", // Optional - author: "John Doe", // Optional - data: "

Charles Lutwidge Dodgson

" - +"
Better known by the pen name Lewis Carroll...
" // pass html string - }, - { - title: "Down the Rabbit Hole", - data: "

Alice was beginning to get very tired...

" - }, - ] + content: chapters, } try { - const epub = new EPub(option, 'book.epub'); + const epub = new EPub(option, output); await epub.render() - console.log('Ebook Generated Successfully!') + console.log('Ebook Generated Successfully! Output:', output) } catch (error) { console.error('Failed to generate Ebook because of:', error); } -const html = marked.parse(allMarkdown) -console.log('html:', html, '\n') - - diff --git a/action.yml b/action.yml index bb43195..9b4b571 100644 --- a/action.yml +++ b/action.yml @@ -38,6 +38,10 @@ inputs: description: 'Hide the table of contents. Default is false.' required: false default: 'false' + output: + description: 'Name of the output EPub file' + required: false + default: 'book.epub' runs: using: 'docker' diff --git a/test-data/chapter-01.md b/test-data/chapter-01.md index c5edebb..ee0e70e 100644 --- a/test-data/chapter-01.md +++ b/test-data/chapter-01.md @@ -1,6 +1,4 @@ -[metadata:chapter]:- "Chapter 1" +[metadata:title]:- "Chapter 1" [metadata:author]:- "Harry Maynard" -## Chapter 1 - Lorem ipsum dolor sit amet, consectetur adipiscing elit. diff --git a/test-data/chapter-02.md b/test-data/chapter-02.md new file mode 100644 index 0000000..63f0902 --- /dev/null +++ b/test-data/chapter-02.md @@ -0,0 +1,3 @@ +[metadata:title]:- "Chapter 2" + +Lorem ipsum dolor sit amet, consectetur adipiscing elit.