readme
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { marked } from "marked";
|
||||
import { mediaUrl } from "@/app/lib/media";
|
||||
|
||||
export interface EbookChunk {
|
||||
html: string;
|
||||
@@ -10,9 +11,21 @@ export interface EbookData {
|
||||
headers: string[];
|
||||
}
|
||||
|
||||
function rewriteMediaTag(tag: string): string {
|
||||
return tag.replace(/\s(src|poster)=(["'])(.*?)\2/gi, (match, attr: string, quote: string, value: string) => {
|
||||
const rewritten = mediaUrl(value);
|
||||
return rewritten ? ` ${attr}=${quote}${rewritten}${quote}` : match;
|
||||
});
|
||||
}
|
||||
|
||||
function rewriteEmbeddedMedia(html: string): string {
|
||||
return html.replace(/<(img|video|audio|source)\b[^>]*>/gi, rewriteMediaTag);
|
||||
}
|
||||
|
||||
export function parseEbookMarkdown(source: string): EbookData {
|
||||
const html = marked.parse(source, { breaks: true, gfm: true }) as string;
|
||||
const lazyHtml = html.replace(/<img /g, '<img loading="lazy" decoding="async" ');
|
||||
const mediaHtml = rewriteEmbeddedMedia(html);
|
||||
const lazyHtml = mediaHtml.replace(/<img /g, '<img loading="lazy" decoding="async" ');
|
||||
|
||||
let rawChunks = lazyHtml.split(/(?=<h1[\s>])/i).filter((c) => c.trim());
|
||||
if (rawChunks.length <= 1) {
|
||||
|
||||
Reference in New Issue
Block a user