Skip to content

Commit 1387006

Browse files
committed
Introduce dynamic language inference for code blocks
The hook now automatically infers the markdown language identifier from the source file extension instead of hardcoding `php`. This allows proper syntax highlighting for GraphQL, JavaScript, TypeScript, and many other file types in the README.
1 parent c55cae1 commit 1387006

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The hook automatically stages the updated README.md if changes are detected, ens
3838

3939
### Syncing Source Code
4040

41-
To sync a code example with a source file, add a comment before your code block:
41+
To sync a code example with a source file, add a comment before your code block. The language identifier (e.g., `php`, `graphql`, `javascript`) is automatically inferred from the file extension:
4242

4343
````markdown
4444
<!-- source: examples/demo.php -->
@@ -47,6 +47,8 @@ To sync a code example with a source file, add a comment before your code block:
4747
```
4848
````
4949

50+
The hook supports a wide range of file extensions including `.php`, `.graphql`, `.gql`, `.js`, `.ts`, `.json`, `.yml`, `.yaml`, `.sql`, and many more.
51+
5052
### Syncing Output (Optional)
5153

5254
To show the output of executing a PHP file, use:

src/SyncReadmeExamples.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ private function syncReadmeWithExamples(string $readme, string $repositoryRoot,
6262
// Check for source comment
6363
if (preg_match('/^<!-- source: (.+) -->$/', trim($line), $matches)) {
6464
$sourceFile = $matches[1];
65+
$language = $this->getLanguageFromExtension($sourceFile);
6566
$result[] = $line; // Keep the source comment
6667
++$i;
6768

6869
// Process the code block
69-
if ($i < count($lines) && preg_match('/^```php\s*$/', $lines[$i])) {
70-
$result[] = $lines[$i]; // Keep ```php
70+
if ($i < count($lines) && preg_match('/^```\w*\s*$/', $lines[$i])) {
71+
$result[] = '```' . $language; // Use inferred language
7172
++$i;
7273

7374
// Skip old code content until closing ```
@@ -262,4 +263,39 @@ private function normalizeOutput(string $output) : string
262263

263264
return implode("\n", $lines);
264265
}
266+
267+
/**
268+
* Infer the language identifier from file extension
269+
*/
270+
private function getLanguageFromExtension(string $filePath) : string
271+
{
272+
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
273+
274+
return match ($extension) {
275+
'php' => 'php',
276+
'graphql', 'gql' => 'graphql',
277+
'js', 'mjs', 'cjs' => 'javascript',
278+
'ts', 'mts', 'cts' => 'typescript',
279+
'json' => 'json',
280+
'yml', 'yaml' => 'yaml',
281+
'xml' => 'xml',
282+
'sql' => 'sql',
283+
'sh', 'bash' => 'bash',
284+
'py' => 'python',
285+
'rb' => 'ruby',
286+
'go' => 'go',
287+
'rs' => 'rust',
288+
'java' => 'java',
289+
'c' => 'c',
290+
'cpp', 'cc', 'cxx' => 'cpp',
291+
'cs' => 'csharp',
292+
'swift' => 'swift',
293+
'kt', 'kts' => 'kotlin',
294+
'md', 'markdown' => 'markdown',
295+
'html', 'htm' => 'html',
296+
'css' => 'css',
297+
'scss', 'sass' => 'scss',
298+
default => '',
299+
};
300+
}
265301
}

0 commit comments

Comments
 (0)