Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Faker Markdown Generator Laravel Package

davidbadura/faker-markdown-generator

A Faker provider that generates realistic Markdown content for PHP and Laravel projects. Create headings, paragraphs, lists, links, code blocks, and other formatted snippets to seed demos, tests, and fixtures with believable documentation-style text.

View on GitHub
Deep Wiki
Context7

Getting Started

This package is a Faker provider, not a standalone markdown library. It plugs into an existing Faker\Generator and adds formatters that return random-but-valid markdown strings.

Install (Laravel 8+ already ships fakerphp/faker):

composer require davidbadura/faker-markdown-generator:^1.1.1 --dev

The --dev flag is usually correct: the realistic use case is fixtures, seeders, and tests, not production content. Pin at least 1.1.1 — see the blockquote note under Gotchas.

First place to look: the provider class itself, typically src/Provider/Markdown.php. The README is short; the source is the real documentation. Grep the public methods to see the exact formatter names and what each block type produces before you build on them.

Minimal usage:

use DavidBadura\FakerMarkdownGenerator\Provider\Markdown;
use Faker\Factory;

$faker = Factory::create();
$faker->addProvider(new Markdown($faker));

echo $faker->markdown(); // a full random markdown document

In Laravel, the same generator is reachable via fake() or $this->faker inside factories and seeders:

$faker = fake();
$faker->addProvider(new \DavidBadura\FakerMarkdownGenerator\Provider\Markdown($faker));

$body = $faker->markdown();

First use case: a Post factory with a markdown body column. It gives you headings, paragraphs, lists, quotes, and code fences without hand-writing fixture text, and it exercises your renderer (league/commonmark, Blade markdown components, etc.) with more structure than $faker->paragraphs() ever will.

Implementation Patterns

Add the provider once, not per call. Faker's addProvider() appends to an internal list and duplicate additions are tolerated but wasteful. Put the addProvider call in a place that runs once per generator instance.

For Laravel factories, configure() is the natural hook:

class PostFactory extends Factory
{
    public function configure(): static
    {
        $this->faker->addProvider(new Markdown($this->faker));

        return $this;
    }

    public function definition(): array
    {
        return [
            'title' => $this->faker->sentence(),
            'body'  => $this->faker->markdown(),
        ];
    }
}

If you want the provider available to every factory and seeder, extend the container binding in a service provider:

// AppServiceProvider::register()
$this->app->extend(\Faker\Generator::class, function ($faker) {
    if (! $faker->getProviders()/* or your own guard */) {
        $faker->addProvider(new Markdown($faker));
    }
    return $faker;
});

Verify this against your Laravel version — the Faker\Generator binding is an implementation detail, and extend() must run after the binding is registered. If that feels fragile, a tiny helper is safer and more obvious:

function markdownFaker(): \Faker\Generator
{
    static $faker = null;
    if ($faker === null) {
        $faker = fake();
        $faker->addProvider(new Markdown($faker));
    }
    return $faker;
}

Mix with native Faker formatters. The provider only owns its own methods. Combine $faker->markdown() for body content with $faker->sentence(), $faker->slug(), $faker->dateTimeBetween() for the surrounding model attributes. Don't try to use the markdown provider for metadata.

Deterministic fixtures. For snapshot tests or reproducible demos, seed before generating:

$faker = fake();
$faker->seed(42);
$faker->addProvider(new Markdown($faker));

Seed after provider registration only if you control all random calls; otherwise seed at the very start of the test/setup.

Granular blocks in feature tests. Prefer the specific block formatters over markdown() when you need a targeted case — e.g., a single heading or list to test a TOC generator or a list-rendering component. This is especially worth doing for blockquotes: 1.1.1 fixed the blockquote output, so a focused test on the quote formatter is cheap coverage against regressions. Check the provider source for the exact names; the coarse markdown() output is great for "renderer does not blow up" tests, less so for asserting a specific HTML shape.

Seeder usage:

Post::factory()
    ->count(50)
    ->create(['body' => fn () => markdownFaker()->markdown()]);

Or simply rely on the factory's definition(). Avoid calling addProvider inside a closure that runs 50 times.

Integration with renderers. Pipe output through the same parser you use in production (e.g., League\CommonMark\CommonMarkConverter) and store the HTML if that's your pipeline. This catches edge cases in list nesting and table-like structures early — and it's exactly the class of bug the 1.1.1 blockquote fix addresses.

Gotchas and Tips

  • 1.1.1 fixes the blockquote formatter (issues #1 and #3). If you're on an earlier version and you use quotes — or use markdown() at all, since documents can contain quotes — you may have been generating malformed blocks that a lenient parser silently merged into the preceding or following paragraph. Upgrade, then re-check any stored fixtures, snapshots, or seeded content that was generated with the old version. If you generated and committed markdown fixtures, regenerate them.
  • Small package, still low bus factor. There's now post-2021 activity (1.1.1), which is a good sign, but it remains a tiny utility with a handful of stars and dependents. Treat it as such: pin the version, and be prepared to vendor the provider class into tests/Support/ if you only need it in tests. The source is small; copying one file removes a supply-chain and upgrade risk entirely.
  • Faker fork compatibility. Older providers require fzaninotto/faker (abandoned); Laravel 8+ uses fakerphp/faker. Check the package's composer.json constraint before installing, and expect composer require to fail or resolve oddly if it targets the abandoned package.
  • addProvider needs the generator instance. new Markdown() with no argument will fail or produce a provider with no Faker reference. Always new Markdown($faker).
  • Method names are the API contract. Confirm formatter names in src/Provider/Markdown.php rather than guessing from the README. If an expected method is missing, you're likely on a different release than the docs.
  • "Valid markdown" is a claim, not a guarantee against your parser. Strict CommonMark parsers and GFM table/nested-list rules can disagree. Round-trip generated output through your actual renderer in a test rather than trusting the provider. The blockquote fix in 1.1.1 is a concrete reminder: a formatting bug can sit in a generator for years without breaking anything until a stricter parser sees it.
  • Patch upgrades can change output. Even a "fix" release alters the byte-for-byte output for affected block types. If you snapshot raw markdown, expect churn on upgrade; otherwise assert structure (has a ## heading, has a fenced block, has a > line) rather than exact text.
  • Determinism breaks across Faker versions. Generated strings change between Faker releases too. Pin fakerphp/faker if you snapshot raw markdown.
  • Locale matters for prose, not structure. Block layout is random in the same way regardless, but lorem text follows the Faker locale. Set the locale explicitly in tests if content language matters.
  • Don't render generated markdown in production. It's lorem ipsum and fake links. Use it for demos, staging seeders, and tests only; guard seeders behind app()->environment(['local', 'testing']).
  • Performance is fine per document, not per thousand. Generating 50 posts each with a long markdown() body adds up. Cap document size or generate once and reuse in bulk seeders.
  • Extension point: subclass Markdown to add your own block formatters (e.g., a task-list block, a fenced php block with realistic code) and register the subclass instead. That's the clean way to get domain-specific fixtures without forking the whole package — and it's a reasonable hedge if upstream goes quiet again.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource