area17 / blast

Storybook for Laravel Blade 🚀

Home Page:https://dev.to/area17/getting-started-with-blast-storybook-for-laravel-blade-c5c

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Addition of source code to Docs tab in Blast view

RobertAByrnes opened this issue · comments

@mrtimbrook Will do, I had a dive into the Storybook docs about this topic earlier,. Will do more digging. Looks like the dependency is already there.

@mrtimbrook This is an entry point following the docs from Storybook.js, next steps, parsing the source code to create the default configuration then dynamically updating the snippet in the dom on editing in canvas.

Addition to GenerateStories.php

use A17\Blast\Traits\SourceCode;

class GenerateStories extends Command
{
    use Helpers;
    use SourceCode;
    private function buildChildTemplate($item)
    {
        $data = [
            'name' => ucwords(
                str_replace('.blade.php', '', $item['name']),
                '/',
            ),
            'parameters' => [
                'server' => [
                    'id' => str_replace('.blade.php', '', $item['path']),
                ],
                'docs' => [
                    'source' => [
                        'code' => $this->fileContents($this->storyViewsPath.'/'.$item['path'])
                    ]
                ]
            ],
        ];

Addition of Trait SourceCode.php (testing grounds)

<?php

namespace A17\Blast\Traits;

trait SourceCode
{
    private function fileContents($filepath)
    {
        return file_get_contents($filepath);
    }
}

This is great, thanks! I'm going to have a play around with it.

@mrtimbrook Also try: npm install storybook-source-code-addon

Edit main.js to:

module.exports = {
  stories: ['../stories/**/*.stories.mdx', '../stories/**/*.stories.@(json)'],
  addons: [
    '@storybook/addon-postcss',
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-a11y',
    'storybook-addon-designs',
    '@etchteam/storybook-addon-status',
    'storybook-source-code-addon'
  ],
  core: {
    builder: 'webpack5'
  }
};

And to allow doc to be rendered in the canvas edit GenerateStories.php to:

 private function buildChildTemplate($item)
    {
        $data = [
            'name' => ucwords(
                str_replace('.blade.php', '', $item['name']),
                '/',
            ),
            'parameters' => [
                'server' => [
                    'id' => str_replace('.blade.php', '', $item['path']),
                ],
                'docs' => [
                    'source' => [
                        'code' => $this->fileContents($this->storyViewsPath.'/'.$item['path'])
                    ]
                ],
                'componentSource' => [
                    'code' => $this->fileContents($this->storyViewsPath.'/'.$item['path'])
                ]
            ],
        ];

@RobertAByrnes Thanks for this. I've created a PR - #24.

@mrtimbrook Excellent. I was just looking at chopping the @storybook directive