New in Symplify 9: Markdown Diff
When we maintain a package docs or generate documentation in Markdown, we write a code snippet from time to time. The clearest way to show what you exactly mean is a diff. That's why diff is used in GitHub commit suggestions.
"Diff is worth 100 code-review words."
But creating automated diff in Markdown is a pickle. We picked the pickle and turned it into a package - symplify/markdown-diff package.
The package does exactly what it says and is mostly used in symplify/rule-doc-generator. I'd say this is the smallest package on Symplify.
3 step to Markdown Diff
- Install Package
composer require symplify/markdown-diff
- Register in
config/bundles.php
use Symplify\MarkdownDiff\Bundle\MarkdownDiffBundle;
return [
MarkdownDiffBundle::class => [
'all' => true,
],
];
- Use it
namespace App;
use Symplify\MarkdownDiff\Differ\MarkdownDiffer;
final class SomeClass
{
/**
* @var MarkdownDiffer
*/
private $markdownDiffer;
public function __construct(MarkdownDiffer $markdownDiffer)
{
$this->markdownDiffer = $markdownDiffer;
}
public function run(): void
{
$markdownDiff = $this->markdownDiffer->diff('oldContent', 'newContent');
// ...
file_put_contents(getcwd() . '/docs/diff.md', $markdownDiff);
}
}
↓
-'oldContent'
+'newContent'
Pretty simple, right?
Smarter than Normal
Compared to sebastian/diff it builds on, this package does an extra cleaning job:
- removes line numbers
- removes
---/+++ @@
spam - removes trailing white space
- most important one: provides full diff, so you'll get full contents of compared files
That's all for today.
Happy coding!
Have you find this post useful? Do you want more?
Follow me on Twitter, RSS or support me on GitHub Sponsors.