diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 63d6044..e7a5032 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,7 +12,7 @@ jobs: fail-fast: false matrix: php: [8.1] - statamic: [4.0.*, 5.0.*] + statamic: [4.6.*, 5.*] os: [ubuntu-latest] laravel: [10.*] testbench: [8.*] diff --git a/src/Tags/Toc.php b/src/Tags/Toc.php index 4468931..ab77445 100644 --- a/src/Tags/Toc.php +++ b/src/Tags/Toc.php @@ -26,6 +26,12 @@ class Toc extends Tags */ public function index() { + if ($this->params->get('when', true) === false) { + return []; + } + + // get the supported header-levels + // get the supported header-levels $depth = $this->params->int("depth", 3); $start = $this->params->get('from', "h1"); diff --git a/tests/Unit/RecursionTest.php b/tests/Unit/RecursionTest.php new file mode 100644 index 0000000..d0aa89b --- /dev/null +++ b/tests/Unit/RecursionTest.php @@ -0,0 +1,50 @@ +put('toc', \Goldnead\StatamicToc\Tags\Toc::class); + } + + /** @test */ + public function it_renders_toc_with_when_param() + { + $content = '

Heading 1

Heading 2

'; + + $template = <<<'EOT' +
    + {{ toc field="content" :when="show_toc" }} +
  1. + {{ toc_title }} + {{ if children }} +
      + {{ *recursive children* }} +
    + {{ /if }} +
  2. + {{ /toc }} +
+EOT; + + $data = [ + 'content' => $content, + 'show_toc' => true, + ]; + + $output = (string) Antlers::parse($template, $data); + + $this->assertStringContainsString('Heading 1', $output); + + // Test false + $data['show_toc'] = false; + $output = (string) Antlers::parse($template, $data); + $this->assertStringNotContainsString('Heading 1', $output); + } +}