feat: 发布 v0.4.0 桌面端
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
MarkdownDocumentParseError,
|
||||
renderMarkdown
|
||||
} from "@md-to-pdf/renderer";
|
||||
import {
|
||||
createThemeRegistry,
|
||||
type ThemeRegistryOptions
|
||||
} from "./theme-registry.js";
|
||||
|
||||
export const MAXIMUM_MARKDOWN_LENGTH = 1_500_000;
|
||||
|
||||
export interface MarkdownRenderRequest {
|
||||
markdown?: unknown;
|
||||
language?: unknown;
|
||||
}
|
||||
|
||||
export class ApplicationRequestError extends Error {
|
||||
constructor(
|
||||
readonly statusCode: 400 | 404 | 413,
|
||||
readonly code: string,
|
||||
message: string
|
||||
) {
|
||||
super(message);
|
||||
this.name = "ApplicationRequestError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface ApplicationServiceOptions extends ThemeRegistryOptions {}
|
||||
|
||||
export function createApplicationService(
|
||||
options: ApplicationServiceOptions
|
||||
) {
|
||||
const themes = createThemeRegistry(options);
|
||||
|
||||
function render(request: MarkdownRenderRequest) {
|
||||
const { markdown, language } = request;
|
||||
if (typeof markdown !== "string") {
|
||||
throw new ApplicationRequestError(
|
||||
400,
|
||||
"INVALID_MARKDOWN",
|
||||
"markdown 必须是字符串"
|
||||
);
|
||||
}
|
||||
if (markdown.length > MAXIMUM_MARKDOWN_LENGTH) {
|
||||
throw new ApplicationRequestError(
|
||||
413,
|
||||
"MARKDOWN_TOO_LARGE",
|
||||
"Markdown 内容不能超过 1.5 MB"
|
||||
);
|
||||
}
|
||||
if (language !== undefined && typeof language !== "string") {
|
||||
throw new ApplicationRequestError(
|
||||
400,
|
||||
"INVALID_LANGUAGE",
|
||||
"language 必须是字符串"
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return renderMarkdown(markdown, {
|
||||
...(language ? { language } : {})
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof MarkdownDocumentParseError) {
|
||||
throw new ApplicationRequestError(
|
||||
400,
|
||||
error.code,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function listThemes() {
|
||||
return {
|
||||
themes: (await themes.list()).map(({ manifest, source }) => ({
|
||||
id: manifest.id,
|
||||
name: manifest.name,
|
||||
version: manifest.version,
|
||||
description: manifest.description,
|
||||
bundled: manifest.bundled,
|
||||
source
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
getThemeAsset: themes.getAsset,
|
||||
getThemeCss: themes.getCss,
|
||||
invalidateThemes: themes.invalidate,
|
||||
listThemes,
|
||||
render
|
||||
};
|
||||
}
|
||||
|
||||
export type ApplicationService = ReturnType<
|
||||
typeof createApplicationService
|
||||
>;
|
||||
Reference in New Issue
Block a user