|
| 1 | +import type { TagToken, Context as LiquidContext } from 'liquidjs' |
| 2 | +import { fastTextOnly } from '@/content-render/unified/text-only' |
| 3 | +import { renderContent } from '@/content-render/index' |
| 4 | +import type { Context } from '@/types' |
| 5 | +import type { Parameter, BodyParameter, ChildParameter, StatusCode } from '@/rest/components/types' |
| 6 | +import { createLogger } from '@/observability/logger' |
| 7 | + |
| 8 | +const logger = createLogger('article-api/liquid-renderers/rest-tags') |
| 9 | + |
| 10 | +/** |
| 11 | + * Custom Liquid tag for rendering REST API parameters |
| 12 | + * Usage: {% rest_parameter param %} |
| 13 | + */ |
| 14 | +export class RestParameter { |
| 15 | + private paramName: string |
| 16 | + |
| 17 | + constructor( |
| 18 | + token: TagToken, |
| 19 | + remainTokens: TagToken[], |
| 20 | + liquid: { options: any; parser: any }, |
| 21 | + private liquidContext?: LiquidContext, |
| 22 | + ) { |
| 23 | + // The tag receives the parameter object from the template context |
| 24 | + this.paramName = token.args.trim() |
| 25 | + } |
| 26 | + |
| 27 | + async render(ctx: LiquidContext, emitter: any): Promise<void> { |
| 28 | + const param = ctx.get([this.paramName]) as Parameter |
| 29 | + const context = ctx.get(['context']) as Context |
| 30 | + |
| 31 | + if (!param) { |
| 32 | + emitter.write('') |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const lines: string[] = [] |
| 37 | + const required = param.required ? ' (required)' : '' |
| 38 | + const type = param.schema?.type || 'string' |
| 39 | + |
| 40 | + lines.push(`- **\`${param.name}\`** (${type})${required}`) |
| 41 | + |
| 42 | + if (param.description) { |
| 43 | + const description = await htmlToMarkdown(param.description, context) |
| 44 | + lines.push(` ${description}`) |
| 45 | + } |
| 46 | + |
| 47 | + if (param.schema?.default !== undefined) { |
| 48 | + lines.push(` Default: \`${param.schema.default}\``) |
| 49 | + } |
| 50 | + |
| 51 | + if (param.schema?.enum && param.schema.enum.length > 0) { |
| 52 | + lines.push(` Can be one of: ${param.schema.enum.map((v) => `\`${v}\``).join(', ')}`) |
| 53 | + } |
| 54 | + |
| 55 | + emitter.write(lines.join('\n')) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Custom Liquid tag for rendering REST API body parameters |
| 61 | + * Usage: {% rest_body_parameter param indent %} |
| 62 | + */ |
| 63 | +export class RestBodyParameter { |
| 64 | + constructor( |
| 65 | + token: TagToken, |
| 66 | + remainTokens: TagToken[], |
| 67 | + liquid: { options: any; parser: any }, |
| 68 | + private liquidContext?: LiquidContext, |
| 69 | + ) { |
| 70 | + // Parse arguments - param name and optional indent level |
| 71 | + const args = token.args.trim().split(/\s+/) |
| 72 | + this.param = args[0] |
| 73 | + this.indent = args[1] ? parseInt(args[1]) : 0 |
| 74 | + } |
| 75 | + |
| 76 | + private param: string |
| 77 | + private indent: number |
| 78 | + |
| 79 | + async render(ctx: LiquidContext, emitter: any): Promise<void> { |
| 80 | + const param = ctx.get([this.param]) as BodyParameter |
| 81 | + const context = ctx.get(['context']) as Context |
| 82 | + const indent = this.indent |
| 83 | + |
| 84 | + if (!param) { |
| 85 | + emitter.write('') |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + const lines: string[] = [] |
| 90 | + const prefix = ' '.repeat(indent) |
| 91 | + const required = param.isRequired ? ' (required)' : '' |
| 92 | + const type = param.type || 'string' |
| 93 | + |
| 94 | + lines.push(`${prefix}- **\`${param.name}\`** (${type})${required}`) |
| 95 | + |
| 96 | + if (param.description) { |
| 97 | + const description = await htmlToMarkdown(param.description, context) |
| 98 | + lines.push(`${prefix} ${description}`) |
| 99 | + } |
| 100 | + |
| 101 | + if (param.default !== undefined) { |
| 102 | + lines.push(`${prefix} Default: \`${param.default}\``) |
| 103 | + } |
| 104 | + |
| 105 | + if (param.enum && param.enum.length > 0) { |
| 106 | + lines.push(`${prefix} Can be one of: ${param.enum.map((v) => `\`${v}\``).join(', ')}`) |
| 107 | + } |
| 108 | + |
| 109 | + // Handle nested parameters |
| 110 | + if (param.childParamsGroups && param.childParamsGroups.length > 0) { |
| 111 | + for (const childGroup of param.childParamsGroups) { |
| 112 | + lines.push(await renderChildParameter(childGroup, context, indent + 1)) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + emitter.write(lines.join('\n')) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Custom Liquid tag for rendering REST API status codes |
| 122 | + * Usage: {% rest_status_code statusCode %} |
| 123 | + */ |
| 124 | +export class RestStatusCode { |
| 125 | + private statusCodeName: string |
| 126 | + |
| 127 | + constructor( |
| 128 | + token: TagToken, |
| 129 | + remainTokens: TagToken[], |
| 130 | + liquid: { options: any; parser: any }, |
| 131 | + private liquidContext?: LiquidContext, |
| 132 | + ) { |
| 133 | + this.statusCodeName = token.args.trim() |
| 134 | + } |
| 135 | + |
| 136 | + async render(ctx: LiquidContext, emitter: any): Promise<void> { |
| 137 | + const statusCode = ctx.get([this.statusCodeName]) as StatusCode |
| 138 | + const context = ctx.get(['context']) as Context |
| 139 | + |
| 140 | + if (!statusCode) { |
| 141 | + emitter.write('') |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + const lines: string[] = [] |
| 146 | + |
| 147 | + if (statusCode.description) { |
| 148 | + const description = await htmlToMarkdown(statusCode.description, context) |
| 149 | + lines.push(`- **${statusCode.httpStatusCode}**`) |
| 150 | + if (description.trim()) { |
| 151 | + lines.push(` ${description.trim()}`) |
| 152 | + } |
| 153 | + } else if (statusCode.httpStatusMessage) { |
| 154 | + lines.push(`- **${statusCode.httpStatusCode}** - ${statusCode.httpStatusMessage}`) |
| 155 | + } else { |
| 156 | + lines.push(`- **${statusCode.httpStatusCode}**`) |
| 157 | + } |
| 158 | + |
| 159 | + emitter.write(lines.join('\n')) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Helper function to render child parameters recursively |
| 165 | + */ |
| 166 | +async function renderChildParameter( |
| 167 | + param: ChildParameter, |
| 168 | + context: Context, |
| 169 | + indent: number, |
| 170 | +): Promise<string> { |
| 171 | + const lines: string[] = [] |
| 172 | + const prefix = ' '.repeat(indent) |
| 173 | + const required = param.isRequired ? ' (required)' : '' |
| 174 | + const type = param.type || 'string' |
| 175 | + |
| 176 | + lines.push(`${prefix}- **\`${param.name}\`** (${type})${required}`) |
| 177 | + |
| 178 | + if (param.description) { |
| 179 | + const description = await htmlToMarkdown(param.description, context) |
| 180 | + lines.push(`${prefix} ${description}`) |
| 181 | + } |
| 182 | + |
| 183 | + if (param.default !== undefined) { |
| 184 | + lines.push(`${prefix} Default: \`${param.default}\``) |
| 185 | + } |
| 186 | + |
| 187 | + if (param.enum && param.enum.length > 0) { |
| 188 | + lines.push(`${prefix} Can be one of: ${param.enum.map((v: string) => `\`${v}\``).join(', ')}`) |
| 189 | + } |
| 190 | + |
| 191 | + // Recursively handle nested parameters |
| 192 | + if (param.childParamsGroups && param.childParamsGroups.length > 0) { |
| 193 | + for (const child of param.childParamsGroups) { |
| 194 | + lines.push(await renderChildParameter(child, context, indent + 1)) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return lines.join('\n') |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Helper function to convert HTML to markdown |
| 203 | + */ |
| 204 | +async function htmlToMarkdown(html: string, context: Context): Promise<string> { |
| 205 | + if (!html) return '' |
| 206 | + |
| 207 | + try { |
| 208 | + const rendered = await renderContent(html, context, { textOnly: false }) |
| 209 | + return fastTextOnly(rendered) |
| 210 | + } catch (error) { |
| 211 | + logger.error('Failed to render HTML content to markdown in REST tag', { |
| 212 | + error, |
| 213 | + html: html.substring(0, 100), // First 100 chars for context |
| 214 | + contextInfo: context && context.page ? { page: context.page.relativePath } : undefined, |
| 215 | + }) |
| 216 | + // In non-production, re-throw to aid debugging |
| 217 | + if (process.env.NODE_ENV !== 'production') { |
| 218 | + throw error |
| 219 | + } |
| 220 | + // Fallback to simple text extraction |
| 221 | + return fastTextOnly(html) |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +// Export tag names for registration |
| 226 | +export const restTags = { |
| 227 | + rest_parameter: RestParameter, |
| 228 | + rest_body_parameter: RestBodyParameter, |
| 229 | + rest_status_code: RestStatusCode, |
| 230 | +} |
0 commit comments