Skip to content
Draft
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@
"typescript": "^5.9.2",
"vitest": "^3.1.4"
},
"optionalDependencies": {
"@seonbi/node": "0.2.2"
},
"packageManager": "pnpm@9.15.1+sha512.1acb565e6193efbebda772702950469150cf12bcc764262e7587e71d19dc98a423dff9536e57ea44c49bdf790ff694e83c27be5faa23d67e0c033b583be4bfcf"
}
65 changes: 65 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 103 additions & 52 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,106 @@ 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 type PostContentTransformer = (html: string) => Promise<string>;

const koPostContentTransformer = await determineKoPostContentTransformer();

async function determineKoPostContentTransformer(): Promise<PostContentTransformer> {
const SEONBI_NATIVE =
process.env.SEONBI_NATIVE?.trim()?.toLowerCase() === "true";

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

const SEONBI_CONFIGURATION = {
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: {},
},
},
} satisfies import("@seonbi/node").Configuration;

if (SEONBI_NATIVE) {
try {
const { transform } = await import("@seonbi/node");
return async (html: string) => {
try {
return transform(SEONBI_CONFIGURATION, html);
} catch (error) {
logger.error(
"Failed to format post content with Seonbi native: {error}",
{
error,
},
);
return html;
}
};
} catch {
logger.error(
"SEONBI_NATIVE is enabled but @seonbi/node is not installed",
);
}
}

if (SEONBI_URL != null) {
return async (html: string): Promise<string> => {
const response = await fetch(SEONBI_URL, {
method: "POST",
body: JSON.stringify({
...SEONBI_CONFIGURATION,
content: html,
}),
});
try {
const seonbiResult = await response.json();
if (seonbiResult.success) {
if (
Array.isArray(seonbiResult.warnings) &&
seonbiResult.warnings.length > 0
) {
logger.warn("Seonbi warnings: {warnings}", {
warnings: seonbiResult.warnings,
});
}
return seonbiResult.content;
}
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 html;
};
}

return async (html: string) => html;
}

function getPostContentTransformer(
language: string | null | undefined,
): PostContentTransformer | null {
if (language === "ko" || language?.startsWith("ko-")) {
return koPostContentTransformer;
}
return null;
}

export async function formatPostContent(
db: PgDatabase<
Expand All @@ -280,56 +378,9 @@ export async function formatPostContent(
},
): 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,
});
}
const transformer = getPostContentTransformer(language);
if (transformer != null) {
result.html = await transformer(result.html);
}
return result;
}
Expand Down