-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generator): .epub initial implementation
- Loading branch information
Showing
19 changed files
with
2,350 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
<template> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M20 2H4v20h16V2zM6 4h5v8l-2.5-1.5L6 12V4z"></path></svg> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.vscode | ||
.DS_Store | ||
|
||
node_modules | ||
dist | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 2, | ||
"singleQuote": true, | ||
"semi": false, | ||
"vueIndentScriptAndStyle": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "better-write-plugin-exporter-epub", | ||
"version": "1.0.0", | ||
"author": "Novout", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\"", | ||
"build": "tsup", | ||
"lint": "prettier --write src/" | ||
}, | ||
"files": [ | ||
"dist/**/*", | ||
"package.json", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"dependencies": { | ||
"better-write-plugin-core": "^1.0.0", | ||
"better-write-types": "^1.0.0", | ||
"better-write-contenteditable-ast": "^1.0.0", | ||
"epub-gen-memory": "1.0.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { PluginTypes } from 'better-write-types' | ||
import { createPlugin } from 'better-write-plugin-core' | ||
import { PluginEpubSet } from './set' | ||
|
||
export const EpubPlugin = (): PluginTypes.Plugin => | ||
createPlugin({ name: 'epub' }, [PluginEpubSet]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { saveAs } from 'file-saver' | ||
import { ContextState, Entity, PluginTypes } from 'better-write-types' | ||
import { On } from 'better-write-plugin-core' | ||
import { getRows, parse } from 'better-write-contenteditable-ast' | ||
import EPUB, { Chapter } from 'epub-gen-memory/bundle' | ||
import { getStyles } from './styles' | ||
|
||
export const PluginEpubSet = ( | ||
emitter: PluginTypes.PluginEmitter, | ||
stores: PluginTypes.PluginStores, | ||
hooks: PluginTypes.PluginHooks | ||
) => { | ||
const entities = () => { | ||
const headingOne = (entity: Entity) => { | ||
return hooks.substitution.purge(entity.raw) | ||
} | ||
|
||
const headingTwo = (entity: Entity) => { | ||
return `<h2>${hooks.substitution.purge(entity.raw)}</h2>` | ||
} | ||
|
||
const headingThree = (entity: Entity) => { | ||
return `<h3>${hooks.substitution.purge(entity.raw)}</h3>` | ||
} | ||
|
||
const paragraph = (entity: Entity): string[] => { | ||
if ( | ||
hooks.env.emptyLine() === entity.raw || | ||
entity.raw === '' || | ||
entity.raw === ' ' | ||
) | ||
return [lineBreak()] | ||
|
||
return getRows(entity.raw).map((row) => { | ||
const target = parse(hooks.substitution.purge(row)) | ||
|
||
return target.reduce((acc, item) => { | ||
return (acc += item.text.trim() ? `<span>${item.text}</span>` : '') | ||
}, '') | ||
}) | ||
} | ||
|
||
const pageBreak = () => { | ||
return `<span style="page-break-after: always"></span>` | ||
} | ||
|
||
const lineBreak = () => { | ||
return '<span style="width: 100%;padding-top: 1rem;border: none;"></span>' | ||
} | ||
|
||
return { | ||
paragraph, | ||
headingOne, | ||
headingTwo, | ||
headingThree, | ||
pageBreak, | ||
lineBreak, | ||
} | ||
} | ||
|
||
const contents = (): Chapter[] => { | ||
const chapters: Chapter[] = [] | ||
|
||
stores.PROJECT.pages.forEach((page: ContextState) => { | ||
const chapter = { | ||
title: '', | ||
content: '', | ||
} | ||
|
||
page.entities.forEach((entity: Entity) => { | ||
switch (entity.type) { | ||
case 'paragraph': | ||
case 'list': | ||
case 'checkbox': | ||
entities() | ||
.paragraph(entity) | ||
?.forEach( | ||
(paragraph) => (chapter.content += `<p>${paragraph}</p>`) | ||
) | ||
break | ||
case 'heading-one': | ||
chapter.title = entities().headingOne(entity) | ||
break | ||
case 'heading-two': | ||
chapter.content += entities().headingTwo(entity) | ||
break | ||
case 'heading-three': | ||
chapter.content += entities().headingThree(entity) | ||
break | ||
case 'page-break': | ||
chapter.content += entities().pageBreak() | ||
break | ||
case 'line-break': | ||
chapter.content += entities().lineBreak() | ||
break | ||
} | ||
}) | ||
|
||
chapters.push(chapter) | ||
}) | ||
|
||
return chapters | ||
} | ||
|
||
const generate = () => { | ||
EPUB( | ||
{ | ||
title: stores.PROJECT.nameRaw, | ||
author: stores.PROJECT.creator, | ||
description: stores.PROJECT.subject, | ||
publisher: stores.PROJECT.producer, | ||
tocTitle: hooks.i18n.t('editor.bar.epub.table') ?? undefined, | ||
date: new Date().toString(), | ||
css: getStyles(stores, hooks), | ||
cover: stores.PDF.styles.base.background.data ?? undefined, | ||
}, | ||
contents() | ||
).then( | ||
(content) => download(content), | ||
(_) => hooks.toast.error(hooks.i18n.t('toast.generics.error')) | ||
) | ||
} | ||
|
||
const download = (blob: Blob) => { | ||
saveAs(blob, hooks.project.utils().exportFullName('epub')) | ||
|
||
hooks.toast.success(hooks.i18n.t('toast.project.epub.generate')) | ||
} | ||
|
||
On.externals().PluginEpubGenerate(emitter, [ | ||
() => { | ||
generate() | ||
}, | ||
() => {}, | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PluginTypes } from 'better-write-types' | ||
|
||
export const getStyles = ( | ||
stores: PluginTypes.PluginStores, | ||
hooks: PluginTypes.PluginHooks | ||
): string => `* { | ||
margin: 0; | ||
padding: 0; | ||
outline: 0; | ||
font-feature-settings: 'ss02' on, 'ss01' on; | ||
-webkit-font-smoothing: antialiased; | ||
} | ||
p { | ||
font-size: 18px; | ||
text-indent: 1.5rem; | ||
color: black; | ||
} | ||
p > a { | ||
text-decoration: none; | ||
color: black !important; | ||
} | ||
h1, h2, h3 { | ||
text-align: center; | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"include": [ | ||
"src/types/index.ts", | ||
"src/**/*.ts", | ||
"test/**/*.ts", | ||
"demo/**/*.ts", | ||
"demo/**/*.d.ts", | ||
"demo/**/*.tsx", | ||
"demo/**/*.vue" | ||
], | ||
"exclude": ["dist", "node_modules"], | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"rootDir": ".", | ||
"outDir": "dist", | ||
"sourceMap": false, | ||
"noEmit": true, | ||
"declaration": true, | ||
"target": "esnext", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"skipLibCheck": true, | ||
"noUnusedLocals": true, | ||
"strictNullChecks": true, | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"noImplicitReturns": true, | ||
"suppressImplicitAnyIndexErrors": true, | ||
"strict": true, | ||
"isolatedModules": false, | ||
"experimentalDecorators": true, | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"removeComments": false, | ||
"strictPropertyInitialization": false, | ||
"jsx": "preserve", | ||
"lib": ["esnext", "dom"], | ||
"types": ["node"], | ||
"plugins": [ | ||
{ | ||
"name": "@vuedx/typescript-plugin-vue" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'tsup'; | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
format: ['esm', 'cjs'], | ||
clean: true, | ||
dts: true, | ||
external: ['file-saver', 'epub-gen-memory'] | ||
}) |
Oops, something went wrong.