Kirby JSON output for Uncss

For a while now, i’ve used the JSON api to get a list of all page urls as a json file for use with Uncss, but the main drawback with my approach is the necessary dummy article. I followed the advice in the cookbook originally and tweaked the page code in the cookbook for compatibility with the Uncss postcss plugin:

    <?php
    header('Content-type: application/json; charset=utf-8');
    $root = $site->find('home');
    $firstlevel = $site->children()->visible();
    $secondlevel = $site->children()->children()->visible();
    $thirdlevel = $site->children()->children()->children()->visible();
    $json = array();
    $json[] =  (string)$root->url();

    foreach ($firstlevel as $article) {
        $json[] =  (string)$article->url();
    }

    foreach ($secondlevel as $article) {
        $json[] =  (string)$article->url();
    }

    foreach ($thirdlevel as $article) {
        $json[] =  (string)$article->url();
    }

    echo json_encode($json);

Essentially, I grab the JSON to a file in my projects root by hitting domain.com/uncss with curl on the command line via an NPM script, then pump this into PostCss.

But what i don’t like is the uncss content article in the content folder because clients always say whats that??? i just have to say leave it alone, don’t delete it.

Is there a way to do this without having to have that dummy content folder just the make the url work? Is there a more Ninja way?

The cookbook article is old, we have routes now, make more sense for an API.

(But the client wouldn’t have to see the page if you hide it via its blueprint)

But its in the root of the site. Excuse my ignorance but if i turn off pages, wont that turn off all pages in the site, not just the uncss page?

I suppose you mean in the “root” of the content folder, not the site. If your page has a blueprint, you can set

hide: true

But as I said, a route might be the better option nevertheless.

Ah ha! that could work. And yes, the root of the content folder. Apologies for not setting an appropriate tag on the topic. I shall go and sit in the corner and think about what I’ve done. You have better things to do then clean up after us after all… :slight_smile: