Skip to content

Commit

Permalink
feat(epub): image, list, checkbox and svg
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Mar 26, 2023
1 parent 2bc4691 commit e47be15
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/better-write-languages/src/en-US/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default {
txt: {
generate: 'Successfully Downloaded TXT!',
},
epub: {
generate: 'Successfully Downloaded EPUB!',
},
annotations: {
fileDelete: 'Are you sure you want to delete the file?',
folderDelete: 'Are you sure you want to delete the folder?',
Expand Down
3 changes: 3 additions & 0 deletions packages/better-write-languages/src/pt-BR/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export default {
txt: {
generate: 'TXT Baixado com Sucesso!',
},
epub: {
generate: 'EPUB Baixado com Sucesso!',
},
html: {
generate: 'HTML Baixado com Sucesso!',
},
Expand Down
64 changes: 58 additions & 6 deletions packages/better-write-plugin-exporter-epub/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ export const PluginEpubSet = (
return `<h3>${hooks.substitution.purge(entity.raw)}</h3>`
}

const image = (entity: Entity) => {
return `<div><img src="${entity.raw}" alt=""></div>`
}

const svg = (entity: Entity) => {
console.log(entity.raw)

return `<div>${entity.raw}</div>`
}

const checkbox = (entity: Entity) => {
const id = hooks.utils.id().uuidv4()

return `<div><input type="checkbox" name="${id}" id="${id}" ${
entity?.external?.checkbox?.select ? `checked` : ''
}>
<label for="${id}">${hooks.substitution.purge(entity.raw)}</label></div>`
}

const list = (entity: Entity) => {
return `<ul><li style="list-style-type: square;">${hooks.substitution.purge(
entity.raw
)}</li></ul>`
}

const paragraph = (entity: Entity): string[] => {
if (
hooks.env.emptyLine() === entity.raw ||
Expand All @@ -35,24 +60,41 @@ export const PluginEpubSet = (
const target = parse(hooks.substitution.purge(row))

return target.reduce((acc, item) => {
// boldItalics
if (item.italic && item.bold)
return (acc += item.text.trim() ? `<i><b>${item.text}</b></i>` : '')

// italics
if (item.italic)
return (acc += item.text.trim() ? `<i>${item.text}</i>` : '')

// bold
if (item.bold)
return (acc += item.text.trim() ? `<b>${item.text}</b>` : '')

// common case
return (acc += item.text.trim() ? `<span>${item.text}</span>` : '')
}, '')
})
}

const pageBreak = () => {
return `<span style="page-break-after: always"></span>`
return `<span style="page-break-after: always;"></span>`
}

const lineBreak = () => {
return '<span style="width: 100%;padding-top: 1rem;border: none;"></span>'
return '<span style="width: 100%;height:16px;border: none;"></span>'
}

return {
paragraph,
headingOne,
headingTwo,
headingThree,
image,
svg,
checkbox,
list,
pageBreak,
lineBreak,
}
Expand All @@ -67,11 +109,15 @@ export const PluginEpubSet = (
content: '',
}

page.entities.forEach((entity: Entity) => {
for (const entity of page.entities) {
switch (entity.type) {
case 'paragraph':
case 'list':
case 'checkbox':
chapter.content += entities().checkbox(entity)
break
case 'list':
chapter.content += entities().list(entity)
break
case 'paragraph':
entities()
.paragraph(entity)
?.forEach(
Expand All @@ -93,8 +139,14 @@ export const PluginEpubSet = (
case 'line-break':
chapter.content += entities().lineBreak()
break
case 'image':
chapter.content += entities().image(entity)
break
case 'drau':
chapter.content += entities().svg(entity)
break
}
})
}

chapters.push(chapter)
})
Expand Down
9 changes: 9 additions & 0 deletions packages/better-write-plugin-exporter-epub/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getStyles = (
outline: 0;
font-feature-settings: 'ss02' on, 'ss01' on;
-webkit-font-smoothing: antialiased;
color: black;
}
p {
Expand All @@ -24,4 +25,12 @@ p > a {
h1, h2, h3 {
text-align: center;
}
i {
font-style: italic;
}
b {
font-weight: 700;
}`

0 comments on commit e47be15

Please sign in to comment.