Skip to content

Commit

Permalink
Ad-hoc integration with Seonbi
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 10, 2024
1 parent 1e5287b commit 482fdb4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/api/v1/statuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
posts,
reactions,
} from "../../schema";
import { formatText } from "../../text";
import { formatPostContent } from "../../text";

const app = new Hono<{ Variables: Variables }>();

Expand Down Expand Up @@ -140,7 +140,9 @@ app.post(
const id = uuidv7();
const url = fedCtx.getObjectUri(Note, { username: handle, id });
const content =
data.status == null ? null : await formatText(db, data.status, fmtOpts);
data.status == null
? null
: await formatPostContent(db, data.status, data.language, fmtOpts);
const summary =
data.spoiler_text == null || data.spoiler_text.trim() === ""
? null
Expand Down Expand Up @@ -285,7 +287,9 @@ app.put(
}),
};
const content =
data.status == null ? null : await formatText(db, data.status, fmtOpts);
data.status == null
? null
: await formatPostContent(db, data.status, data.language, fmtOpts);
const summary =
data.spoiler_text == null || data.spoiler_text.trim() === ""
? null
Expand Down
76 changes: 75 additions & 1 deletion src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { persistAccount } from "./federation/account";
import { type ASPost, isPost } from "./federation/post";
import * as schema from "./schema";

const logger = getLogger(["hollo", "text"]);

export interface FormatResult {
html: string;
mentions: string[];
Expand Down Expand Up @@ -146,7 +148,7 @@ export async function formatText(
links: [],
};
const html = md.render(text, env);
getLogger(["hollo", "text"]).debug("Markdown-It environment: {env}", { env });
logger.debug("Markdown-It environment: {env}", { env });
let quoteTarget: ASPost | null = null;
for (const link of env.links) {
const object = await lookupObject(link, options);
Expand Down Expand Up @@ -238,4 +240,76 @@ export function extractText(html: string | null): string | null {
return $(":root").text();
}

// biome-ignore lint/complexity/useLiteralKeys: tsc claims about this
const SEONBI_URL = process.env["SEONBI_URL"];

export async function formatPostContent(
db: PgDatabase<
PostgresJsQueryResultHKT,
typeof schema,
ExtractTablesWithRelations<typeof schema>
>,
text: string,
language: string | undefined,
options: {
url: URL | string;
contextLoader?: DocumentLoader;
documentLoader?: DocumentLoader;
},
): Promise<FormatResult> {
const result = await formatText(db, text, options);
if (
SEONBI_URL != null &&
(language === "ko" || language?.startsWith("ko-"))
) {
const response = await fetch(SEONBI_URL, {
method: "POST",
body: JSON.stringify({
content: result.html,
contentType: "text/html",
quote: "HorizontalCornerBrackets",
cite: "AngleQuotes",
arrow: {
bidirArrow: true,
doubleArrow: true,
},
ellipsis: true,
emDash: true,
stop: "Horizontal",
hanja: {
rendering: "HanjaInRuby",
reading: {
initialSoundLaw: true,
useDictionaries: ["kr-stdict"],
dictionary: {},
},
},
}),
});
try {
const seonbiResult = await response.json();
if (seonbiResult.success) {
result.html = seonbiResult.content;
if (
Array.isArray(seonbiResult.warnings) &&
seonbiResult.warnings.length > 0
) {
logger.warn("Seonbi warnings: {warnings}", {
warnings: seonbiResult.warnings,
});
}
} else {
logger.error("Seonbi failed to format post content: {message}", {
message: seonbiResult.message,
});
}
} catch (error) {
logger.error("Failed to format post content with Seonbi: {error}", {
error,
});
}
}
return result;
}

// cSpell: ignore linkify

0 comments on commit 482fdb4

Please sign in to comment.