feat: 支持配置证书文本模板和作者显示方式

This commit is contained in:
Nixevol
2026-09-20 09:54:03 +08:00
parent c5c0168e4c
commit 6669722c00
13 changed files with 202 additions and 21 deletions
+20
View File
@@ -0,0 +1,20 @@
/** 证书正文只做字段替换,不执行表达式,也不渲染 Markdown/HTML。 */
export const DEFAULT_CERTIFICATE_TEXT_TEMPLATE = '{{作品名称}} {{版本}}';
export const CERTIFICATE_TEXT_FIELDS = ['作品名称', '版本', '追溯码', '档案编号', '作者'] as const;
export type CertificateTextField = typeof CERTIFICATE_TEXT_FIELDS[number];
export function validateCertificateTextTemplate(value: unknown): 'empty' | 'length' | 'fields' | null {
if (typeof value !== 'string' || !value.trim()) return 'empty';
if (value.length > 500) return 'length';
let invalid = false;
const remainder = value.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (_, field: string) => {
if (!(CERTIFICATE_TEXT_FIELDS as readonly string[]).includes(field.trim())) invalid = true;
return '';
});
return invalid || /[{}]/.test(remainder) ? 'fields' : null;
}
export function renderCertificateText(template: string, values: Record<CertificateTextField, string>): string {
if (validateCertificateTextTemplate(template)) throw new Error('证书文本模板格式无效');
return template.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (_, field: string) => values[field.trim() as CertificateTextField]).trim();
}
+1
View File
@@ -3,3 +3,4 @@ export * from './error-codes';
export * from './response';
export * from './constants';
export * from './trace-modules';
export * from './certificate-text';