-
Notifications
You must be signed in to change notification settings - Fork 249
new blog linting rules #13947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamgordonbell
wants to merge
3
commits into
master
Choose a base branch
from
agb/build-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
new blog linting rules #13947
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...t/events/connecting-securing-and-scaling-microservices-with-kubernetes/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { execSync } from 'child_process'; | ||
import { z } from 'zod'; | ||
import { parse as parseMatter } from 'zod-matter'; | ||
|
||
const frontMatterSchema = z.object({}).catchall(z.any()); | ||
|
||
type FrontMatter = z.infer<typeof frontMatterSchema>; | ||
|
||
export interface MarkdownFile { | ||
path: string; | ||
isNew: boolean; | ||
content: string; | ||
// frontMatter can be anything that we parse out | ||
frontMatter?: FrontMatter; | ||
} | ||
|
||
/** | ||
* Different modes for finding markdown files and determining if they're new | ||
*/ | ||
export type FinderMode = 'git' | 'gha'; | ||
|
||
const AUTO_GENERATED_HEADING_REGEX = /^> This page was automatically generated\./m; | ||
|
||
/** | ||
* Parse front matter from a markdown file | ||
*/ | ||
function parseFrontMatter(filePath: string): FrontMatter | undefined { | ||
try { | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
|
||
// Check for auto-generated heading in content | ||
if (content.match(AUTO_GENERATED_HEADING_REGEX)) { | ||
return { no_edit_this_page: true }; | ||
} | ||
|
||
// Parse front matter with zod-matter | ||
const { data } = parseMatter(content, frontMatterSchema); | ||
return data; | ||
} catch (e) { | ||
// If there's no front matter or it's invalid, return null | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Check if a file should be excluded based on its front matter | ||
*/ | ||
function shouldExcludeByFrontMatter(frontMatter: FrontMatter | undefined): boolean { | ||
if (!frontMatter) return false; | ||
|
||
return frontMatter.no_edit_this_page === true || | ||
typeof frontMatter.redirect_to === 'string' || | ||
frontMatter.block_external_search_index === true || | ||
!!frontMatter.allow_long_title; | ||
} | ||
|
||
import { debug } from './utils'; | ||
|
||
/** | ||
* Get list of files that are newly added in the current branch compared to base branch, | ||
* including staged and unstaged changes. Handles both local git and GitHub Actions environments. | ||
*/ | ||
function getGitModifiedFiles(baseBranch?: string): Set<string> { | ||
try { | ||
debug('Getting git modified files...'); | ||
const newFiles = new Set<string>(); | ||
|
||
// In GitHub Actions, use FETCH_HEAD since we just fetched the base branch | ||
const isGHA = process.env.GITHUB_ACTIONS === 'true'; | ||
const baseRef = isGHA ? 'FETCH_HEAD' : (baseBranch || process.env.BASE_BRANCH || 'master'); | ||
debug('Using base ref:', baseRef); | ||
|
||
try { | ||
// In GHA, we've just fetched the base branch to FETCH_HEAD, so this is safe | ||
const diffOutput = execSync(`git diff --name-status ${baseRef} HEAD`, { encoding: 'utf8' }); | ||
debug('Got diff output'); | ||
diffOutput.split('\n') | ||
.filter(line => line.startsWith('A\t')) | ||
.map(line => line.split('\t')[1]) | ||
.forEach(file => newFiles.add(file)); | ||
} catch (error) { | ||
debug('Diff failed:', error); | ||
if (isGHA) { | ||
// In GHA, if diff fails, something is wrong with our setup | ||
throw error; | ||
} | ||
// In local dev, try to get staged/untracked files | ||
debug('Falling back to staged/untracked files only'); | ||
} | ||
|
||
// In local dev, also look for staged and untracked files | ||
if (!isGHA) { | ||
try { | ||
// Get new files from staged changes | ||
const stagedFiles = execSync('git diff --name-status --cached', { encoding: 'utf8' }); | ||
stagedFiles.split('\n') | ||
.filter(line => line.startsWith('A\t')) | ||
.map(line => line.split('\t')[1]) | ||
.forEach(file => newFiles.add(file)); | ||
|
||
// Get untracked files | ||
debug('Getting untracked files...'); | ||
const untrackedFiles = execSync('git ls-files --others --exclude-standard --full-name', { encoding: 'utf8' }); | ||
debug('Raw untracked files output:', untrackedFiles); | ||
untrackedFiles.split('\n').filter(Boolean).forEach(file => newFiles.add(file)); | ||
|
||
// Also get untracked markdown files specifically | ||
const untrackedMarkdown = execSync('git ls-files --others --exclude-standard --full-name "*.md"', { encoding: 'utf8' }); | ||
debug('Raw untracked markdown files:', untrackedMarkdown); | ||
untrackedMarkdown.split('\n').filter(Boolean).forEach(file => newFiles.add(file)); | ||
} catch (error) { | ||
debug('Failed to get staged/untracked files:', error); | ||
} | ||
} | ||
|
||
debug('Found new files:', newFiles); | ||
return newFiles; | ||
} catch (error) { | ||
console.warn('Failed to get git modified files:', error); | ||
if (process.env.GITHUB_ACTIONS === 'true') { | ||
console.warn('Running in GitHub Actions - treating all files as existing'); | ||
} else { | ||
console.warn('Running locally - treating all files as existing'); | ||
} | ||
return new Set<string>(); | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* Find all markdown files in the given directory and its subdirectoriesA | ||
* Optionally marks files as new based on git/GHA context. | ||
*/ | ||
export function findMarkdownFiles( | ||
rootDir: string, | ||
mode: FinderMode = 'git', | ||
excludePaths: string[] = ['/content/docs/reference/pkg', '/content/registry', '/node_modules'] | ||
): MarkdownFile[] { | ||
const files: MarkdownFile[] = []; | ||
|
||
// Determine which files are considered new based on mode | ||
let modifiedFiles = new Set<string>(); | ||
if (mode === 'git') { | ||
modifiedFiles = getGitModifiedFiles(); | ||
} else if (mode === 'gha') { | ||
const baseBranch = process.env.BASE_BRANCH; | ||
debug('GHA mode using base branch:', baseBranch); | ||
modifiedFiles = getGitModifiedFiles(baseBranch); | ||
} | ||
|
||
function isExcluded(filePath: string): boolean { | ||
return excludePaths.some(excludePath => filePath.includes(excludePath)); | ||
} | ||
|
||
function searchDirectory(dir: string) { | ||
const entries = fs.readdirSync(dir); | ||
|
||
for (const entry of entries) { | ||
const fullPath = path.join(dir, entry); | ||
const stat = fs.statSync(fullPath); | ||
|
||
if (isExcluded(fullPath)) { | ||
continue; | ||
} | ||
|
||
if (stat.isDirectory()) { | ||
searchDirectory(fullPath); | ||
continue; | ||
} | ||
|
||
if (path.extname(fullPath) === '.md') { | ||
const content = fs.readFileSync(fullPath, 'utf8'); | ||
const frontMatter = parseFrontMatter(fullPath); | ||
if (!shouldExcludeByFrontMatter(frontMatter)) { | ||
// Convert absolute path to relative for comparing with git modified files | ||
const relPath = path.relative(process.cwd(), fullPath); | ||
files.push({ | ||
path: fullPath, | ||
isNew: modifiedFiles.has(relPath), | ||
content, | ||
frontMatter | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
searchDirectory(path.resolve(rootDir)); | ||
return files; | ||
} | ||
|
||
// If run directly, output found files | ||
if (require.main === module) { | ||
const mode = (process.argv[2] as FinderMode) || 'gha'; | ||
const showAll = process.argv.includes('--show-all'); | ||
debug('Running in mode:', mode, 'show all:', showAll); | ||
|
||
// Find all markdown files | ||
const files = findMarkdownFiles('.', mode); | ||
|
||
if (showAll) { | ||
console.log('All markdown files (new files marked with *):') | ||
console.log(JSON.stringify(files.map(f => `${f.isNew ? '*' : ' '} ${f.path}`), null, 2)); | ||
} else { | ||
const newFiles = files.filter(f => f.isNew).map(f => f.path); | ||
console.log('New markdown files detected:'); | ||
console.log(JSON.stringify(newFiles, null, 2)); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adapted from previous lint-markdown.js but expanded to cover if file is net new when compared to parent branch.