Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra docs for Statamic static site generation #22

Open
stevebauman opened this issue Jan 6, 2022 · 7 comments
Open

Extra docs for Statamic static site generation #22

stevebauman opened this issue Jan 6, 2022 · 7 comments

Comments

@stevebauman
Copy link

stevebauman commented Jan 6, 2022

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! 😄

@aarondfrancis
Copy link
Contributor

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!

@mbootsman
Copy link

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.

@joshuablum
Copy link

joshuablum commented Sep 12, 2023

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.

@stevebauman
Copy link
Author

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

@joshuablum
Copy link

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

@simonhamp
Copy link

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

😏

@stevebauman
Copy link
Author

lmao I would rather DIE @simonhamp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants