Skip to content

Commit 5c1843d

Browse files
committed
Run linting/fixing before compare
1 parent 7b2cf10 commit 5c1843d

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

.github/workflows/verify-docs.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@ jobs:
2222
coverage: none
2323

2424
- name: Install dependencies
25-
run: composer install --prefer-dist --no-progress
25+
run: |
26+
# Install PHP dependencies
27+
composer install --prefer-dist --no-progress
28+
# Install Node.js dependencies
29+
npm ci
2630
2731
- name: Generate fresh documentation
2832
run: |
2933
# Create temp directory for fresh docs
3034
mkdir -p temp/code-reference
31-
# Generate fresh documentation
32-
php docs/bin/generate-parsed-md.php --output=temp/code-reference
35+
36+
# Run full docs workflow with new output directory
37+
DOCS_OUTPUT_DIR=temp/code-reference composer run docs
3338
3439
- name: Compare documentation
3540
run: |
36-
# Compare generated docs with existing docs
37-
if ! diff -r --ignore-all-space --ignore-blank-lines docs/code-reference temp/code-reference; then
41+
if ! php docs/bin/verify-docs.php; then
3842
echo "::error::Documentation in docs/code-reference does not match what would be generated from source code."
39-
echo "::error::Please update the source code documentation instead of modifying the generated files directly."
43+
echo "::error::The markdown files are generated from PHP docblocks - please update the source code documentation instead."
4044
exit 1
4145
fi

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"scripts": {
2424
"docs:manifest": "php docs/bin/generate-manifest.php",
2525
"docs:links": "php docs/bin/update-markdown-links.php",
26-
"docs:parse": "php docs/bin/generate-parsed-md.php",
26+
"docs:parse": "php docs/bin/generate-parsed-md.php --output=${DOCS_OUTPUT_DIR:-docs/code-reference}",
2727
"docs:fix": "npm run fix:md",
2828
"docs:lint": "npm run lint:md",
2929
"docs": [

docs/bin/verify-docs.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Compare generated markdown documentation with existing files.
4+
*
5+
* @package wordpress/secure-custom-fields
6+
*/
7+
8+
namespace WordPress\SCF\Docs;
9+
10+
// phpcs:disable WordPress.WP.AlternativeFunctions
11+
12+
/**
13+
* Normalize markdown content for comparison by removing formatting.
14+
*
15+
* @param string $content The markdown content to normalize.
16+
* @return string Normalized content.
17+
*/
18+
function normalize_content( $content ) {
19+
// Remove URL formatting
20+
$content = preg_replace( '/<(https?:\/\/[^>]+)>/', '$1', $content );
21+
// Normalize list markers
22+
$content = preg_replace( '/^[*-]\s+/m', '- ', $content );
23+
// Normalize whitespace
24+
$content = preg_replace( '/\s+/', ' ', $content );
25+
return trim( $content );
26+
}
27+
28+
/**
29+
* Compare two directories of markdown files.
30+
*
31+
* @param string $dir1 First directory path.
32+
* @param string $dir2 Second directory path.
33+
* @return bool True if content matches, false otherwise.
34+
*/
35+
function compare_dirs( $dir1, $dir2 ) {
36+
$files1 = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir1 ) );
37+
$files2 = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir2 ) );
38+
39+
foreach ( $files1 as $file1 ) {
40+
if ( $file1->isFile() && 'md' === $file1->getExtension() ) {
41+
$relative_path = str_replace( $dir1 . '/', '', $file1->getPathname() );
42+
$file2 = $dir2 . '/' . $relative_path;
43+
44+
if ( ! file_exists( $file2 ) ) {
45+
continue;
46+
}
47+
48+
$content1 = normalize_content( file_get_contents( $file1 ) );
49+
$content2 = normalize_content( file_get_contents( $file2 ) );
50+
51+
if ( $content1 !== $content2 ) {
52+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
53+
echo "Content mismatch in {$relative_path}\n";
54+
return false;
55+
}
56+
}
57+
}
58+
return true;
59+
}
60+
61+
$result = compare_dirs( 'docs/code-reference', 'temp/code-reference' );
62+
exit( $result ? 0 : 1 );

0 commit comments

Comments
 (0)