Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constructors/manifestConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function manifestCover(fileFormat: string): string {
*/
export function manifestImage(uri: string, fileFormat: string): string {
return `<item id="${uri.replace(
/.*\//,
/[^]*\//,
'',
)}" href="${uri}" media-type="image/${
fileFormat === 'jpg' ? 'jpeg' : fileFormat
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class EpubFile {
const idRef = `${sanitizeFileName(chapter.title)}_image_${imageIndex}`;

chapter.htmlBody = chapter.htmlBody
.replace(/(?<=<img[^>]+src=(?:"|')).+?(?="|')/gi, (uri: string) => {
.replace(/(?<=<img[^>]+?\bsrc=[\"\'])[^>]+?(?=[\"\'][>\s\/])/gi, (uri: string) => {
imageIndex++;
const fileType = getImageType(uri);
const path = `OEBPS/images/${idRef}.${fileType}`;
Expand All @@ -126,8 +126,8 @@ export default class EpubFile {
return `../../${path}`;
})
.replace(/&nbsp/g, '')
.replace(/(<img[^>]+>)(?!\s*<\/img>)/g, '$1</img>')
.replace(/<\/?(?:html|head|body|input)[^>]*>/g, '');
.replace(/<img[^>]+>(?!\s*<\/img>)/g, '$&</img>')
.replace(/<\/?(?:html|head|body|input)\b[^>]*>/g, '');

manifest.push(manifestChapter(idRef, chapter.fileName));
files.push(createChapter(chapter));
Expand Down
2 changes: 1 addition & 1 deletion src/methods/createChapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function createChapter(chapter: EpubChapter) {
allowedAttributes: false, //@ts-ignore
// disallowedTagsMode: 'completelyDiscard',
})
.replace(/<!--(.|\n)*?-->/gm, '') //? remove comments
.replace(/<!--[^]*?-->/g, '') //? remove comments
}
</body>
</html>
Expand Down
15 changes: 7 additions & 8 deletions src/methods/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ export function parseJSon(json: string) {
* @returns The extracted JSON string, or an empty string if no JSON is found.
*/
export function jsonExtractor(content: string) {
const jsonReg = new RegExp(/<JSON>(.|\n)*?<\/JSON>/, 'mgi');
const jsonReg = new RegExp(/<JSON>[^]*?<\/JSON>/, 'mgi');
return (single(jsonReg.exec(content)) ?? '')
.replace(/<JSON>/gim, '')
.replace(/<\/JSON>/gim, '');
.replace(/<\/?JSON>/gim, '');
}

/**
Expand All @@ -104,10 +103,9 @@ export function jsonExtractor(content: string) {
* @returns The extracted body content without the <body> tags.
*/
export function bodyExtrator(content: string) {
const jsonReg = new RegExp(/<body>(.|\n)*?<\/body>/, 'mgi');
const jsonReg = new RegExp(/<body>[^]*?<\/body>/, 'mgi');
return (single(jsonReg.exec(content)) ?? '')
.replace(/<body>/gim, '')
.replace(/<\/body>/gim, '');
.replace(/<\/?body>/gim, '');
}

/**
Expand Down Expand Up @@ -138,7 +136,8 @@ export function getImageType(path: string) {
* console.log(result); // Output: "example"
*/
export function removeFileExtension(name: string) {
return name.replace(/(.*)(\.opf|\.epub)/, '$1');
return name
.replace(/([^]+)(?:\.opf|\.epub)/, '$1');
}

/**
Expand All @@ -153,7 +152,7 @@ export function removeFileExtension(name: string) {
* console.log(sanitizedFileName); // Output: "my_filetxt"
*/
export function sanitizeFileName(fileName: string) {
return fileName.replaceAll(' ', '_').replace(/[^\w]/gi, ''); // remove all non-word and non-space characters
return fileName.replaceAll(' ', '_').replace(/[^\w]+/gi, ''); // remove all non-word and non-space characters
}

/**
Expand Down