Skip to content

Commit

Permalink
Generating EPub file from markdown!
Browse files Browse the repository at this point in the history
  • Loading branch information
harrymaynard committed Aug 18, 2024
1 parent c0bcf68 commit 518a6ab
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
uses: harrymaynard/markdowntoepub-action@main
with:
markdownFiles: |-
README.md
test-data/*.md
title: My Book
author: Harry Maynard
Expand Down
66 changes: 42 additions & 24 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,28 +82,13 @@ const option = {
tocTitle,
hideToC,
verbose: true,
content: [
{
title: "About the author", // Optional
author: "John Doe", // Optional
data: "<h2>Charles Lutwidge Dodgson</h2>"
+"<div lang=\"en\">Better known by the pen name Lewis Carroll...</div>" // pass html string
},
{
title: "Down the Rabbit Hole",
data: "<p>Alice was beginning to get very tired...</p>"
},
]
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')


4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 1 addition & 3 deletions test-data/chapter-01.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions test-data/chapter-02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[metadata:title]:- "Chapter 2"

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

0 comments on commit 518a6ab

Please sign in to comment.