|
| 1 | +import type { Context, Page, SecretScanningData } from '@/types' |
| 2 | +import type { PageTransformer } from './types' |
| 3 | +import fs from 'fs' |
| 4 | +import yaml from 'js-yaml' |
| 5 | +import path from 'path' |
| 6 | +import { getVersionInfo } from '@/app/lib/constants' |
| 7 | +import { liquid } from '@/content-render/index' |
| 8 | +import { allVersions } from '@/versions/lib/all-versions' |
| 9 | + |
| 10 | +/** |
| 11 | + * Transformer for Secret Scanning pages. |
| 12 | + * Loads pattern data and converts secret scanning documentation into markdown format. |
| 13 | + * Used by the Article API to render Secret Scanning documentation dynamically. |
| 14 | + */ |
| 15 | +export class SecretScanningTransformer implements PageTransformer { |
| 16 | + canTransform(page: Page): boolean { |
| 17 | + return page.autogenerated === 'secret-scanning' |
| 18 | + } |
| 19 | + |
| 20 | + async transform(page: Page, _pathname: string, context: Context): Promise<string> { |
| 21 | + if (!context.secretScanningData) { |
| 22 | + const currentVersion = context.currentVersion |
| 23 | + if (!currentVersion) throw new Error('currentVersion is required') |
| 24 | + |
| 25 | + const { isEnterpriseCloud, isEnterpriseServer } = getVersionInfo(currentVersion) |
| 26 | + const versionPath = isEnterpriseCloud |
| 27 | + ? 'ghec' |
| 28 | + : isEnterpriseServer |
| 29 | + ? `ghes-${allVersions[currentVersion].currentRelease}` |
| 30 | + : 'fpt' |
| 31 | + |
| 32 | + const secretScanningDir = path.join(process.cwd(), 'src/secret-scanning/data/pattern-docs') |
| 33 | + const filepath = path.join(secretScanningDir, versionPath, 'public-docs.yml') |
| 34 | + |
| 35 | + if (fs.existsSync(filepath)) { |
| 36 | + const data = yaml.load(fs.readFileSync(filepath, 'utf-8')) as SecretScanningData[] |
| 37 | + |
| 38 | + // Process Liquid in values |
| 39 | + for (const entry of data) { |
| 40 | + // Only process Liquid for the hasValidityCheck field, as in the middleware |
| 41 | + if (typeof entry.hasValidityCheck === 'string' && entry.hasValidityCheck.includes('{%')) { |
| 42 | + // Render Liquid and parse as YAML to get correct boolean type |
| 43 | + entry.hasValidityCheck = yaml.load( |
| 44 | + await liquid.parseAndRender(entry.hasValidityCheck, context), |
| 45 | + ) as boolean |
| 46 | + } |
| 47 | + |
| 48 | + if (entry.isduplicate) { |
| 49 | + entry.secretType += ' <br/><a href="#token-versions">Token versions</a>' |
| 50 | + } |
| 51 | + if (entry.ismultipart) { |
| 52 | + entry.secretType += ' <br/><a href="#multi-part-secrets">Multi-part secrets</a>' |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + context.secretScanningData = data |
| 57 | + } else { |
| 58 | + // If the file does not exist, set to empty array to ensure predictable behavior |
| 59 | + context.secretScanningData = [] |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + context.markdownRequested = true |
| 64 | + let content = await page.render(context) |
| 65 | + |
| 66 | + // Strip HTML comments from the rendered content |
| 67 | + content = content.replace(/<!--.*?-->/gs, '') |
| 68 | + |
| 69 | + // Normalize whitespace after stripping comments |
| 70 | + content = content.replace(/\n{3,}/g, '\n\n').trim() |
| 71 | + |
| 72 | + const intro = page.intro ? await page.renderProp('intro', context, { textOnly: true }) : '' |
| 73 | + |
| 74 | + return `# ${page.title}\n\n${intro}\n\n${content}` |
| 75 | + } |
| 76 | +} |
0 commit comments