|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { z } from 'zod'; |
| 5 | +import { parse as parseMatter } from 'zod-matter'; |
| 6 | + |
| 7 | +// Define the front matter schema |
| 8 | +const frontMatterSchema = z.object({ |
| 9 | + // Optional fields that affect file filtering |
| 10 | + no_edit_this_page: z.boolean().optional(), |
| 11 | + redirect_to: z.string().optional(), |
| 12 | + block_external_search_index: z.boolean().optional(), |
| 13 | + allow_long_title: z.boolean().optional(), |
| 14 | + // Other common fields |
| 15 | + title: z.string().optional(), |
| 16 | + meta_desc: z.string().optional(), |
| 17 | + meta_image: z.string().optional(), |
| 18 | +}); |
| 19 | + |
| 20 | +type FrontMatter = z.infer<typeof frontMatterSchema>; |
| 21 | + |
| 22 | +export interface MarkdownFile { |
| 23 | + path: string; |
| 24 | + isNew: boolean; |
| 25 | + content: string; |
| 26 | + frontMatter?: FrontMatter; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Different modes for finding markdown files and determining if they're new |
| 31 | + */ |
| 32 | +type FinderMode = 'git' | 'gha' | 'standard'; |
| 33 | + |
| 34 | +const AUTO_GENERATED_HEADING_REGEX = /^> This page was automatically generated\./m; |
| 35 | + |
| 36 | +/** |
| 37 | + * Parse front matter from a markdown file |
| 38 | + */ |
| 39 | +function parseFrontMatter(filePath: string): FrontMatter | undefined { |
| 40 | + try { |
| 41 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 42 | + |
| 43 | + // Check for auto-generated heading in content |
| 44 | + if (content.match(AUTO_GENERATED_HEADING_REGEX)) { |
| 45 | + return { no_edit_this_page: true }; |
| 46 | + } |
| 47 | + |
| 48 | + // Parse front matter with zod-matter |
| 49 | + const { data } = parseMatter(content, frontMatterSchema); |
| 50 | + return data; |
| 51 | + } catch (e) { |
| 52 | + // If there's no front matter or it's invalid, return null |
| 53 | + return undefined; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Check if a file should be excluded based on its front matter |
| 59 | + */ |
| 60 | +function shouldExcludeByFrontMatter(frontMatter: FrontMatter | undefined): boolean { |
| 61 | + if (!frontMatter) return false; |
| 62 | + |
| 63 | + return frontMatter.no_edit_this_page === true || |
| 64 | + typeof frontMatter.redirect_to === 'string' || |
| 65 | + frontMatter.block_external_search_index === true || |
| 66 | + !!frontMatter.allow_long_title; |
| 67 | +} |
| 68 | + |
| 69 | +import { debug } from './utils'; |
| 70 | + |
| 71 | +/** |
| 72 | + * Get list of files modified in the latest git commit |
| 73 | + */ |
| 74 | +function getGitModifiedFiles(): Set<string> { |
| 75 | + try { |
| 76 | + debug('Getting git modified files...'); |
| 77 | + const output = execSync('git diff-tree --no-commit-id --name-only -r HEAD', { encoding: 'utf8' }); |
| 78 | + const set = new Set(output.split('\n').filter(Boolean)); |
| 79 | + debug('Modified files:', set); |
| 80 | + return set; |
| 81 | + } catch (error) { |
| 82 | + console.warn('Failed to get git modified files, treating all files as existing'); |
| 83 | + return new Set<string>(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Get list of files modified in the current PR (GitHub Actions) |
| 89 | + */ |
| 90 | +function getGHAModifiedFiles(): Set<string> { |
| 91 | + const eventPath = process.env.GITHUB_EVENT_PATH; |
| 92 | + if (!eventPath) { |
| 93 | + console.warn('No GITHUB_EVENT_PATH found, treating all files as existing'); |
| 94 | + return new Set<string>(); |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + const event = JSON.parse(fs.readFileSync(eventPath, 'utf8')); |
| 99 | + const files = event.pull_request?.changed_files || []; |
| 100 | + return new Set(files); |
| 101 | + } catch (error) { |
| 102 | + console.warn('Failed to get PR modified files, treating all files as existing'); |
| 103 | + return new Set<string>(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Find all markdown files in the given directory and its subdirectories. |
| 109 | + * Optionally marks files as new based on git/GHA context. |
| 110 | + */ |
| 111 | +export function findMarkdownFiles( |
| 112 | + rootDir: string, |
| 113 | + mode: FinderMode = 'standard', |
| 114 | + excludePaths: string[] = ['/content/docs/reference/pkg', '/content/registry'] |
| 115 | +): MarkdownFile[] { |
| 116 | + const files: MarkdownFile[] = []; |
| 117 | + const modifiedFiles = mode === 'git' ? getGitModifiedFiles() |
| 118 | + : mode === 'gha' ? getGHAModifiedFiles() |
| 119 | + : new Set<string>(); |
| 120 | + |
| 121 | + function isExcluded(filePath: string): boolean { |
| 122 | + return excludePaths.some(excludePath => filePath.includes(excludePath)); |
| 123 | + } |
| 124 | + |
| 125 | + function searchDirectory(dir: string) { |
| 126 | + const entries = fs.readdirSync(dir); |
| 127 | + |
| 128 | + for (const entry of entries) { |
| 129 | + const fullPath = path.join(dir, entry); |
| 130 | + const stat = fs.statSync(fullPath); |
| 131 | + |
| 132 | + if (isExcluded(fullPath)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + if (stat.isDirectory()) { |
| 137 | + searchDirectory(fullPath); |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + if (path.extname(fullPath) === '.md') { |
| 142 | + const content = fs.readFileSync(fullPath, 'utf8'); |
| 143 | + const frontMatter = parseFrontMatter(fullPath); |
| 144 | + if (!shouldExcludeByFrontMatter(frontMatter)) { |
| 145 | + files.push({ |
| 146 | + path: fullPath, |
| 147 | + isNew: modifiedFiles.has(fullPath), |
| 148 | + content, |
| 149 | + frontMatter |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + searchDirectory(path.resolve(rootDir)); |
| 157 | + return files; |
| 158 | +} |
| 159 | + |
| 160 | +// If run directly, output found files |
| 161 | +if (require.main === module) { |
| 162 | + const mode = (process.argv[2] as FinderMode) || 'standard'; |
| 163 | + const rootDir = process.argv[3] || '../../content'; |
| 164 | + const files = findMarkdownFiles(rootDir, mode); |
| 165 | + console.log(JSON.stringify(files, null, 2)); |
| 166 | +} |
0 commit comments