feat: allow creating articles with marka

This commit is contained in:
Max Richter
2025-10-31 15:26:34 +01:00
parent dfa3826ec5
commit 7e60327940
12 changed files with 332 additions and 210 deletions

View File

@@ -1,6 +1,5 @@
import { Handlers } from "$fresh/server.ts";
import { Readability } from "https://cdn.skypack.dev/@mozilla/readability";
import { DOMParser } from "domparser";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { createStreamResponse, isValidUrl } from "@lib/helpers.ts";
import * as openai from "@lib/openai.ts";
@@ -10,13 +9,10 @@ import { Recipe } from "@lib/resource/recipes.ts";
import recipeSchema from "@lib/recipeSchema.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { safeFileName } from "@lib/string.ts";
import { createDocument } from "@lib/documents.ts";
import { parseJsonLdToRecipeSchema } from "./parseJsonLd.ts";
import z from "npm:zod";
import z from "zod";
import { fetchHtmlWithPlaywright } from "@lib/playwright.ts";
const parser = new DOMParser();
const log = createLogger("api/article");
function makeUrlAbsolute(url: URL, src: string) {
@@ -49,7 +45,7 @@ async function extractUsingAI(
service.addRule("fix image links", {
filter: ["img"],
replacement: function (_: string, node: HTMLImageElement) {
replacement: function(_: string, node: HTMLImageElement) {
const src = node.getAttribute("src");
const alt = node.getAttribute("alt") || "";
if (!src || src.startsWith("data:image")) return "";
@@ -59,7 +55,7 @@ async function extractUsingAI(
});
service.addRule("fix normal links", {
filter: ["a"],
replacement: function (content: string, node: HTMLImageElement) {
replacement: function(content: string, node: HTMLImageElement) {
const href = node.getAttribute("href");
if (!href) return content;
@@ -73,9 +69,8 @@ async function extractUsingAI(
}
if (!href.startsWith("https://") && !href.startsWith("http://")) {
return `[${content}](${url.origin.replace(/\/$/, "")}/${
href.replace(/^\//, "")
})`;
return `[${content}](${url.origin.replace(/\/$/, "")}/${href.replace(/^\//, "")
})`;
}
return `[${content}](${href})`;
@@ -199,9 +194,8 @@ async function processCreateRecipeFromUrl(
const src = makeUrlAbsolute(url, newRecipe.meta.image);
if (src?.length > 5) {
const extension = fileExtension(new URL(src).pathname);
const finalPath = `Media/articles/images/${
safeFileName(id)
}_cover.${extension}`;
const finalPath = `Media/articles/images/${safeFileName(id)
}_cover.${extension}`;
streamResponse.enqueue("downloading image");
try {
streamResponse.enqueue("downloading image");