|
| 1 | +import type { Context, Page } from '@/types' |
| 2 | +import type { PageTransformer } from './types' |
| 3 | +import type { CategorizedEvents } from '@/audit-logs/types' |
| 4 | +import { renderContent } from '@/content-render/index' |
| 5 | +import matter from '@gr2m/gray-matter' |
| 6 | +import { readFileSync } from 'fs' |
| 7 | +import { join, dirname } from 'path' |
| 8 | +import { fileURLToPath } from 'url' |
| 9 | + |
| 10 | +const __filename = fileURLToPath(import.meta.url) |
| 11 | +const __dirname = dirname(__filename) |
| 12 | + |
| 13 | +/** |
| 14 | + * Transformer for Audit Logs pages |
| 15 | + * Converts audit log events and their data into markdown format using a Liquid template |
| 16 | + */ |
| 17 | +export class AuditLogsTransformer implements PageTransformer { |
| 18 | + canTransform(page: Page): boolean { |
| 19 | + return page.autogenerated === 'audit-logs' |
| 20 | + } |
| 21 | + |
| 22 | + async transform(page: Page, pathname: string, context: Context): Promise<string> { |
| 23 | + // Import audit log lib dynamically to avoid circular dependencies |
| 24 | + const { getCategorizedAuditLogEvents, getCategoryNotes, resolveReferenceLinksToMarkdown } = |
| 25 | + await import('@/audit-logs/lib/index') |
| 26 | + |
| 27 | + // Extract version from context |
| 28 | + const currentVersion = context.currentVersion! |
| 29 | + |
| 30 | + let pageType = '' |
| 31 | + if (pathname.includes('/security-log-events')) { |
| 32 | + pageType = 'user' |
| 33 | + } else if (pathname.includes('/audit-log-events-for-your-enterprise')) { |
| 34 | + pageType = 'enterprise' |
| 35 | + } else if (pathname.includes('/audit-log-events-for-your-organization')) { |
| 36 | + pageType = 'organization' |
| 37 | + } else { |
| 38 | + throw new Error(`Unknown audit log page type for path: ${pathname}`) |
| 39 | + } |
| 40 | + |
| 41 | + // Get the audit log events data |
| 42 | + const categorizedEvents = getCategorizedAuditLogEvents(pageType, currentVersion) |
| 43 | + const categoryNotes = getCategoryNotes() |
| 44 | + |
| 45 | + // Prepare manual content |
| 46 | + let manualContent = '' |
| 47 | + if (page.markdown) { |
| 48 | + const markerIndex = page.markdown.indexOf( |
| 49 | + '<!-- Content after this section is automatically generated -->', |
| 50 | + ) |
| 51 | + if (markerIndex > 0) { |
| 52 | + const { content } = matter(page.markdown) |
| 53 | + const manualContentMarkerIndex = content.indexOf( |
| 54 | + '<!-- Content after this section is automatically generated -->', |
| 55 | + ) |
| 56 | + if (manualContentMarkerIndex > 0) { |
| 57 | + const rawManualContent = content.substring(0, manualContentMarkerIndex).trim() |
| 58 | + if (rawManualContent) { |
| 59 | + manualContent = await renderContent(rawManualContent, { |
| 60 | + ...context, |
| 61 | + markdownRequested: true, |
| 62 | + }) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Prepare data for template |
| 69 | + const templateData = await this.prepareTemplateData( |
| 70 | + page, |
| 71 | + categorizedEvents, |
| 72 | + categoryNotes, |
| 73 | + context, |
| 74 | + manualContent, |
| 75 | + resolveReferenceLinksToMarkdown, |
| 76 | + ) |
| 77 | + |
| 78 | + // Load and render template |
| 79 | + const templatePath = join(__dirname, '../templates/audit-logs-page.template.md') |
| 80 | + const templateContent = readFileSync(templatePath, 'utf8') |
| 81 | + |
| 82 | + // Render the template with Liquid |
| 83 | + const rendered = await renderContent(templateContent, { |
| 84 | + ...context, |
| 85 | + ...templateData, |
| 86 | + markdownRequested: true, |
| 87 | + }) |
| 88 | + |
| 89 | + return rendered |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Prepare data for the Liquid template |
| 94 | + */ |
| 95 | + private async prepareTemplateData( |
| 96 | + page: Page, |
| 97 | + categorizedEvents: CategorizedEvents, |
| 98 | + categoryNotes: Record<string, string>, |
| 99 | + context: Context, |
| 100 | + manualContent: string, |
| 101 | + resolveReferenceLinksToMarkdown: (docsReferenceLinks: string, context: any) => Promise<string>, |
| 102 | + ): Promise<Record<string, unknown>> { |
| 103 | + // Prepare page intro |
| 104 | + const intro = page.intro ? await page.renderProp('intro', context, { textOnly: true }) : '' |
| 105 | + |
| 106 | + // Sort categories and events |
| 107 | + const sortedCategorizedEvents: CategorizedEvents = {} |
| 108 | + const sortedCategories = Object.keys(categorizedEvents).sort((a, b) => a.localeCompare(b)) |
| 109 | + |
| 110 | + for (const category of sortedCategories) { |
| 111 | + // Create a copy of the events array to avoid mutating the cache |
| 112 | + const events = [...categorizedEvents[category]].sort((a, b) => |
| 113 | + a.action.localeCompare(b.action), |
| 114 | + ) |
| 115 | + sortedCategorizedEvents[category] = await Promise.all( |
| 116 | + events.map(async (event) => { |
| 117 | + const newEvent = { ...event } |
| 118 | + if (newEvent.docs_reference_links && newEvent.docs_reference_links !== 'N/A') { |
| 119 | + newEvent.docs_reference_links = await resolveReferenceLinksToMarkdown( |
| 120 | + newEvent.docs_reference_links, |
| 121 | + context, |
| 122 | + ) |
| 123 | + } |
| 124 | + return newEvent |
| 125 | + }), |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + return { |
| 130 | + page: { |
| 131 | + title: page.title, |
| 132 | + intro, |
| 133 | + }, |
| 134 | + manualContent, |
| 135 | + categorizedEvents: sortedCategorizedEvents, |
| 136 | + categoryNotes, |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments