torchlight-laravel icon indicating copy to clipboard operation
torchlight-laravel copied to clipboard

Extra docs for Statamic static site generation

Open stevebauman opened this issue 3 years ago • 7 comments

There were issues I ran into and had to resolve when using Statamic with their Static Site Generation plugin. This isn't an issue with Torchlight exactly but with how SSG is performed in the plugin.

When generating the pages via php please ssg:generate, views are generated with the raw Torchlight placeholders:

Screen Shot 2022-01-06 at 11 19 52 AM

After creating a Statamic custom page generator and forcing the page to be rendered in full by manually calling the RenderTorchlight middleware, we get the full HTML code snippets:

Screen Shot 2022-01-06 at 11 20 00 AM

Results can be seen here: https://stevebauman.ca/create-api-responses-with-eloquent-factories/

Here's the code for forcing code generation:

// app/Providers/AppServiceProvider.php

use App\CustomGenerator;
use Statamic\StaticSite\Tasks;
use Statamic\Facades\Markdown;
use Statamic\StaticSite\Generator;
use Torchlight\Commonmark\V2\TorchlightExtension;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Markdown::addExtension(function () {
        return new TorchlightExtension;
    });

    $this->app->singleton(Generator::class, function ($app) {
        return new CustomGenerator($app, $app['files'], $app['router'], $app[Tasks::class]);
    });
}
// app/CustomGenerator.php

namespace App;

use Statamic\StaticSite\Generator;

class CustomGenerator extends Generator
{
    protected function createPage($content)
    {
        return new CustomPage($this->files, $this->config, $content);
    }
}
// app/CustomPage.php

namespace App;

use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\RedirectResponse;
use Statamic\StaticSite\GeneratedPage;
use Statamic\StaticSite\Page;
use Torchlight\Middleware\RenderTorchlight;

class CustomPage extends Page
{
    protected function write($request)
    {
        $response = (new RenderTorchlight())->handle($request, function ($request) {
            try {
                $response = $this->content->toResponse($request);
            } catch (HttpResponseException $e) {
                $response = $e->getResponse();
                throw_unless($response instanceof RedirectResponse, $e);
            }
            
            // This has to be set/overridden, as additional pages being generated
            // by Statamic SSG will have a `null` content type and Torchlight
            // relies on this to return the full response instead of Blade.
            $response->headers->set('content-type', 'html');

            return $response;
        });

        $html = $response->getContent();

        if (! $this->files->exists($this->directory())) {
            $this->files->makeDirectory($this->directory(), 0755, true);
        }

        $this->files->put($this->path(), $html);

        return new GeneratedPage($this, $response);
    }
}

I hope this helps someone who is looking to do the same thing! 😄

stevebauman avatar Jan 06 '22 16:01 stevebauman

Steve this is amazing, thank you for taking the time to write this up. I'll add it to the docs, I really really appreciate it!

aarondfrancis avatar Jan 19 '22 22:01 aarondfrancis

Thanks for the tip to look at this issue for a solution @joshuablum. I have a pair of red underlines in my code that prevents this from working.

Markdown::addExtension(function () {
   return new TorchlightExtension;
});

Undefined type 'App\Providers\Markdown' And Undefined type 'Torchlight\Commonmark\V2\TorchlightExtension'`

Hope you can point me in the right way.

mbootsman avatar Sep 12 '23 12:09 mbootsman

Looks like you might have forgotten to import Statamic's Markdown facade, @mbootsman?

Try to add use Statamic\Facades\Markdown;, like mentioned here and here.

joshuablum avatar Sep 12 '23 12:09 joshuablum

Thanks @joshuablum -- I missed that import in the example, I've added it in 👍

stevebauman avatar Sep 12 '23 22:09 stevebauman

Don't worry, it's nothing more than a Facade, @stevebauman. Thanks for the great work, really appreciated!

joshuablum avatar Sep 12 '23 22:09 joshuablum

I am shocked... SHOOK! ... to see that these classes are not final @stevebauman

😏

simonhamp avatar Sep 15 '23 15:09 simonhamp

lmao I would rather DIE @simonhamp

stevebauman avatar Sep 15 '23 16:09 stevebauman